fix(sql-schema): omit source sequence default in target DDL
This commit is contained in:
1 parent
40bfe02074
commit
d6c803b17b
2 files changed
+58
-1
No files matched your search
@@ -435,7 +435,14 @@ pub fn apply_auto_inc_to_column_def(
|
|||||||
AutoIncColumnBuild::AppendSuffix { suffix, skip_default: false, postgres_sequence: false }
|
AutoIncColumnBuild::AppendSuffix { suffix, skip_default: false, postgres_sequence: false }
|
||||||
}
|
}
|
||||||
crate::sql_dialect::ddl_profile::AutoIncSyntax::PostgresSequence if col.is_primary_key && is_integer_like => {
|
crate::sql_dialect::ddl_profile::AutoIncSyntax::PostgresSequence if col.is_primary_key && is_integer_like => {
|
||||||
AutoIncColumnBuild::AppendSuffix { suffix: "", skip_default: false, postgres_sequence: true }
|
// The caller emits `ALTER TABLE ... SET DEFAULT nextval('<new>_<col>_seq')`
|
||||||
|
// right after creating that sequence (see `generate_create_table_sql`), so
|
||||||
|
// the inline default here must be skipped. `col.column_default` is the raw
|
||||||
|
// value read from the *source* column — typically `nextval('<old>_id_seq'
|
||||||
|
// ::regclass)` pointing at the source database's own sequence — and inlining
|
||||||
|
// it fails the CREATE TABLE outright on any target that doesn't already have
|
||||||
|
// that exact sequence name (#10086: "relation ... does not exist").
|
||||||
|
AutoIncColumnBuild::AppendSuffix { suffix: "", skip_default: true, postgres_sequence: true }
|
||||||
}
|
}
|
||||||
_ => AutoIncColumnBuild::Normal { skip_default: false },
|
_ => AutoIncColumnBuild::Normal { skip_default: false },
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7139,6 +7139,56 @@ mod tests {
|
|||||||
assert!(sql.contains("INT"), "integer→INT: {sql}");
|
assert!(sql.contains("INT"), "integer→INT: {sql}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn postgres_create_table_serial_pk_omits_source_sequence_default() {
|
||||||
|
// #10086: on a Postgres-family target (Kingbase in the report), the source
|
||||||
|
// column's raw `column_default` is a live `nextval('<old>_id_seq'::regclass)`
|
||||||
|
// cast pointing at the *source* database's sequence. Inlining that into
|
||||||
|
// `CREATE TABLE ... DEFAULT nextval(...)` fails at parse time on the target
|
||||||
|
// because that sequence doesn't exist there yet ("relation ... does not
|
||||||
|
// exist") — before the table is even created. The correct default is the
|
||||||
|
// `ALTER TABLE ... SET DEFAULT nextval('<name>_<col>_seq')` this function
|
||||||
|
// already emits further down, against the *new* sequence it just created.
|
||||||
|
let columns = vec![ColumnDiff {
|
||||||
|
diff_type: "added".into(),
|
||||||
|
name: "id".into(),
|
||||||
|
source: Some(ColumnInfo {
|
||||||
|
name: "id".into(),
|
||||||
|
data_type: "bigint".into(),
|
||||||
|
is_nullable: false,
|
||||||
|
is_primary_key: true,
|
||||||
|
column_default: Some("nextval('collection_abnormal_record_id_seq'::regclass)".into()),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
target: None,
|
||||||
|
changes: vec![],
|
||||||
|
add_position: None,
|
||||||
|
}];
|
||||||
|
let (sql, missing) = generate_create_table_sql(
|
||||||
|
"collection_abnormal_record",
|
||||||
|
&columns,
|
||||||
|
&[],
|
||||||
|
&[],
|
||||||
|
None,
|
||||||
|
DatabaseType::Postgres,
|
||||||
|
None,
|
||||||
|
Some(DialectKind::Postgres),
|
||||||
|
&[],
|
||||||
|
&[],
|
||||||
|
);
|
||||||
|
assert!(missing.is_empty(), "{missing:?}");
|
||||||
|
let create_table = sql.split("CREATE SEQUENCE").next().unwrap();
|
||||||
|
assert!(
|
||||||
|
!create_table.contains("nextval"),
|
||||||
|
"CREATE TABLE must not reference the source's sequence before the new one exists: {sql}"
|
||||||
|
);
|
||||||
|
assert!(sql.contains("CREATE SEQUENCE IF NOT EXISTS"), "new sequence: {sql}");
|
||||||
|
assert!(
|
||||||
|
sql.contains("ALTER TABLE \"collection_abnormal_record\" ALTER COLUMN \"id\" SET DEFAULT nextval("),
|
||||||
|
"default wired up after the sequence exists: {sql}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sync_to_sqlite_keeps_plain_integer_pk_without_autoincrement() {
|
fn sync_to_sqlite_keeps_plain_integer_pk_without_autoincrement() {
|
||||||
let columns = vec![ColumnDiff {
|
let columns = vec![ColumnDiff {
|
||||||
|
|||||||
Reference in new issue
Block a user