Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: dump database header only when we're dumping schema for multiple databases (#768)

Danny Xua249df7669e4
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 38a62f9d5d..fb9e6d15c6 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -729,13 +729,13 @@ func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, s
}
for _, dbName := range dumpableDbNames {
- // Database header.
- header := fmt.Sprintf(databaseHeaderFmt, dbName)
- if _, err := io.WriteString(out, header); err != nil {
- return err
- }
// Include "USE DATABASE xxx" if dumping multiple databases.
if len(dumpableDbNames) > 1 {
+ // Database header.
+ header := fmt.Sprintf(databaseHeaderFmt, dbName)
+ if _, err := io.WriteString(out, header); err != nil {
+ return err
+ }
dbStmt, err := getDatabaseStmt(txn, dbName)
if err != nil {
return fmt.Errorf("failed to get database %q: %s", dbName, err)
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 0b20f675dc..7b23649bbb 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -951,13 +951,13 @@ func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, s
}
for _, dbName := range dumpableDbNames {
- // Database header.
- header := fmt.Sprintf(databaseHeaderFmt, dbName)
- if _, err := io.WriteString(out, header); err != nil {
- return err
- }
// Include "USE DATABASE xxx" if dumping multiple databases.
if len(dumpableDbNames) > 1 {
+ // Database header.
+ header := fmt.Sprintf(databaseHeaderFmt, dbName)
+ if _, err := io.WriteString(out, header); err != nil {
+ return err
+ }
dbStmt, err := getDatabaseStmt(txn, dbName)
if err != nil {
return fmt.Errorf("failed to get database %q: %s", dbName, err)
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index 7f032e8c83..a3b01794ce 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -809,13 +809,13 @@ func (driver *Driver) dumpOneDatabase(ctx context.Context, database string, out
}
defer txn.Rollback()
- // Database header.
- header := fmt.Sprintf(databaseHeaderFmt, database)
- if _, err := io.WriteString(out, header); err != nil {
- return err
- }
// Database statement.
if includeUseDatabase {
+ // Database header.
+ header := fmt.Sprintf(databaseHeaderFmt, database)
+ if _, err := io.WriteString(out, header); err != nil {
+ return err
+ }
// Use database statement.
useStmt := fmt.Sprintf(useDatabaseFmt, database)
if _, err := io.WriteString(out, useStmt); err != nil {
diff --git a/plugin/db/snowflake/snowflake.go b/plugin/db/snowflake/snowflake.go
index 18cef39d99..4dabbbda5e 100644
--- a/plugin/db/snowflake/snowflake.go
+++ b/plugin/db/snowflake/snowflake.go
@@ -896,10 +896,12 @@ func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, s
// dumpOneDatabase will dump the database DDL schema for a database.
// Note: this operation is not supported on shared databases, e.g. SNOWFLAKE_SAMPLE_DATA.
func dumpOneDatabase(ctx context.Context, txn *sql.Tx, database string, out io.Writer, schemaOnly bool, dumpSingleDatabase bool) error {
- // Database header.
- header := fmt.Sprintf(databaseHeaderFmt, database)
- if _, err := io.WriteString(out, header); err != nil {
- return err
+ if !dumpSingleDatabase {
+ // Database header.
+ header := fmt.Sprintf(databaseHeaderFmt, database)
+ if _, err := io.WriteString(out, header); err != nil {
+ return err
+ }
}
query := fmt.Sprintf(`SELECT GET_DDL('DATABASE', '%s', true)`, database)