241 lines
7.3 KiB
Rust
241 lines
7.3 KiB
Rust
use super::*;
|
|
use dever_runtime::database::{Migration, Plan, Statement, scalar::decimal};
|
|
use dever_runtime::value::decimal::Decimal;
|
|
|
|
const FIRST: Sql = Sql::same(
|
|
"CREATE TABLE \"article\" (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL);",
|
|
);
|
|
const SECOND: Sql = Sql::same(
|
|
"CREATE TABLE \"article\" (id INTEGER PRIMARY KEY AUTOINCREMENT, heading TEXT NOT NULL, published BOOLEAN NOT NULL DEFAULT FALSE);",
|
|
);
|
|
static SEED: [Statement; 1] = [Statement {
|
|
sql: Sql::same("INSERT INTO article(id,title) VALUES ($1,$2) ON CONFLICT(id) DO NOTHING"),
|
|
parameters: || Ok(vec![Parameter::Integer(1), Parameter::Text("seed".into())]),
|
|
}];
|
|
static INITIAL: Plan = Plan {
|
|
create: FIRST,
|
|
indexes: Sql::same(""),
|
|
postgres_columns: &[],
|
|
renames: &[],
|
|
migrations: &[],
|
|
seeds: &SEED,
|
|
seed_revision: "first",
|
|
};
|
|
static RENAMED: Plan = Plan {
|
|
create: SECOND,
|
|
indexes: Sql::same(""),
|
|
postgres_columns: &[],
|
|
renames: &[("heading", "title")],
|
|
migrations: &[],
|
|
seeds: &[],
|
|
seed_revision: "second",
|
|
};
|
|
|
|
#[test]
|
|
fn model_schema_rename_and_seeds_preserve_actual_data() {
|
|
let file = File::new("schema-rename");
|
|
task::run(false, 16, async |scope| {
|
|
let database = Database::sqlite(&file.0, true, options(), None).await?;
|
|
let mut executor = database.acquire().await?;
|
|
executor
|
|
.migrate(scope, "article", FIRST, Some(&INITIAL))
|
|
.await?;
|
|
executor
|
|
.execute(
|
|
scope,
|
|
Sql::same("UPDATE article SET title=$1 WHERE id=1"),
|
|
vec![Parameter::Text("edited".into())],
|
|
)
|
|
.await?;
|
|
executor
|
|
.migrate(scope, "article", FIRST, Some(&INITIAL))
|
|
.await?;
|
|
executor
|
|
.migrate(scope, "article", SECOND, Some(&RENAMED))
|
|
.await?;
|
|
executor
|
|
.migrate(scope, "article", SECOND, Some(&RENAMED))
|
|
.await?;
|
|
let records = executor
|
|
.query(
|
|
scope,
|
|
Sql::same("SELECT id,heading,published FROM article"),
|
|
vec![],
|
|
|row| {
|
|
Ok((
|
|
row.get::<i64>(0)?,
|
|
row.get::<String>(1)?,
|
|
row.get::<bool>(2)?,
|
|
))
|
|
},
|
|
)
|
|
.await?;
|
|
assert_eq!(records, [(1, "edited".into(), false)]);
|
|
executor
|
|
.execute(
|
|
scope,
|
|
Sql::same("CREATE INDEX unexpected ON article(heading)"),
|
|
vec![],
|
|
)
|
|
.await?;
|
|
assert_eq!(
|
|
executor
|
|
.migrate(scope, "article", SECOND, Some(&RENAMED))
|
|
.await
|
|
.unwrap_err()
|
|
.kind,
|
|
Kind::Migration
|
|
);
|
|
assert_eq!(
|
|
executor
|
|
.query(scope, COUNT, vec![], |row| row.get::<i64>(0))
|
|
.await?,
|
|
[1]
|
|
);
|
|
Ok::<_, Error>(())
|
|
})
|
|
.unwrap()
|
|
.unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn failed_constraint_migration_rolls_back_structure_and_rows() {
|
|
const STRICT: Sql = Sql::same(
|
|
"CREATE TABLE \"article\" (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL CHECK(_dever_length(title)<3));",
|
|
);
|
|
static PLAN: Plan = Plan {
|
|
create: STRICT,
|
|
indexes: Sql::same(""),
|
|
postgres_columns: &[],
|
|
renames: &[],
|
|
migrations: &[],
|
|
seeds: &[],
|
|
seed_revision: "strict",
|
|
};
|
|
let file = File::new("schema-rollback");
|
|
task::run(false, 16, async |scope| {
|
|
let database = Database::sqlite(&file.0, true, options(), None).await?;
|
|
let mut executor = database.acquire().await?;
|
|
executor
|
|
.migrate(scope, "article", FIRST, Some(&INITIAL))
|
|
.await?;
|
|
assert_eq!(
|
|
executor
|
|
.migrate(scope, "article", STRICT, Some(&PLAN))
|
|
.await
|
|
.unwrap_err()
|
|
.kind,
|
|
Kind::Constraint
|
|
);
|
|
executor
|
|
.migrate(scope, "article", FIRST, Some(&INITIAL))
|
|
.await?;
|
|
assert_eq!(
|
|
executor
|
|
.query(
|
|
scope,
|
|
Sql::same("SELECT title FROM article"),
|
|
vec![],
|
|
|row| row.get::<String>(0)
|
|
)
|
|
.await?,
|
|
["seed"]
|
|
);
|
|
assert_eq!(
|
|
executor
|
|
.query(scope, Sql::same("PRAGMA foreign_keys"), vec![], |row| row
|
|
.get::<i64>(
|
|
0
|
|
))
|
|
.await?,
|
|
[1]
|
|
);
|
|
Ok::<_, Error>(())
|
|
})
|
|
.unwrap()
|
|
.unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn column_removal_requires_explicit_immutable_migration() {
|
|
const REMOVED: Sql =
|
|
Sql::same("CREATE TABLE \"article\" (id INTEGER PRIMARY KEY AUTOINCREMENT);");
|
|
static UNSAFE: Plan = Plan {
|
|
create: REMOVED,
|
|
indexes: Sql::same(""),
|
|
postgres_columns: &[],
|
|
renames: &[],
|
|
migrations: &[],
|
|
seeds: &[],
|
|
seed_revision: "removed",
|
|
};
|
|
static SAFE: Plan = Plan {
|
|
migrations: &[Migration {
|
|
name: "remove_title",
|
|
revision: "one",
|
|
drops: &["title"],
|
|
before: &[],
|
|
after: &[],
|
|
}],
|
|
..UNSAFE
|
|
};
|
|
static REWRITTEN: Plan = Plan {
|
|
migrations: &[Migration {
|
|
name: "remove_title",
|
|
revision: "changed",
|
|
drops: &["title"],
|
|
before: &[],
|
|
after: &[],
|
|
}],
|
|
..UNSAFE
|
|
};
|
|
let file = File::new("schema-drop");
|
|
task::run(false, 16, async |scope| {
|
|
let database = Database::sqlite(&file.0, true, options(), None).await?;
|
|
let mut executor = database.acquire().await?;
|
|
executor
|
|
.migrate(scope, "article", FIRST, Some(&INITIAL))
|
|
.await?;
|
|
assert_eq!(
|
|
executor
|
|
.migrate(scope, "article", REMOVED, Some(&UNSAFE))
|
|
.await
|
|
.unwrap_err()
|
|
.kind,
|
|
Kind::Migration
|
|
);
|
|
executor
|
|
.migrate(scope, "article", REMOVED, Some(&SAFE))
|
|
.await?;
|
|
assert_eq!(
|
|
executor
|
|
.migrate(scope, "article", REMOVED, Some(&REWRITTEN))
|
|
.await
|
|
.unwrap_err()
|
|
.kind,
|
|
Kind::Migration
|
|
);
|
|
assert_eq!(
|
|
executor
|
|
.query(scope, COUNT, vec![], |row| row.get::<i64>(0))
|
|
.await?,
|
|
[1]
|
|
);
|
|
Ok::<_, Error>(())
|
|
})
|
|
.unwrap()
|
|
.unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn decimal_storage_keeps_exact_order_and_never_rounds_away_digits() {
|
|
let values = ["-99.99", "-1", "0", "0.01", "99.99"].map(|text| Decimal::parse(text).unwrap());
|
|
let encoded = values.map(|value| decimal::encode(value, 4, 2).unwrap());
|
|
assert!(encoded.windows(2).all(|pair| pair[0] < pair[1]));
|
|
for (value, encoded) in values.iter().zip(&encoded) {
|
|
assert_eq!(*value, decimal::decode(encoded, 4, 2).unwrap());
|
|
}
|
|
assert!(decimal::encode(Decimal::parse("0.001").unwrap(), 4, 2).is_err());
|
|
assert!(decimal::encode(Decimal::parse("100").unwrap(), 4, 2).is_err());
|
|
}
|