v0.42.4_fixes
This commit is contained in:
1 parent
efc6eda4cd
commit
51631810e1
29 files changed
+1192
-394
No files matched your search
+139
@@ -1,5 +1,15 @@
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn migration_registry_is_complete_and_ordered() {
|
||||
assert_eq!(MIGRATIONS.first().map(|(version, _)| *version), Some(2));
|
||||
assert_eq!(
|
||||
MIGRATIONS.last().map(|(version, _)| *version),
|
||||
Some(SCHEMA_VERSION)
|
||||
);
|
||||
assert!(MIGRATIONS.windows(2).all(|pair| pair[1].0 == pair[0].0 + 1));
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn persists_and_rejects_stale_writes() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
@@ -208,6 +218,135 @@ mod tests {
|
||||
assert!(store.load().await.unwrap().config.vhosts.is_empty());
|
||||
store.close().await;
|
||||
}
|
||||
async fn seed_v11_with_preferences(path: &Path) {
|
||||
let options = SqliteConnectOptions::new()
|
||||
.filename(path)
|
||||
.create_if_missing(true)
|
||||
.foreign_keys(true);
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect_with(options)
|
||||
.await
|
||||
.unwrap();
|
||||
for &(target, migration) in MIGRATIONS {
|
||||
if target >= 12 {
|
||||
break;
|
||||
}
|
||||
sqlx::raw_sql(migration).execute(&pool).await.unwrap();
|
||||
}
|
||||
sqlx::query(
|
||||
"INSERT INTO administrator(id, username, password_hash, created_at) VALUES(1, 'owner', 'hash', 1)",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("INSERT INTO admin_preferences(administrator_id, document) VALUES(1, ?)")
|
||||
.bind(r#"{"language":"pl","theme":"dark"}"#)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("PRAGMA user_version = 11")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
pool.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn v11_upgrade_keeps_admin_preferences_and_is_not_repeated() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("v11.sqlite3");
|
||||
seed_v11_with_preferences(&path).await;
|
||||
|
||||
let store = Store::open(&path, &Config::default()).await.unwrap();
|
||||
assert_eq!(
|
||||
store.admin_preferences("owner").await.unwrap().as_deref(),
|
||||
Some(r#"{"language":"pl","theme":"dark"}"#)
|
||||
);
|
||||
let version: i64 = sqlx::query_scalar("PRAGMA user_version")
|
||||
.fetch_one(&store.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(version, SCHEMA_VERSION);
|
||||
let large_document = "x".repeat(8192);
|
||||
assert!(store
|
||||
.save_admin_preferences("owner", &large_document)
|
||||
.await
|
||||
.unwrap());
|
||||
assert!(store
|
||||
.save_admin_preferences("owner", &"x".repeat(65537))
|
||||
.await
|
||||
.is_err());
|
||||
store.close().await;
|
||||
|
||||
let reopened = Store::open(&path, &Config::default()).await.unwrap();
|
||||
assert_eq!(
|
||||
reopened
|
||||
.admin_preferences("owner")
|
||||
.await
|
||||
.unwrap()
|
||||
.as_deref(),
|
||||
Some(large_document.as_str())
|
||||
);
|
||||
let fk_check: Vec<(String, i64, String, i64)> = sqlx::query_as("PRAGMA foreign_key_check")
|
||||
.fetch_all(&reopened.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(fk_check.is_empty());
|
||||
sqlx::query("DELETE FROM administrator WHERE id = 1")
|
||||
.execute(&reopened.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(reopened.admin_preferences("owner").await.unwrap().is_none());
|
||||
let rows: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM admin_preferences")
|
||||
.fetch_one(&reopened.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
rows, 0,
|
||||
"administrator preferences must still cascade on delete"
|
||||
);
|
||||
reopened.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn failed_v11_migration_rolls_back_entire_upgrade() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("v11-failed.sqlite3");
|
||||
seed_v11_with_preferences(&path).await;
|
||||
let options = SqliteConnectOptions::new().filename(&path);
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect_with(options)
|
||||
.await
|
||||
.unwrap();
|
||||
sqlx::query("CREATE TABLE admin_preferences_v2 (administrator_id INTEGER PRIMARY KEY)")
|
||||
.execute(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
pool.close().await;
|
||||
|
||||
assert!(Store::open(&path, &Config::default()).await.is_err());
|
||||
let options = SqliteConnectOptions::new().filename(&path);
|
||||
let pool = SqlitePoolOptions::new()
|
||||
.max_connections(1)
|
||||
.connect_with(options)
|
||||
.await
|
||||
.unwrap();
|
||||
let version: i64 = sqlx::query_scalar("PRAGMA user_version")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(version, 11);
|
||||
let original: String =
|
||||
sqlx::query_scalar("SELECT document FROM admin_preferences WHERE administrator_id = 1")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(original, r#"{"language":"pl","theme":"dark"}"#);
|
||||
pool.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn migration_keeps_v1_config_and_revision() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in new issue
Block a user