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
@@ -574,6 +574,14 @@ JOIN %s.%s_attribute a ON a.attrelid = t.oid AND a.attnum = pos.attnum
|
|||||||
WHERE n.nspname = %s AND t.relname = %s ORDER BY i.relname, pos.n`, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, quoteLiteral(schema), quoteLiteral(table))
|
WHERE n.nspname = %s AND t.relname = %s ORDER BY i.relname, pos.n`, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, quoteLiteral(schema), quoteLiteral(table))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func kingbaseCatalogFunction(catalog, sysFunction, postgresFunction string) string {
|
||||||
|
// Kingbase compatibility modes expose different catalog-qualified deparser names.
|
||||||
|
if catalog == "pg_catalog" {
|
||||||
|
return "pg_catalog." + postgresFunction
|
||||||
|
}
|
||||||
|
return "sys_catalog." + sysFunction
|
||||||
|
}
|
||||||
|
|
||||||
func (s *server) listForeignKeys(schema, table string) ([]foreignKeyInfo, error) {
|
func (s *server) listForeignKeys(schema, table string) ([]foreignKeyInfo, error) {
|
||||||
effective, err := s.effectiveSchema(schema)
|
effective, err := s.effectiveSchema(schema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -673,7 +681,16 @@ func (s *server) getTableDDL(schema, table string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
tableComment, _ := s.getTableComment(effective, table)
|
tableComment, _ := s.getTableComment(effective, table)
|
||||||
return renderTableDDL(effective, table, columns, tableComment), nil
|
ddl := renderTableDDL(effective, table, columns, tableComment)
|
||||||
|
ddl, err = s.appendTableIndexDDL(effective, table, ddl)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
ddl, err = s.appendTableTriggerDDL(effective, table, ddl)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return ddl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTableDDL(schema, table string, columns []columnInfo, tableComment *string) string {
|
func renderTableDDL(schema, table string, columns []columnInfo, tableComment *string) string {
|
||||||
@@ -702,6 +719,120 @@ func renderTableDDL(schema, table string, columns []columnInfo, tableComment *st
|
|||||||
return ddl
|
return ddl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *server) appendTableIndexDDL(schema, table, ddl string) (string, error) {
|
||||||
|
definitions, err := s.listIndexDefinitions(schema, table)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return appendDDLStatements(ddl, definitions), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) appendTableTriggerDDL(schema, table, ddl string) (string, error) {
|
||||||
|
definitions, err := s.listTriggerDefinitions(schema, table)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return appendDDLStatements(ddl, definitions), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) listIndexDefinitions(schema, table string) ([]string, error) {
|
||||||
|
effective, err := s.effectiveSchema(schema)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
catalog, prefix := "sys_catalog", "sys"
|
||||||
|
if s.mode.postgresCatalog {
|
||||||
|
catalog, prefix = "pg_catalog", "pg"
|
||||||
|
}
|
||||||
|
indexDefinitionFunction := kingbaseCatalogFunction(catalog, "sys_get_indexdef", "pg_get_indexdef")
|
||||||
|
query := fmt.Sprintf(`SELECT i.relname, %s(ix.indexrelid, 0, true), obj_description(i.oid)
|
||||||
|
FROM %s.%s_index ix JOIN %s.%s_class t ON t.oid = ix.indrelid
|
||||||
|
JOIN %s.%s_class i ON i.oid = ix.indexrelid JOIN %s.%s_namespace n ON n.oid = t.relnamespace
|
||||||
|
WHERE n.nspname = %s AND t.relname = %s AND NOT ix.indisprimary ORDER BY i.relname`, indexDefinitionFunction, catalog, prefix, catalog, prefix, catalog, prefix, catalog, prefix, quoteLiteral(effective), quoteLiteral(table))
|
||||||
|
rows, err := s.metadataQuery(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
result := []string{}
|
||||||
|
for rows.Next() {
|
||||||
|
var name string
|
||||||
|
var definition string
|
||||||
|
var comment sql.NullString
|
||||||
|
if err := rows.Scan(&name, &definition, &comment); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(definition) != "" {
|
||||||
|
result = append(result, definition)
|
||||||
|
}
|
||||||
|
if comment.Valid && strings.TrimSpace(comment.String) != "" {
|
||||||
|
result = append(result, "COMMENT ON INDEX "+quoteIdentifier(effective)+"."+quoteIdentifier(name)+" IS "+quoteLiteral(comment.String))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) listTriggerDefinitions(schema, table string) ([]string, error) {
|
||||||
|
effective, err := s.effectiveSchema(schema)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
catalog, prefix := "sys_catalog", "sys"
|
||||||
|
if s.mode.postgresCatalog {
|
||||||
|
catalog, prefix = "pg_catalog", "pg"
|
||||||
|
}
|
||||||
|
triggerDefinitionFunction := kingbaseCatalogFunction(catalog, "sys_get_triggerdef", "pg_get_triggerdef")
|
||||||
|
query := fmt.Sprintf(`SELECT %s(tg.oid, true)
|
||||||
|
FROM %s.%s_trigger tg JOIN %s.%s_class c ON c.oid = tg.tgrelid JOIN %s.%s_namespace n ON n.oid = c.relnamespace
|
||||||
|
WHERE n.nspname = %s AND c.relname = %s AND NOT tg.tgisinternal ORDER BY tg.tgname`, triggerDefinitionFunction, catalog, prefix, catalog, prefix, catalog, prefix, quoteLiteral(effective), quoteLiteral(table))
|
||||||
|
rows, err := s.metadataQuery(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
result := []string{}
|
||||||
|
for rows.Next() {
|
||||||
|
var definition string
|
||||||
|
if err := rows.Scan(&definition); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(definition) != "" {
|
||||||
|
result = append(result, definition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendDDLStatements(ddl string, statements []string) string {
|
||||||
|
for _, statement := range statements {
|
||||||
|
ddl = appendDDLStatement(ddl, statement)
|
||||||
|
}
|
||||||
|
return ddl
|
||||||
|
}
|
||||||
|
|
||||||
|
func appendDDLStatement(ddl, statement string) string {
|
||||||
|
ddl = strings.TrimRight(ddl, "\r\n\t ")
|
||||||
|
statement = ensureStatementTerminator(statement)
|
||||||
|
if statement == "" {
|
||||||
|
return ddl
|
||||||
|
}
|
||||||
|
if ddl == "" {
|
||||||
|
return statement
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(ddl, ";") {
|
||||||
|
ddl += ";"
|
||||||
|
}
|
||||||
|
return ddl + "\n\n" + statement
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureStatementTerminator(statement string) string {
|
||||||
|
trimmed := strings.TrimSpace(statement)
|
||||||
|
if trimmed == "" || strings.HasSuffix(trimmed, ";") {
|
||||||
|
return trimmed
|
||||||
|
}
|
||||||
|
return trimmed + ";"
|
||||||
|
}
|
||||||
|
|
||||||
func columnDDLDefinition(column columnInfo) string {
|
func columnDDLDefinition(column columnInfo) string {
|
||||||
definition := quoteIdentifier(column.Name) + " " + column.DataType
|
definition := quoteIdentifier(column.Name) + " " + column.DataType
|
||||||
if column.Extra != nil && *column.Extra != "" {
|
if column.Extra != nil && *column.Extra != "" {
|
||||||
|
|||||||
@@ -144,6 +144,22 @@ func (connection *fallbackConn) QueryContext(_ context.Context, query string, _
|
|||||||
rows: [][]driver.Value{{"orders table"}},
|
rows: [][]driver.Value{{"orders table"}},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
if strings.Contains(query, "SELECT i.relname, sys_catalog.sys_get_indexdef(") || strings.Contains(query, "SELECT i.relname, pg_catalog.pg_get_indexdef(") {
|
||||||
|
return &valueRows{
|
||||||
|
columns: []string{"index_name", "index_definition", "index_comment"},
|
||||||
|
rows: [][]driver.Value{
|
||||||
|
{"orders_id_idx", `CREATE INDEX orders_id_idx ON public.orders USING btree (id)`, "lookup index"},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
if strings.Contains(query, "SELECT sys_catalog.sys_get_triggerdef(tg.oid, true)") || strings.Contains(query, "SELECT pg_catalog.pg_get_triggerdef(tg.oid, true)") {
|
||||||
|
return &valueRows{
|
||||||
|
columns: []string{"trigger_definition"},
|
||||||
|
rows: [][]driver.Value{
|
||||||
|
{`CREATE TRIGGER orders_audit BEFORE INSERT ON public.orders FOR EACH ROW EXECUTE FUNCTION audit_orders()`},
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
if strings.Contains(query, "FROM information_schema.columns c") {
|
if strings.Contains(query, "FROM information_schema.columns c") {
|
||||||
return &valueRows{
|
return &valueRows{
|
||||||
columns: []string{"column_name", "data_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},
|
columns: []string{"column_name", "data_type", "is_nullable", "column_default", "column_comment", "numeric_precision", "numeric_scale", "character_maximum_length"},
|
||||||
@@ -334,6 +350,48 @@ func TestKingbaseListIndexesQuerySupportsSQLServerMode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKingbaseCatalogFunctionsFollowMetadataMode(t *testing.T) {
|
||||||
|
registerExpressionFallbackDriver.Do(func() { sql.Register("kingbase-expression-fallback-test", fallbackDriver{}) })
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
postgresCatalog bool
|
||||||
|
expectedIndex string
|
||||||
|
expectedTrigger string
|
||||||
|
}{
|
||||||
|
{name: "sys catalog", expectedIndex: "sys_catalog.sys_get_indexdef", expectedTrigger: "sys_catalog.sys_get_triggerdef"},
|
||||||
|
{name: "postgres catalog", postgresCatalog: true, expectedIndex: "pg_catalog.pg_get_indexdef", expectedTrigger: "pg_catalog.pg_get_triggerdef"},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
state := &fallbackDriverState{}
|
||||||
|
expressionFallbackState.Store(state)
|
||||||
|
db, err := sql.Open("kingbase-expression-fallback-test", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
db.SetMaxOpenConns(1)
|
||||||
|
t.Cleanup(func() { _ = db.Close() })
|
||||||
|
server := newServer()
|
||||||
|
server.db = db
|
||||||
|
server.mode.postgresCatalog = test.postgresCatalog
|
||||||
|
|
||||||
|
if _, err := server.listIndexDefinitions("public", "orders"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := server.listTriggerDefinitions("public", "orders"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
state.mu.Lock()
|
||||||
|
queries := append([]string(nil), state.queries...)
|
||||||
|
state.mu.Unlock()
|
||||||
|
if len(queries) != 2 || !strings.Contains(queries[0], test.expectedIndex+"(") || !strings.Contains(queries[1], test.expectedTrigger+"(") {
|
||||||
|
t.Fatalf("catalog functions do not match metadata mode: %v", queries)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMySQLCompatSchemaQueryKeepsUserSchemasWithSystemLikeNames(t *testing.T) {
|
func TestMySQLCompatSchemaQueryKeepsUserSchemasWithSystemLikeNames(t *testing.T) {
|
||||||
query := kingbaseMySQLCompatListSchemasSQL
|
query := kingbaseMySQLCompatListSchemasSQL
|
||||||
for _, prefix := range []string{"SYS", "XLOG"} {
|
for _, prefix := range []string{"SYS", "XLOG"} {
|
||||||
@@ -540,7 +598,7 @@ func TestKingbaseIdentityClausesAreExposedAndRendered(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTableDDLIncludesCatalogIdentityClause(t *testing.T) {
|
func TestTableDDLIncludesIdentityIndexesTriggersAndComments(t *testing.T) {
|
||||||
registerExpressionFallbackDriver.Do(func() { sql.Register("kingbase-expression-fallback-test", fallbackDriver{}) })
|
registerExpressionFallbackDriver.Do(func() { sql.Register("kingbase-expression-fallback-test", fallbackDriver{}) })
|
||||||
expressionFallbackState.Store(&fallbackDriverState{})
|
expressionFallbackState.Store(&fallbackDriverState{})
|
||||||
db, err := sql.Open("kingbase-expression-fallback-test", "")
|
db, err := sql.Open("kingbase-expression-fallback-test", "")
|
||||||
@@ -559,8 +617,15 @@ func TestTableDDLIncludesCatalogIdentityClause(t *testing.T) {
|
|||||||
if !strings.Contains(ddl, `"id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL`) {
|
if !strings.Contains(ddl, `"id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL`) {
|
||||||
t.Fatalf("identity clause missing from table DDL: %s", ddl)
|
t.Fatalf("identity clause missing from table DDL: %s", ddl)
|
||||||
}
|
}
|
||||||
if !strings.Contains(ddl, `COMMENT ON TABLE "public"."orders" IS 'orders table';`) {
|
for _, expected := range []string{
|
||||||
t.Fatalf("table comment missing from table DDL: %s", ddl)
|
`COMMENT ON TABLE "public"."orders" IS 'orders table';`,
|
||||||
|
`CREATE INDEX orders_id_idx ON public.orders USING btree (id);`,
|
||||||
|
`COMMENT ON INDEX "public"."orders_id_idx" IS 'lookup index';`,
|
||||||
|
`CREATE TRIGGER orders_audit BEFORE INSERT ON public.orders FOR EACH ROW EXECUTE FUNCTION audit_orders();`,
|
||||||
|
} {
|
||||||
|
if !strings.Contains(ddl, expected) {
|
||||||
|
t.Fatalf("table DDL missing %q:\n%s", expected, ddl)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -607,6 +672,14 @@ func TestColumnDDLDefinitionPreservesCompatibilityExtras(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAppendDDLStatementEnsuresSingleTerminator(t *testing.T) {
|
||||||
|
got := appendDDLStatement("CREATE TABLE \"public\".\"orders\" (\n \"id\" integer\n)\n", "CREATE INDEX orders_id_idx ON public.orders (id)")
|
||||||
|
want := "CREATE TABLE \"public\".\"orders\" (\n \"id\" integer\n);\n\nCREATE INDEX orders_id_idx ON public.orders (id);"
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("unexpected appended DDL:\ngot: %q\nwant: %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMySQLCompatColumnsUsePostgresColumnComments(t *testing.T) {
|
func TestMySQLCompatColumnsUsePostgresColumnComments(t *testing.T) {
|
||||||
registerExpressionFallbackDriver.Do(func() { sql.Register("kingbase-expression-fallback-test", fallbackDriver{}) })
|
registerExpressionFallbackDriver.Do(func() { sql.Register("kingbase-expression-fallback-test", fallbackDriver{}) })
|
||||||
state := &fallbackDriverState{}
|
state := &fallbackDriverState{}
|
||||||
|
|||||||
@@ -3401,6 +3401,24 @@ pub async fn list_triggers(pool: &Pool, schema: &str, table: &str) -> Result<Vec
|
|||||||
.collect())
|
.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 {
|
fn postgres_functions_sql(has_proc_prokind: bool) -> &'static str {
|
||||||
if has_proc_prokind {
|
if has_proc_prokind {
|
||||||
return "SELECT p.proname, \
|
return "SELECT p.proname, \
|
||||||
@@ -5109,6 +5127,14 @@ mod tests {
|
|||||||
assert!(sql.contains("attname = 'prosp'"));
|
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]
|
#[test]
|
||||||
fn transaction_recovery_statement_detection_matches_common_postgres_commands() {
|
fn transaction_recovery_statement_detection_matches_common_postgres_commands() {
|
||||||
assert!(is_transaction_recovery_statement("ROLLBACK"));
|
assert!(is_transaction_recovery_statement("ROLLBACK"));
|
||||||
|
|||||||
@@ -6979,6 +6979,30 @@ mod ddl_tests {
|
|||||||
assert_eq!(ddl.matches("CONSTRAINT \"aaa_1\" FOREIGN KEY").count(), 1);
|
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]
|
#[test]
|
||||||
fn sqlserver_table_ddl_includes_column_comments() {
|
fn sqlserver_table_ddl_includes_column_comments() {
|
||||||
let mut display_name = column("display]name", "nvarchar(100)");
|
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]
|
#[test]
|
||||||
fn mysql_display_ddl_gets_statement_terminator() {
|
fn mysql_display_ddl_gets_statement_terminator() {
|
||||||
let ddl = "CREATE TABLE `users` (\n `id` int NOT NULL\n) ENGINE=InnoDB";
|
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())?
|
.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> {
|
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 {
|
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))
|
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> {
|
fn append_postgres_trigger_definitions(mut ddl: String, trigger_definitions: &[String]) -> String {
|
||||||
let (columns, indexes, fkeys, table_comment, partition_key) = tokio::try_join!(
|
for definition in
|
||||||
db::postgres::get_columns(pool, schema, table),
|
trigger_definitions.iter().map(|definition| definition.trim()).filter(|definition| !definition.is_empty())
|
||||||
db::postgres::list_indexes(pool, schema, table),
|
{
|
||||||
db::postgres::list_foreign_keys(pool, schema, table),
|
ddl = ddl.trim_end().to_string();
|
||||||
async { db::postgres::get_table_comment(pool, schema, table).await },
|
if !ddl.ends_with(';') {
|
||||||
db::postgres::get_table_partition_key(pool, schema, table),
|
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(
|
fn append_opengauss_trigger_definitions(mut ddl: String, trigger_definitions: &[String]) -> String {
|
||||||
schema,
|
for definition in
|
||||||
table,
|
trigger_definitions.iter().map(|definition| definition.trim()).filter(|definition| !definition.is_empty())
|
||||||
&columns,
|
{
|
||||||
&indexes,
|
ddl = ddl.trim_end().to_string();
|
||||||
&fkeys,
|
if !ddl.ends_with(';') {
|
||||||
table_comment.as_deref(),
|
ddl.push(';');
|
||||||
partition_key.as_deref(),
|
}
|
||||||
))
|
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> {
|
pub async fn cloudberry_ddl(pool: &deadpool_postgres::Pool, schema: &str, table: &str) -> Result<String, String> {
|
||||||
|
|||||||
Reference in new issue
Block a user