MCPcopy Index your code
hub / github.com/coder/coder / main

Function main

scripts/migrate-test/main.go:29–106  ·  view source on GitHub ↗

This script validates the migration path between two versions. It performs the following actions: Given OLD_VERSION and NEW_VERSION: 1. Checks out $OLD_VERSION and inits schema at that version. 2. Checks out $NEW_VERSION and runs migrations. 3. Compares database schema post-migrate to that in VCS. I

()

Source from the content-addressed store, hash-verified

27// 3. Compares database schema post-migrate to that in VCS.
28// If any diffs are found, exits with an error.
29func main() {
30 var (
31 migrateFromVersion string
32 migrateToVersion string
33 postgresURL string
34 skipCleanup bool
35 )
36
37 flag.StringVar(&migrateFromVersion, "from", "", "Migrate from this version")
38 flag.StringVar(&migrateToVersion, "to", "", "Migrate to this version")
39 flag.StringVar(&postgresURL, "postgres-url", "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable", "Postgres URL to migrate")
40 flag.BoolVar(&skipCleanup, "skip-cleanup", false, "Do not clean up on exit.")
41 flag.Parse()
42
43 if migrateFromVersion == "" || migrateToVersion == "" {
44 _, _ = fmt.Fprintln(os.Stderr, "must specify --from=<old version> and --to=<new version>")
45 os.Exit(1)
46 }
47
48 _, _ = fmt.Fprintf(os.Stderr, "Read schema at version %q\n", migrateToVersion)
49 expectedSchemaAfter, err := gitShow("coderd/database/dump.sql", migrateToVersion)
50 if err != nil {
51 panic(err)
52 }
53
54 _, _ = fmt.Fprintf(os.Stderr, "Read migrations for %q\n", migrateFromVersion)
55 migrateFromFS, err := makeMigrateFS(migrateFromVersion)
56 if err != nil {
57 panic(err)
58 }
59 _, _ = fmt.Fprintf(os.Stderr, "Read migrations for %q\n", migrateToVersion)
60 migrateToFS, err := makeMigrateFS(migrateToVersion)
61 if err != nil {
62 panic(err)
63 }
64
65 _, _ = fmt.Fprintf(os.Stderr, "Connect to postgres\n")
66 conn, err := sql.Open("postgres", postgresURL)
67 if err != nil {
68 panic(err)
69 }
70 defer conn.Close()
71
72 ver, err := checkMigrateVersion(conn)
73 if err != nil {
74 panic(err)
75 }
76 if ver < 0 {
77 _, _ = fmt.Fprintf(os.Stderr, "No previous migration detected.\n")
78 } else {
79 _, _ = fmt.Fprintf(os.Stderr, "Detected migration version %d\n", ver)
80 }
81
82 _, _ = fmt.Fprintf(os.Stderr, "Init database at version %q\n", migrateFromVersion)
83 if err := migrations.UpWithFS(conn, migrateFromFS); err != nil {
84 friendlyError(os.Stderr, err, migrateFromVersion, migrateToVersion)
85 panic("")
86 }

Callers

nothing calls this directly

Calls 12

UpWithFSFunction · 0.92
PGDumpSchemaOnlyFunction · 0.92
gitShowFunction · 0.85
makeMigrateFSFunction · 0.85
checkMigrateVersionFunction · 0.85
friendlyErrorFunction · 0.85
stripGenPreambleFunction · 0.85
ExitMethod · 0.80
ParseMethod · 0.65
CloseMethod · 0.65
OpenMethod · 0.45
ErrorfMethod · 0.45

Tested by

no test coverage detected