fix(postgres): bigint cast in sequence sync SQL
This commit is contained in:
1 parent
291890f67e
commit
d765315af4
1 file changed
+64
-4
@@ -2242,7 +2242,7 @@ fn generate_postgres_sequence_sync_sql(columns: &[db::ColumnInfo], table: &str,
|
|||||||
.map(|column| {
|
.map(|column| {
|
||||||
let quoted_column = quote_identifier(&column.name, &DatabaseType::Postgres);
|
let quoted_column = quote_identifier(&column.name, &DatabaseType::Postgres);
|
||||||
format!(
|
format!(
|
||||||
"SELECT setval(pg_get_serial_sequence({}, {}), GREATEST(COALESCE(MAX({quoted_column}), 0), 1), MAX({quoted_column}) IS NOT NULL) FROM {full_table}",
|
"SELECT setval(pg_get_serial_sequence({}, {})::regclass, GREATEST(COALESCE(MAX({quoted_column}::bigint), 0), 1), MAX({quoted_column}::bigint) IS NOT NULL) FROM {full_table}",
|
||||||
quote_string_literal(&full_table),
|
quote_string_literal(&full_table),
|
||||||
quote_string_literal(&column.name)
|
quote_string_literal(&column.name)
|
||||||
)
|
)
|
||||||
@@ -11582,8 +11582,68 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
sql,
|
sql,
|
||||||
vec![
|
vec![
|
||||||
"SELECT setval(pg_get_serial_sequence('\"public\".\"users\"', 'id'), GREATEST(COALESCE(MAX(\"id\"), 0), 1), MAX(\"id\") IS NOT NULL) FROM \"public\".\"users\"".to_string(),
|
"SELECT setval(pg_get_serial_sequence('\"public\".\"users\"', 'id')::regclass, GREATEST(COALESCE(MAX(\"id\"::bigint), 0), 1), MAX(\"id\"::bigint) IS NOT NULL) FROM \"public\".\"users\"".to_string(),
|
||||||
"SELECT setval(pg_get_serial_sequence('\"public\".\"users\"', 'identity_id'), GREATEST(COALESCE(MAX(\"identity_id\"), 0), 1), MAX(\"identity_id\") IS NOT NULL) FROM \"public\".\"users\"".to_string()
|
"SELECT setval(pg_get_serial_sequence('\"public\".\"users\"', 'identity_id')::regclass, GREATEST(COALESCE(MAX(\"identity_id\"::bigint), 0), 1), MAX(\"identity_id\"::bigint) IS NOT NULL) FROM \"public\".\"users\"".to_string()
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn postgres_sequence_sync_sql_casts_text_and_numeric_columns_to_bigint() {
|
||||||
|
let sql = generate_postgres_sequence_sync_sql(
|
||||||
|
&[
|
||||||
|
db::ColumnInfo {
|
||||||
|
name: "text_id".to_string(),
|
||||||
|
data_type: "text".to_string(),
|
||||||
|
is_nullable: false,
|
||||||
|
column_default: Some("nextval('public.dbx_text_id_seq'::regclass)::text".to_string()),
|
||||||
|
is_primary_key: true,
|
||||||
|
extra: None,
|
||||||
|
comment: None,
|
||||||
|
numeric_precision: None,
|
||||||
|
numeric_scale: None,
|
||||||
|
character_maximum_length: None,
|
||||||
|
enum_values: None,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
db::ColumnInfo {
|
||||||
|
name: "numeric_id".to_string(),
|
||||||
|
data_type: "numeric".to_string(),
|
||||||
|
is_nullable: false,
|
||||||
|
column_default: Some("nextval('public.dbx_numeric_id_seq'::regclass)".to_string()),
|
||||||
|
is_primary_key: false,
|
||||||
|
extra: None,
|
||||||
|
comment: None,
|
||||||
|
numeric_precision: None,
|
||||||
|
numeric_scale: None,
|
||||||
|
character_maximum_length: None,
|
||||||
|
enum_values: None,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
db::ColumnInfo {
|
||||||
|
name: "plain_text".to_string(),
|
||||||
|
data_type: "text".to_string(),
|
||||||
|
is_nullable: true,
|
||||||
|
column_default: None,
|
||||||
|
is_primary_key: false,
|
||||||
|
extra: None,
|
||||||
|
comment: None,
|
||||||
|
numeric_precision: None,
|
||||||
|
numeric_scale: None,
|
||||||
|
character_maximum_length: None,
|
||||||
|
enum_values: None,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"seq_test",
|
||||||
|
"public",
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
sql,
|
||||||
|
vec![
|
||||||
|
"SELECT setval(pg_get_serial_sequence('\"public\".\"seq_test\"', 'text_id')::regclass, GREATEST(COALESCE(MAX(\"text_id\"::bigint), 0), 1), MAX(\"text_id\"::bigint) IS NOT NULL) FROM \"public\".\"seq_test\"".to_string(),
|
||||||
|
"SELECT setval(pg_get_serial_sequence('\"public\".\"seq_test\"', 'numeric_id')::regclass, GREATEST(COALESCE(MAX(\"numeric_id\"::bigint), 0), 1), MAX(\"numeric_id\"::bigint) IS NOT NULL) FROM \"public\".\"seq_test\"".to_string(),
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -11698,7 +11758,7 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
sequence_sync_sql,
|
sequence_sync_sql,
|
||||||
vec![
|
vec![
|
||||||
"SELECT setval(pg_get_serial_sequence('\"archive\".\"it_quick_entry\"', 'id'), GREATEST(COALESCE(MAX(\"id\"), 0), 1), MAX(\"id\") IS NOT NULL) FROM \"archive\".\"it_quick_entry\"".to_string()
|
"SELECT setval(pg_get_serial_sequence('\"archive\".\"it_quick_entry\"', 'id')::regclass, GREATEST(COALESCE(MAX(\"id\"::bigint), 0), 1), MAX(\"id\"::bigint) IS NOT NULL) FROM \"archive\".\"it_quick_entry\"".to_string()
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user