use super::*;
#[test]
fn quotes_sql_values() {
assert_eq!(quote_ident("project\"name"), "\"project\"\"name\"");
assert_eq!(quote_literal("it's"), "'it''s'");
}
#[test]
fn blocked_plans_cannot_be_rendered() {
let error = migration_sql(&MigrationPlan {
changes: vec![crate::PlannedChange {
change: SchemaChange::RemoveTable {
table: TableSchema {
id: "notes".to_owned(),
name: "notes".to_owned(),
columns: Vec::new(),
},
},
risk: crate::ChangeRisk {
schema: crate::SchemaRisk::Destructive,
execution: crate::ExecutionRisk::ManualReview,
},
}],
})
.unwrap_err();
assert!(error.contains("require review"));
}
#[test]
fn renders_identity_enum_json_and_set_null_foreign_keys() {
let schema = DatabaseSchema {
schema_version: crate::SCHEMA_VERSION,
provider: appstruct_ir::DatabaseProvider::Postgres,
tables: vec![TableSchema {
id: "items".to_owned(),
name: "items".to_owned(),
columns: vec![
ColumnSchema {
id: "items.id".to_owned(),
name: "id".to_owned(),
data_type: DatabaseType::Integer,
nullable: false,
primary_key: true,
unique: false,
default: None,
generated: Some(GeneratedValueIr::AutoIncrement),
},
ColumnSchema {
id: "items.status".to_owned(),
name: "status".to_owned(),
data_type: DatabaseType::Enum {
values: vec!["open".to_owned(), "closed".to_owned()],
},
nullable: false,
primary_key: false,
unique: true,
default: Some("open".to_owned()),
generated: None,
},
ColumnSchema {
id: "items.payload".to_owned(),
name: "payload".to_owned(),
data_type: DatabaseType::Json,
nullable: true,
primary_key: false,
unique: false,
default: None,
generated: None,
},
ColumnSchema {
id: "items.active".to_owned(),
name: "active".to_owned(),
data_type: DatabaseType::Boolean,
nullable: false,
primary_key: false,
unique: false,
default: Some("true".to_owned()),
generated: None,
},
],
}],
unique_constraints: vec![UniqueConstraintSchema {
id: "items.pair".to_owned(),
table: "items".to_owned(),
columns: vec!["id".to_owned(), "status".to_owned()],
}],
indexes: Vec::new(),
seeds: vec![crate::SeedSchema {
id: "items.demo".to_owned(),
table: "items".to_owned(),
values: vec![
crate::SeedValueSchema {
column: "active".to_owned(),
value: "true".to_owned(),
data_type: DatabaseType::Boolean,
},
crate::SeedValueSchema {
column: "id".to_owned(),
value: "1".to_owned(),
data_type: DatabaseType::Integer,
},
],
}],
foreign_keys: vec![ForeignKeySchema {
id: "items.parent".to_owned(),
source_table: "items".to_owned(),
source_columns: vec!["tenant_id".to_owned(), "parent_id".to_owned()],
target_table: "items".to_owned(),
target_columns: vec!["tenant_id".to_owned(), "id".to_owned()],
unique: false,
on_delete: OnDeleteIr::SetNull,
}],
};
let sql = initial_migration(&schema);
assert!(sql.contains("GENERATED BY DEFAULT AS IDENTITY"));
assert!(sql.contains("CHECK (\"status\" IN ('open', 'closed'))"));
assert!(sql.contains("JSONB"));
assert!(sql.contains("DEFAULT true"));
assert!(sql.contains("ON DELETE SET NULL (\"parent_id\")"));
assert!(sql.contains("UNIQUE (\"id\", \"status\")"));
assert!(sql.contains("VALUES (true, 1)"));
}
#[test]
fn alter_column_rejects_shape_changes_and_renders_default_drops() {
let before = ColumnSchema {
id: "items.note".to_owned(),
name: "note".to_owned(),
data_type: DatabaseType::Text,
nullable: true,
primary_key: false,
unique: false,
default: Some("x".to_owned()),
generated: None,
};
let mut after = before.clone();
after.default = None;
let sql = alter_column("items", &before, &after).unwrap();
assert!(sql.contains("DROP DEFAULT"));
after.name = "renamed".to_owned();
assert!(alter_column("items", &before, &after).is_err());
let mut not_null = before.clone();
not_null.nullable = false;
assert!(alter_column("items", &before, ¬_null).is_err());
}