fix(store): run the v1 members migration in one transaction
- store: migrateMembersV1 renames, creates, copies and drops inside a transaction - test: a copy that fails on an orphan row leaves the old members table with its rows and no members_v1
This commit is contained in:
parent
f340078aed
commit
1e8d60b72c
2 changed files with 67 additions and 9 deletions
|
|
@ -467,3 +467,47 @@ func TestThreadListingsDoNotLoadBodies(t *testing.T) {
|
|||
t.Fatal("a single message still has its body")
|
||||
}
|
||||
}
|
||||
|
||||
// The members migration renames, creates, copies and drops. If any step fails, nothing may be left
|
||||
// half done: the old table keeps its rows, and no members_v1 stays behind.
|
||||
func TestFailedMembersMigrationLeavesTheOldTablesIntact(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "old.db")
|
||||
db, err := sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The second member belongs to a list that does not exist: the copy fails on the foreign key.
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE lists (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, domain TEXT NOT NULL, title TEXT NOT NULL DEFAULT '', UNIQUE(name, domain));
|
||||
CREATE TABLE members (list_id INTEGER NOT NULL, address TEXT NOT NULL, display_name TEXT NOT NULL DEFAULT '', PRIMARY KEY(list_id, address));
|
||||
INSERT INTO lists(name, domain, title) VALUES('dev','example.org','Dev');
|
||||
INSERT INTO members(list_id, address, display_name) VALUES(1,'a@x.com','A'),(99,'ghost@x.com','G');`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db.Close()
|
||||
|
||||
if s, err := Open(path); err == nil {
|
||||
s.Close()
|
||||
t.Fatal("the migration must fail on the orphan row")
|
||||
}
|
||||
|
||||
db, err = sql.Open("sqlite", path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
var n int
|
||||
db.QueryRow(`SELECT COUNT(*) FROM members`).Scan(&n)
|
||||
if n != 2 {
|
||||
t.Fatalf("the old members table lost rows: %d", n)
|
||||
}
|
||||
if has, _ := hasColumn(db, "members", "id"); has {
|
||||
t.Fatal("members must still be the old table")
|
||||
}
|
||||
var tables int
|
||||
db.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE name='members_v1'`).Scan(&tables)
|
||||
if tables != 0 {
|
||||
t.Fatal("members_v1 was left behind")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -246,15 +246,7 @@ func migrate(db *sql.DB) error {
|
|||
if hasID, err := hasColumn(db, "members", "id"); err != nil {
|
||||
return err
|
||||
} else if !hasID {
|
||||
if _, err := db.Exec(`ALTER TABLE members RENAME TO members_v1`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(schema); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO members(list_id, address, display_name)
|
||||
SELECT list_id, address, display_name FROM members_v1 ORDER BY list_id, address;
|
||||
DROP TABLE members_v1;`); err != nil {
|
||||
if err := migrateMembersV1(db); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -305,6 +297,28 @@ func migrate(db *sql.DB) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// migrateMembersV1 replaces the milestone 1 members table (no id column) in one transaction, so a
|
||||
// failure or a crash in the middle leaves the old table exactly as it was.
|
||||
func migrateMembersV1(db *sql.DB) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err := tx.Exec(`ALTER TABLE members RENAME TO members_v1`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(schema); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO members(list_id, address, display_name)
|
||||
SELECT list_id, address, display_name FROM members_v1 ORDER BY list_id, address;
|
||||
DROP TABLE members_v1;`); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// addHeldSummary adds the summary columns of the held queue and fills them for the messages
|
||||
// already there, one message at a time so that a big queue is not loaded into memory.
|
||||
func addHeldSummary(db *sql.DB) error {
|
||||
|
|
|
|||
Loading…
Reference in a new issue