Phase 9, part 3: schema migrations for both databases
Migrations from schema 30 on are one list (internal/db/migrations.go), written in SQLite's SQL and translated for Postgres (db.PostgresSQL) unless one gives its own. On Postgres the whole migration is one transaction under an advisory lock, and each step is recorded in schema_migrations. TestSchemasMatch compares the two schemas (tables, columns, types, NOT NULL, nocase, indexes), with and without a new migration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -34,25 +34,25 @@ func TestBeforeMigrate(t *testing.T) {
|
||||
// One migration behind: backed up first, before the migration runs.
|
||||
old := filepath.Join(dir, "old.db")
|
||||
raw, _ := sql.Open("sqlite", "file:"+old)
|
||||
for _, m := range migrations[:len(migrations)-1] {
|
||||
if _, err := raw.Exec(m); err != nil {
|
||||
for i := 0; i < Schema()-1; i++ {
|
||||
if _, err := raw.Exec(sqliteStep(i)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
raw.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, len(migrations)-1))
|
||||
raw.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, Schema()-1))
|
||||
raw.Close()
|
||||
d, err = OpenSQLite(old)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d.Close()
|
||||
want := fmt.Sprintf("%d→%d at %d", len(migrations)-1, len(migrations), len(migrations)-1)
|
||||
if len(calls) != 1 || calls[0] != want || Schema() != len(migrations) {
|
||||
want := fmt.Sprintf("%d→%d at %d", Schema()-1, Schema(), Schema()-1)
|
||||
if len(calls) != 1 || calls[0] != want || false {
|
||||
t.Errorf("calls: %v, want %s", calls, want)
|
||||
}
|
||||
// A failed backup stops the migration.
|
||||
raw, _ = sql.Open("sqlite", "file:"+old)
|
||||
raw.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, len(migrations)-1))
|
||||
raw.Exec(fmt.Sprintf(`PRAGMA user_version = %d`, Schema()-1))
|
||||
raw.Close()
|
||||
BeforeMigrate = func(*sql.DB, int, int) error { return errors.New("disk full") }
|
||||
if _, err := OpenSQLite(old); err == nil || !strings.Contains(err.Error(), "disk full") {
|
||||
|
||||
Reference in New Issue
Block a user