Skip to content

Commit 3813b56

Browse files
codexByron
andcommitted
Show that but-db caches open in memory if migrations can't be applied.
This means that newer caches versions that are incompatible won't be overwritten or cleared by older clients. They will instead open in memory, which seems like it's desirable behaviour. Co-authored-by: Sebastian Thiel <sebastian.thiel@icloud.com>
1 parent 5dcef12 commit 3813b56

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

crates/but-db/src/cache/tests.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@ mod open_with_migrations_infallible {
5353
assert!(table_exists(&conn, "foo")?);
5454
Ok(())
5555
}
56+
57+
#[test]
58+
fn existing_cache_with_migration_failure_falls_back_to_memory() -> anyhow::Result<()> {
59+
let tmp = tempfile::tempdir()?;
60+
let url = tmp.path().join("existing-cache.sqlite");
61+
let conn = rusqlite::Connection::open(&url)?;
62+
conn.execute_batch(
63+
"CREATE TABLE `foo`(
64+
`id` TEXT NOT NULL PRIMARY KEY
65+
);",
66+
)?;
67+
drop(conn);
68+
69+
let (conn, actual_url) = open_with_migrations_infallible(&url, migrations());
70+
assert_eq!(
71+
actual_url, ":memory:",
72+
"migration failures on a valid existing cache fall back to memory"
73+
);
74+
assert!(url.exists(), "valid caches are kept on disk");
75+
assert!(table_exists(&conn, "foo")?);
76+
assert!(table_exists(&conn, "__diesel_schema_migrations")?);
77+
78+
let disk_conn = rusqlite::Connection::open(&url)?;
79+
assert!(table_exists(&disk_conn, "foo")?);
80+
assert!(
81+
!table_exists(&disk_conn, "__diesel_schema_migrations")?,
82+
"failed migrations leave the existing cache untouched"
83+
);
84+
Ok(())
85+
}
5686
}
5787

5888
fn migrations() -> impl Iterator<Item = M<'static>> + Clone {

0 commit comments

Comments
 (0)