Fix migration v4 ordering and add a v3->v4 regression test (AUD-70)
schema.sql runs before the incremental migrations, so the folder index must not live there: on a v3 database the column does not exist yet and startup failed with "no such column: folder". The migration still creates the index. The regression test opens a hand-built v3 database with the current code. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -34,6 +35,59 @@ func seedMessage(t *testing.T, s *Store, ctx context.Context, gmailID, subject,
|
||||
return id
|
||||
}
|
||||
|
||||
func TestMigrationFromV3AddsFolderColumns(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "old.db")
|
||||
|
||||
// Simulate a database created by the v3 release: messages has the IMAP
|
||||
// columns but not folder/mailbox. Opening it with the new code must apply
|
||||
// migration v4 without failing on the folder index (the schema file is
|
||||
// executed before migrations).
|
||||
db, err := sql.Open("sqlite", "file:"+path)
|
||||
if err != nil {
|
||||
t.Fatalf("open old db: %v", err)
|
||||
}
|
||||
stmts := []string{
|
||||
`CREATE TABLE messages (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
gmail_id TEXT NOT NULL UNIQUE,
|
||||
thread_id TEXT, from_addr TEXT, to_addr TEXT, subject TEXT,
|
||||
body_text TEXT, source_address TEXT, date_sent TEXT,
|
||||
imap_uid INTEGER, imap_uidvalidity INTEGER,
|
||||
received_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')))`,
|
||||
`CREATE TABLE schema_migrations (version INTEGER PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT (datetime('now')))`,
|
||||
`INSERT INTO schema_migrations (version) VALUES (3)`,
|
||||
}
|
||||
for _, q := range stmts {
|
||||
if _, err := db.ExecContext(ctx, q); err != nil {
|
||||
t.Fatalf("seed old db: %v", err)
|
||||
}
|
||||
}
|
||||
_ = db.Close()
|
||||
|
||||
s, err := Open(ctx, path)
|
||||
if err != nil {
|
||||
t.Fatalf("Open() on v3 database error = %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
v, err := s.SchemaVersion(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("SchemaVersion() error = %v", err)
|
||||
}
|
||||
if v < 4 {
|
||||
t.Errorf("SchemaVersion() = %d, want >= 4", v)
|
||||
}
|
||||
var n int
|
||||
if err := s.DB().QueryRowContext(ctx, `SELECT COUNT(*) FROM pragma_table_info('messages') WHERE name = 'folder'`).Scan(&n); err != nil {
|
||||
t.Fatalf("count folder column: %v", err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Errorf("folder column count = %d, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationAddsFolderColumns(t *testing.T) {
|
||||
s, ctx := testStore(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user