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:
@@ -39,7 +39,6 @@ CREATE TABLE IF NOT EXISTS messages (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_messages_received_at ON messages (received_at);
|
CREATE INDEX IF NOT EXISTS idx_messages_received_at ON messages (received_at);
|
||||||
CREATE INDEX IF NOT EXISTS idx_messages_folder ON messages (folder, received_at);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_messages_thread_id ON messages (thread_id);
|
CREATE INDEX IF NOT EXISTS idx_messages_thread_id ON messages (thread_id);
|
||||||
CREATE INDEX IF NOT EXISTS idx_messages_source ON messages (source_address);
|
CREATE INDEX IF NOT EXISTS idx_messages_source ON messages (source_address);
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -34,6 +35,59 @@ func seedMessage(t *testing.T, s *Store, ctx context.Context, gmailID, subject,
|
|||||||
return id
|
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) {
|
func TestMigrationAddsFolderColumns(t *testing.T) {
|
||||||
s, ctx := testStore(t)
|
s, ctx := testStore(t)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user