|
6 | 6 | "database/sql"
|
7 | 7 | "flag"
|
8 | 8 | "fmt"
|
| 9 | +"io" |
9 | 10 | "io/fs"
|
10 | 11 | "os"
|
11 | 12 | "os/exec"
|
@@ -80,27 +81,39 @@ func main() {
|
80 | 81 |
|
81 | 82 | _,_=fmt.Fprintf(os.Stderr,"Init database at version %q\n",migrateFromVersion)
|
82 | 83 | iferr:=migrations.UpWithFS(conn,migrateFromFS);err!=nil {
|
83 |
| -panic(err) |
| 84 | +friendlyError(os.Stderr,err,migrateFromVersion,migrateToVersion) |
| 85 | +os.Exit(1) |
84 | 86 | }
|
85 | 87 |
|
86 | 88 | _,_=fmt.Fprintf(os.Stderr,"Migrate to version %q\n",migrateToVersion)
|
87 | 89 | iferr:=migrations.UpWithFS(conn,migrateToFS);err!=nil {
|
88 |
| -panic(err) |
| 90 | +friendlyError(os.Stderr,err,migrateFromVersion,migrateToVersion) |
| 91 | +os.Exit(1) |
89 | 92 | }
|
90 | 93 |
|
91 | 94 | _,_=fmt.Fprintf(os.Stderr,"Dump schema at version %q\n",migrateToVersion)
|
92 | 95 | dumpBytesAfter,err:=dbtestutil.PGDumpSchemaOnly(postgresURL)
|
93 | 96 | iferr!=nil {
|
94 |
| -panic(err) |
| 97 | +friendlyError(os.Stderr,err,migrateFromVersion,migrateToVersion) |
| 98 | +os.Exit(1) |
95 | 99 | }
|
96 | 100 |
|
97 | 101 | ifdiff:=cmp.Diff(string(dumpBytesAfter),string(stripGenPreamble(expectedSchemaAfter)));diff!="" {
|
98 |
| -_,_=fmt.Fprintf(os.Stderr,"Schema differs from expected after migration: %s\n",diff) |
| 102 | +friendlyError(os.Stderr,xerrors.Errorf("Schema differs from expected after migration: %s",diff),migrateFromVersion,migrateToVersion) |
99 | 103 | os.Exit(1)
|
100 | 104 | }
|
101 | 105 | _,_=fmt.Fprintf(os.Stderr,"OK\n")
|
102 | 106 | }
|
103 | 107 |
|
| 108 | +funcfriendlyError(w io.Writer,errerror,v1,v2string) { |
| 109 | +_,_=fmt.Fprintf(w,"Migrating from version %q to %q failed:\n",v1,v2) |
| 110 | +_,_=fmt.Fprintf(w,"\t%s\n",err.Error()) |
| 111 | +_,_=fmt.Fprintf(w,"Check the following:\n") |
| 112 | +_,_=fmt.Fprintf(w," - All migrations from version %q must exist in version %q with the same migration numbers.\n",v2,v1) |
| 113 | +_,_=fmt.Fprintf(w," - Each migration must have the same effect.\n") |
| 114 | +_,_=fmt.Fprintf(w," - There must be no gaps or duplicates in the migration numbers.\n") |
| 115 | +} |
| 116 | + |
104 | 117 | funcmakeMigrateFS(versionstring) (fs.FS,error) {
|
105 | 118 | // Export the migrations from the requested version to a zip archive
|
106 | 119 | out,err:=exec.Command("git","archive","--format=zip",version,"coderd/database/migrations").CombinedOutput()
|
|