fix(transfer): preserve constraints after deferred foreign keys
This commit is contained in:
1 parent
dd11e42748
commit
7157b04d87
1 file changed
+42
-11
@@ -5003,11 +5003,10 @@ fn transfer_ddl_statements(sql: &str, db_type: &DatabaseType) -> Vec<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Strips inline `CONSTRAINT ... FOREIGN KEY ... REFERENCES ...` lines from a
|
/// Strips inline `CONSTRAINT ... FOREIGN KEY ... REFERENCES ...` lines from a
|
||||||
/// `CREATE TABLE` statement, fixing up the now-dangling trailing comma on the
|
/// `CREATE TABLE` statement, fixing up a trailing comma only when removing the
|
||||||
/// preceding line. Dialect-agnostic: relies only on the ` FOREIGN KEY ` clause
|
/// foreign key leaves one immediately before the table's closing parenthesis.
|
||||||
/// text, which both Postgres and MySQL-family `SHOW CREATE TABLE` output share,
|
/// Dialect-agnostic: relies only on the definition-line shape shared by Postgres
|
||||||
/// and on foreign key constraints always being the last items before the closing
|
/// and MySQL-family DDL dumps.
|
||||||
/// paren (true for both dialects' DDL dumps).
|
|
||||||
///
|
///
|
||||||
/// Only genuine constraint definition lines match (`CONSTRAINT <name> FOREIGN
|
/// Only genuine constraint definition lines match (`CONSTRAINT <name> FOREIGN
|
||||||
/// KEY (` / bare `FOREIGN KEY (`); a column line whose COMMENT or DEFAULT text
|
/// KEY (` / bare `FOREIGN KEY (`); a column line whose COMMENT or DEFAULT text
|
||||||
@@ -5018,18 +5017,26 @@ fn strip_inline_foreign_key_constraint_lines(statement: &str) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut lines: Vec<String> = Vec::new();
|
let mut lines: Vec<String> = Vec::new();
|
||||||
|
let mut removed_foreign_key = false;
|
||||||
for line in statement.lines() {
|
for line in statement.lines() {
|
||||||
if INLINE_FOREIGN_KEY_CONSTRAINT_LINE_RE.is_match(line) {
|
if INLINE_FOREIGN_KEY_CONSTRAINT_LINE_RE.is_match(line) {
|
||||||
if let Some(previous) = lines.last_mut() {
|
removed_foreign_key = true;
|
||||||
let trimmed_len = previous.trim_end_matches(char::is_whitespace).len();
|
|
||||||
if previous[..trimmed_len].ends_with(',') {
|
|
||||||
previous.truncate(trimmed_len - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
lines.push(line.to_string());
|
lines.push(line.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if removed_foreign_key {
|
||||||
|
if let Some(closing_index) = lines.iter().rposition(|line| line.trim_start().starts_with(')')) {
|
||||||
|
if let Some(previous) = lines[..closing_index].iter_mut().rfind(|line| !line.trim().is_empty()) {
|
||||||
|
let trimmed_len = previous.trim_end_matches(char::is_whitespace).len();
|
||||||
|
if previous[..trimmed_len].ends_with(',') {
|
||||||
|
previous.remove(trimmed_len - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
lines.join("\n")
|
lines.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10581,6 +10588,30 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn postgres_transfer_ddl_preserves_constraint_after_inline_foreign_key() {
|
||||||
|
let ddl = "CREATE TABLE \"source_7931\".\"dbx_child\" (\n \"id\" integer NOT NULL,\n \"parent_id\" integer NOT NULL,\n \"status\" integer NOT NULL,\n CONSTRAINT \"dbx_child_parent_fk\" FOREIGN KEY (\"parent_id\") REFERENCES \"source_7931\".\"dbx_parent\"(\"id\"),\n CONSTRAINT \"dbx_child_status_check\" CHECK (status >= 0)\n);";
|
||||||
|
|
||||||
|
let statements = transfer_ddl_statements(ddl, &DatabaseType::Postgres);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
statements,
|
||||||
|
vec!["CREATE TABLE \"source_7931\".\"dbx_child\" (\n \"id\" integer NOT NULL,\n \"parent_id\" integer NOT NULL,\n \"status\" integer NOT NULL,\n CONSTRAINT \"dbx_child_status_check\" CHECK (status >= 0)\n)".to_string()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn postgres_transfer_ddl_removes_multiple_inline_foreign_keys_without_damaging_retained_items() {
|
||||||
|
let ddl = "CREATE TABLE \"public\".\"assignments\" (\n \"id\" integer NOT NULL,\n \"owner_id\" integer NOT NULL,\n \"reviewer_id\" integer NOT NULL,\n CONSTRAINT \"assignments_owner_fk\" FOREIGN KEY (\"owner_id\") REFERENCES \"users\"(\"id\"),\n CONSTRAINT \"assignments_owner_check\" CHECK (owner_id > 0),\n CONSTRAINT \"assignments_reviewer_fk\" FOREIGN KEY (\"reviewer_id\") REFERENCES \"users\"(\"id\"),\n CONSTRAINT \"assignments_owner_reviewer_unique\" UNIQUE (\"owner_id\", \"reviewer_id\")\n);";
|
||||||
|
|
||||||
|
let statements = transfer_ddl_statements(ddl, &DatabaseType::Postgres);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
statements,
|
||||||
|
vec!["CREATE TABLE \"public\".\"assignments\" (\n \"id\" integer NOT NULL,\n \"owner_id\" integer NOT NULL,\n \"reviewer_id\" integer NOT NULL,\n CONSTRAINT \"assignments_owner_check\" CHECK (owner_id > 0),\n CONSTRAINT \"assignments_owner_reviewer_unique\" UNIQUE (\"owner_id\", \"reviewer_id\")\n)".to_string()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn transfer_create_table_result_treats_existing_table_as_preexisting() {
|
fn transfer_create_table_result_treats_existing_table_as_preexisting() {
|
||||||
assert!(!transfer_create_table_created(
|
assert!(!transfer_create_table_created(
|
||||||
|
|||||||
Reference in new issue
Block a user