Serge Bazanski | 48e9bab | 2023-02-20 15:28:59 +0100 | [diff] [blame] | 1 | package bmdb |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | // TestMigrateUpDown performs a full-up and full-down migration test on an |
| 8 | // in-memory database twice. |
| 9 | // |
| 10 | // Doing this the first time allows us to check the up migrations are valid and |
| 11 | // that the down migrations clean up enough after themselves for earlier down |
| 12 | // migrations to success. |
| 13 | // |
| 14 | // Doing this the second time allows us to make sure the down migrations cleaned |
| 15 | // up enough after themselves that they have left no table/type behind. |
| 16 | func TestMigrateUpDown(t *testing.T) { |
| 17 | // Start with an empty database. |
| 18 | b := dut() |
| 19 | _, err := b.Open(false) |
| 20 | if err != nil { |
| 21 | t.Fatalf("Starting empty database failed: %v", err) |
| 22 | } |
| 23 | |
| 24 | // Migrations go up. |
| 25 | if err := b.Database.MigrateUp(); err != nil { |
| 26 | t.Fatalf("Initial up migration failed: %v", err) |
| 27 | } |
| 28 | // Migrations go down. |
| 29 | if err := b.Database.MigrateDownDangerDanger(); err != nil { |
| 30 | t.Fatalf("Initial down migration failed: %v", err) |
| 31 | } |
| 32 | // Migrations go up. |
| 33 | if err := b.Database.MigrateUp(); err != nil { |
| 34 | t.Fatalf("Second up migration failed: %v", err) |
| 35 | } |
| 36 | // Migrations go down. |
| 37 | if err := b.Database.MigrateDownDangerDanger(); err != nil { |
| 38 | t.Fatalf("Second down migration failed: %v", err) |
| 39 | } |
| 40 | } |
Serge Bazanski | 6963c63 | 2023-04-06 14:55:49 +0200 | [diff] [blame^] | 41 | |
| 42 | // TestMigrateTwice makes sure we don't hit https://review.monogon.dev/1502 again. |
| 43 | func TestMigrateTwice(t *testing.T) { |
| 44 | // Start with an empty database. |
| 45 | b := dut() |
| 46 | _, err := b.Open(false) |
| 47 | if err != nil { |
| 48 | t.Fatalf("Starting empty database failed: %v", err) |
| 49 | } |
| 50 | |
| 51 | // Migrations go up. |
| 52 | if err := b.Database.MigrateUp(); err != nil { |
| 53 | t.Fatalf("Initial up migration failed: %v", err) |
| 54 | } |
| 55 | // Migrations go up again. |
| 56 | if err := b.Database.MigrateUp(); err != nil { |
| 57 | t.Fatalf("Initial up migration failed: %v", err) |
| 58 | } |
| 59 | } |