feat(schema): include indexes and triggers in table DDL
This commit is contained in:
1 parent
e2ecb0ccff
commit
736ba75c9e
4 files changed
+333
-22
No files matched your search
@@ -3401,6 +3401,24 @@ pub async fn list_triggers(pool: &Pool, schema: &str, table: &str) -> Result<Vec
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub async fn list_trigger_definitions(pool: &Pool, schema: &str, table: &str) -> Result<Vec<String>, String> {
|
||||
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
|
||||
let rows = postgres_query_cached(&client, postgres_trigger_definitions_sql(), &[&schema, &table])
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Ok(rows.iter().map(|row| pg_row_try_string(row, 0)).filter(|definition| !definition.trim().is_empty()).collect())
|
||||
}
|
||||
|
||||
fn postgres_trigger_definitions_sql() -> &'static str {
|
||||
"SELECT pg_catalog.pg_get_triggerdef(t.oid, true) AS trigger_definition \
|
||||
FROM pg_catalog.pg_trigger t \
|
||||
JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid \
|
||||
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
|
||||
WHERE n.nspname = $1 AND c.relname = $2 AND NOT t.tgisinternal \
|
||||
ORDER BY t.tgname, t.oid"
|
||||
}
|
||||
|
||||
fn postgres_functions_sql(has_proc_prokind: bool) -> &'static str {
|
||||
if has_proc_prokind {
|
||||
return "SELECT p.proname, \
|
||||
@@ -5109,6 +5127,14 @@ mod tests {
|
||||
assert!(sql.contains("attname = 'prosp'"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_trigger_definitions_sql_excludes_internal_triggers() {
|
||||
let sql = postgres_trigger_definitions_sql();
|
||||
assert!(sql.contains("pg_catalog.pg_get_triggerdef(t.oid, true) AS trigger_definition"));
|
||||
assert!(sql.contains("NOT t.tgisinternal"));
|
||||
assert!(sql.contains("ORDER BY t.tgname, t.oid"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transaction_recovery_statement_detection_matches_common_postgres_commands() {
|
||||
assert!(is_transaction_recovery_statement("ROLLBACK"));
|
||||
|
||||
@@ -6979,6 +6979,30 @@ mod ddl_tests {
|
||||
assert_eq!(ddl.matches("CONSTRAINT \"aaa_1\" FOREIGN KEY").count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_table_ddl_appends_trigger_definitions() {
|
||||
let ddl = append_postgres_trigger_definitions(
|
||||
render_postgres_table_ddl("public", "users", &[column("id", "integer")], &[], &[], None),
|
||||
&[r#"CREATE TRIGGER users_set_updated_at BEFORE UPDATE ON "public"."users" FOR EACH ROW EXECUTE FUNCTION set_updated_at()"#
|
||||
.to_string()],
|
||||
);
|
||||
|
||||
assert!(ddl.contains("CREATE TABLE \"public\".\"users\""));
|
||||
assert!(ddl.contains(
|
||||
"\n\nCREATE TRIGGER users_set_updated_at BEFORE UPDATE ON \"public\".\"users\" FOR EACH ROW EXECUTE FUNCTION set_updated_at();"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_table_ddl_does_not_duplicate_trigger_statement_terminators() {
|
||||
let ddl = append_postgres_trigger_definitions(
|
||||
render_postgres_table_ddl("public", "users", &[column("id", "integer")], &[], &[], None),
|
||||
&[r#"CREATE TRIGGER users_audit AFTER INSERT ON "public"."users" FOR EACH ROW EXECUTE FUNCTION audit_user();"#.to_string()],
|
||||
);
|
||||
|
||||
assert!(!ddl.contains("audit_user();;"), "ddl: {ddl}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_table_ddl_includes_column_comments() {
|
||||
let mut display_name = column("display]name", "nvarchar(100)");
|
||||
@@ -7042,6 +7066,20 @@ mod ddl_tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opengauss_table_ddl_appends_trigger_definitions() {
|
||||
let ddl = append_opengauss_trigger_definitions(
|
||||
"CREATE TABLE \"public\".\"users\" (\n \"id\" integer\n);".to_string(),
|
||||
&[r#"CREATE TRIGGER users_bi BEFORE INSERT ON "public"."users" FOR EACH ROW EXECUTE PROCEDURE fill_created_at()"#
|
||||
.to_string()],
|
||||
);
|
||||
|
||||
assert!(ddl.contains("CREATE TABLE \"public\".\"users\""));
|
||||
assert!(ddl.contains(
|
||||
"\n\nCREATE TRIGGER users_bi BEFORE INSERT ON \"public\".\"users\" FOR EACH ROW EXECUTE PROCEDURE fill_created_at();"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_display_ddl_gets_statement_terminator() {
|
||||
let ddl = "CREATE TABLE `users` (\n `id` int NOT NULL\n) ENGINE=InnoDB";
|
||||
@@ -7109,8 +7147,37 @@ pub async fn sqlite_ddl(pool: &db::sqlite::SqliteHandle, schema: &str, table: &s
|
||||
.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
pub async fn pg_ddl(pool: &deadpool_postgres::Pool, schema: &str, table: &str) -> Result<String, String> {
|
||||
let (columns, indexes, fkeys, table_comment, partition_key, trigger_definitions) = tokio::try_join!(
|
||||
db::postgres::get_columns(pool, schema, table),
|
||||
db::postgres::list_indexes(pool, schema, table),
|
||||
db::postgres::list_foreign_keys(pool, schema, table),
|
||||
async { db::postgres::get_table_comment(pool, schema, table).await },
|
||||
db::postgres::get_table_partition_key(pool, schema, table),
|
||||
db::postgres::list_trigger_definitions(pool, schema, table),
|
||||
)?;
|
||||
|
||||
Ok(append_postgres_trigger_definitions(
|
||||
render_postgres_table_ddl_with_partition_key(
|
||||
schema,
|
||||
table,
|
||||
&columns,
|
||||
&indexes,
|
||||
&fkeys,
|
||||
table_comment.as_deref(),
|
||||
partition_key.as_deref(),
|
||||
),
|
||||
&trigger_definitions,
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn opengauss_table_ddl(pool: &deadpool_postgres::Pool, schema: &str, table: &str) -> Result<String, String> {
|
||||
first_string_cell(db::postgres::execute_query(pool, &opengauss_table_ddl_sql(schema, table)).await?)
|
||||
let (ddl, trigger_definitions) = tokio::try_join!(
|
||||
async { first_string_cell(db::postgres::execute_query(pool, &opengauss_table_ddl_sql(schema, table)).await?) },
|
||||
db::postgres::list_trigger_definitions(pool, schema, table),
|
||||
)?;
|
||||
|
||||
Ok(append_opengauss_trigger_definitions(ddl, &trigger_definitions))
|
||||
}
|
||||
|
||||
pub fn opengauss_table_ddl_sql(schema: &str, table: &str) -> String {
|
||||
@@ -7118,24 +7185,38 @@ pub fn opengauss_table_ddl_sql(schema: &str, table: &str) -> String {
|
||||
format!("SELECT pg_get_tabledef({})", sql_string(&qualified_name))
|
||||
}
|
||||
|
||||
pub async fn pg_ddl(pool: &deadpool_postgres::Pool, schema: &str, table: &str) -> Result<String, String> {
|
||||
let (columns, indexes, fkeys, table_comment, partition_key) = tokio::try_join!(
|
||||
db::postgres::get_columns(pool, schema, table),
|
||||
db::postgres::list_indexes(pool, schema, table),
|
||||
db::postgres::list_foreign_keys(pool, schema, table),
|
||||
async { db::postgres::get_table_comment(pool, schema, table).await },
|
||||
db::postgres::get_table_partition_key(pool, schema, table),
|
||||
)?;
|
||||
fn append_postgres_trigger_definitions(mut ddl: String, trigger_definitions: &[String]) -> String {
|
||||
for definition in
|
||||
trigger_definitions.iter().map(|definition| definition.trim()).filter(|definition| !definition.is_empty())
|
||||
{
|
||||
ddl = ddl.trim_end().to_string();
|
||||
if !ddl.ends_with(';') {
|
||||
ddl.push(';');
|
||||
}
|
||||
ddl.push_str("\n\n");
|
||||
ddl.push_str(definition);
|
||||
if !definition.ends_with(';') {
|
||||
ddl.push(';');
|
||||
}
|
||||
}
|
||||
ddl
|
||||
}
|
||||
|
||||
Ok(render_postgres_table_ddl_with_partition_key(
|
||||
schema,
|
||||
table,
|
||||
&columns,
|
||||
&indexes,
|
||||
&fkeys,
|
||||
table_comment.as_deref(),
|
||||
partition_key.as_deref(),
|
||||
))
|
||||
fn append_opengauss_trigger_definitions(mut ddl: String, trigger_definitions: &[String]) -> String {
|
||||
for definition in
|
||||
trigger_definitions.iter().map(|definition| definition.trim()).filter(|definition| !definition.is_empty())
|
||||
{
|
||||
ddl = ddl.trim_end().to_string();
|
||||
if !ddl.ends_with(';') {
|
||||
ddl.push(';');
|
||||
}
|
||||
ddl.push_str("\n\n");
|
||||
ddl.push_str(definition);
|
||||
if !definition.ends_with(';') {
|
||||
ddl.push(';');
|
||||
}
|
||||
}
|
||||
ddl
|
||||
}
|
||||
|
||||
pub async fn cloudberry_ddl(pool: &deadpool_postgres::Pool, schema: &str, table: &str) -> Result<String, String> {
|
||||
|
||||
Reference in new issue
Block a user