|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/jmoiron/sqlx" |
| 8 | + "github.com/knadh/dictpress/internal/migrations" |
| 9 | + "github.com/knadh/koanf/v2" |
| 10 | + "github.com/knadh/stuffbin" |
| 11 | + "github.com/lib/pq" |
| 12 | + "golang.org/x/mod/semver" |
| 13 | +) |
| 14 | + |
| 15 | +// migFunc represents a migration function for a particular version. |
| 16 | +// fn (generally) executes database migrations and additionally |
| 17 | +// takes the filesystem and config objects in case there are additional bits |
| 18 | +// of logic to be performed before executing upgrades. fn is idempotent. |
| 19 | +type migFunc struct { |
| 20 | + version string |
| 21 | + fn func(*sqlx.DB, stuffbin.FileSystem, *koanf.Koanf) error |
| 22 | +} |
| 23 | + |
| 24 | +// migList is the list of available migList ordered by the semver. |
| 25 | +// Each migration is a Go file in internal/migrations named after the semver. |
| 26 | +// The functions are named as: v0.7.0 => migrations.V0_7_0() and are idempotent. |
| 27 | +var migList = []migFunc{ |
| 28 | + {"v2.0.0", migrations.V2_0_0}, |
| 29 | +} |
| 30 | + |
| 31 | +// upgrade upgrades the database to the current version by running SQL migration files |
| 32 | +// for all version from the last known version to the current one. |
| 33 | +func upgrade(db *sqlx.DB, fs stuffbin.FileSystem, prompt bool) { |
| 34 | + if prompt { |
| 35 | + var ok string |
| 36 | + fmt.Printf("** IMPORTANT: Take a backup of the database before upgrading.\n") |
| 37 | + fmt.Print("continue (y/n)? ") |
| 38 | + if _, err := fmt.Scanf("%s", &ok); err != nil { |
| 39 | + lo.Fatalf("error reading value from terminal: %v", err) |
| 40 | + } |
| 41 | + if strings.ToLower(ok) != "y" { |
| 42 | + fmt.Println("upgrade cancelled") |
| 43 | + return |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + _, toRun, err := getPendingMigrations(db) |
| 48 | + if err != nil { |
| 49 | + lo.Fatalf("error checking migrations: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // No migrations to run. |
| 53 | + if len(toRun) == 0 { |
| 54 | + lo.Printf("no upgrades to run. Database is up to date.") |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + // Execute migrations in succession. |
| 59 | + for _, m := range toRun { |
| 60 | + lo.Printf("running migration %s", m.version) |
| 61 | + if err := m.fn(db, fs, ko); err != nil { |
| 62 | + lo.Fatalf("error running migration %s: %v", m.version, err) |
| 63 | + } |
| 64 | + |
| 65 | + // Record the migration version in the settings table. There was no |
| 66 | + // settings table until v0.7.0, so ignore the no-table errors. |
| 67 | + if err := recordMigrationVersion(m.version, db); err != nil { |
| 68 | + if isTableNotExistErr(err) { |
| 69 | + continue |
| 70 | + } |
| 71 | + lo.Fatalf("error recording migration version %s: %v", m.version, err) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + lo.Printf("upgrade complete") |
| 76 | +} |
| 77 | + |
| 78 | +// checkUpgrade checks if the current database schema matches the expected |
| 79 | +// binary version. |
| 80 | +func checkUpgrade(db *sqlx.DB) { |
| 81 | + lastVer, toRun, err := getPendingMigrations(db) |
| 82 | + if err != nil { |
| 83 | + lo.Fatalf("error checking migrations: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + // No migrations to run. |
| 87 | + if len(toRun) == 0 { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + var vers []string |
| 92 | + for _, m := range toRun { |
| 93 | + vers = append(vers, m.version) |
| 94 | + } |
| 95 | + |
| 96 | + lo.Fatalf(`there are %d pending database upgrade(s): %v. The last upgrade was %s. Backup the database and run --upgrade`, |
| 97 | + len(toRun), vers, lastVer) |
| 98 | +} |
| 99 | + |
| 100 | +// getPendingMigrations gets the pending migrations by comparing the last |
| 101 | +// recorded migration in the DB against all migrations listed in `migrations`. |
| 102 | +func getPendingMigrations(db *sqlx.DB) (string, []migFunc, error) { |
| 103 | + lastVer, err := getLastMigrationVersion(db) |
| 104 | + if err != nil { |
| 105 | + return "", nil, err |
| 106 | + } |
| 107 | + |
| 108 | + // Iterate through the migration versions and get everything above the last |
| 109 | + // upgraded semver. |
| 110 | + var toRun []migFunc |
| 111 | + for i, m := range migList { |
| 112 | + if semver.Compare(m.version, lastVer) > 0 { |
| 113 | + toRun = migList[i:] |
| 114 | + break |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return lastVer, toRun, nil |
| 119 | +} |
| 120 | + |
| 121 | +// getLastMigrationVersion returns the last migration semver recorded in the DB. |
| 122 | +// If there isn't any, `v0.0.0` is returned. |
| 123 | +func getLastMigrationVersion(db *sqlx.DB) (string, error) { |
| 124 | + var v string |
| 125 | + if err := db.Get(&v, ` |
| 126 | + SELECT COALESCE( |
| 127 | + (SELECT value->>-1 FROM settings WHERE key='migrations'), |
| 128 | + 'v0.0.0')`); err != nil { |
| 129 | + if isTableNotExistErr(err) { |
| 130 | + return "v0.0.0", nil |
| 131 | + } |
| 132 | + return v, err |
| 133 | + } |
| 134 | + return v, nil |
| 135 | +} |
| 136 | + |
| 137 | +// isTableNotExistErr checks if the given error represents a Postgres/pq |
| 138 | +// "table does not exist" error. |
| 139 | +func isTableNotExistErr(err error) bool { |
| 140 | + if p, ok := err.(*pq.Error); ok { |
| 141 | + // `settings` table does not exist. It was introduced in v0.7.0. |
| 142 | + if p.Code == "42P01" { |
| 143 | + return true |
| 144 | + } |
| 145 | + } |
| 146 | + return false |
| 147 | +} |
0 commit comments