Skip to content

Commit

Permalink
test(schema-engine): allow SQLite migration to be D1-compatible (#4808)
Browse files Browse the repository at this point in the history
* test(schema-engine): allow SQLite migration to be D1-compatible

* test(schema-engine): fix failures in "migrations::foreign_keys::changing_all_referenced_columns_of_foreign_key_works" and "migrations::relations::adding_mutual_references_on_existing_tables_works"
  • Loading branch information
jkomyno committed May 5, 2024
1 parent 8853577 commit 133a47f
Showing 1 changed file with 10 additions and 1 deletion.
Expand Up @@ -185,6 +185,7 @@ impl SqlRenderer for SqliteFlavour {
fn render_redefine_tables(&self, tables: &[RedefineTable], schemas: MigrationPair<&SqlSchema>) -> Vec<String> {
// Based on 'Making Other Kinds Of Table Schema Changes' from https://www.sqlite.org/lang_altertable.html
let mut result = vec!["PRAGMA foreign_keys=OFF".to_string()];
let mut foreign_key_checks = vec![];

for redefine_table in tables {
let tables = schemas.walk(redefine_table.table_ids);
Expand All @@ -208,9 +209,17 @@ impl SqlRenderer for SqliteFlavour {
for index in tables.next.indexes().filter(|idx| !idx.is_primary_key()) {
result.push(self.render_create_index(index));
}

// Collect foreign key checks for any renamed tables.
// These must be executed immediately before `PRAGMA foreign_keys=ON`.
foreign_key_checks.push(format!(
r#"PRAGMA foreign_key_check("{new_name}")"#,
new_name = tables.next.name()
));
}

result.push("PRAGMA foreign_key_check".to_string());
result.extend(foreign_key_checks);

result.push("PRAGMA foreign_keys=ON".to_string());

result
Expand Down

0 comments on commit 133a47f

Please sign in to comment.