1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! Schema 13 -> 14: `cards.labels TEXT` added. The column is nullable so
//! pre-migration rows that never had `labels` read back as `labels == []`
//! (see `row_to_card` in `conversions.rs`).
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use tempfile::TempDir;
use super::super::{SqliteStore, SUPPORTED_SCHEMA_VERSION};
use super::make_rt;
use kanban_domain::DataStore;
/// Open the current-v14 DB, then `DROP COLUMN labels` and stamp
/// `schema_version = 13` to simulate a pre-migration state. Reopening with
/// `SqliteStore::open` must trigger `migrate_v13_to_v14_tags`, which adds
/// the `labels` column back. The pre-existing card should then read back
/// with `labels == []`.
#[test]
fn test_sqlite_migration_v13_to_v14_adds_tags_empty() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("t.sqlite3");
let rt = make_rt();
rt.block_on(async {
// Step 1: seed a real DB (board, column, card with a known label)
let card_id = {
let store = SqliteStore::open(&path).await.unwrap();
let board = kanban_domain::Board::new("B", Some("BBB"));
let col = kanban_domain::Column::new(board.id, "Col", 0);
let mut card = kanban_domain::Card::new(board.id, col.id, "Card", 0);
card.labels = vec!["seed".to_string()];
let card_id = card.id;
store.upsert_board(board).unwrap();
store.upsert_column(col).unwrap();
store.upsert_card(card).unwrap();
store.close().await;
card_id
};
// Step 2: drop the labels column and stamp schema_version = 13
{
let pool = SqlitePoolOptions::new()
.max_connections(1)
.connect_with(
SqliteConnectOptions::new()
.filename(&path)
.foreign_keys(false),
)
.await
.unwrap();
sqlx::query("ALTER TABLE cards DROP COLUMN labels")
.execute(&pool)
.await
.unwrap();
sqlx::query("UPDATE metadata SET schema_version = 13 WHERE id = 1")
.execute(&pool)
.await
.unwrap();
pool.close().await;
}
// Step 3: reopening triggers the 13 -> 14 migration
let store = SqliteStore::open(&path).await.unwrap();
// Step 4: binary constant must be 15 (labels migration added v14 -> v15)
assert_eq!(SUPPORTED_SCHEMA_VERSION, 15);
// Step 5: schema_version in the DB is now 14
let version: u32 = sqlx::query_scalar("SELECT schema_version FROM metadata WHERE id = 1")
.fetch_one(&store.pool)
.await
.unwrap();
assert_eq!(
version, SUPPORTED_SCHEMA_VERSION,
"v13 -> v14 migration must bump the schema marker"
);
// Step 6: the pre-migration card reads back with an empty label list
let card = store
.get_card(card_id)
.unwrap()
.expect("card should still be present after migration");
assert_eq!(
card.labels,
Vec::<String>::new(),
"a pre-migration card must read back with labels == []"
);
});
}