Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Adding schema only dump option to bb CLI.
spinningbotff5a22ca68fa
diff --git a/bin/bb/cmd/dump.go b/bin/bb/cmd/dump.goindex 03fe066973..fc1c0a5d85 100644--- a/bin/bb/cmd/dump.go+++ b/bin/bb/cmd/dump.go@@ -20,6 +20,8 @@ func init() {dumpCmd.Flags().StringVar(&sslCert, "ssl-cert", "", "X509 cert in PEM format.")dumpCmd.Flags().StringVar(&sslKey, "ssl-key", "", "X509 key in PEM format.")+ dumpCmd.Flags().BoolVar(&schemaOnly, "schema-only", false, "Schema only dump.")+rootCmd.AddCommand(dumpCmd)}@@ -38,6 +40,9 @@ var (sslKey string // client-key.pemsslVerifyServerName string // The server name to be verified.+ // Dump options.+ schemaOnly bool+dumpCmd = &cobra.Command{Use: "dump",Short: "Exports the schema of a database instance",@@ -47,7 +52,7 @@ var (SslCert: sslCert,SslKey: sslKey,}- return dump.Dump(databaseType, username, password, hostname, port, database, directory, tlsCfg)+ return dump.Dump(databaseType, username, password, hostname, port, database, directory, tlsCfg, schemaOnly)},})diff --git a/bin/bb/dump/dump.go b/bin/bb/dump/dump.goindex af3665ce5f..6fe857c3c2 100644--- a/bin/bb/dump/dump.go+++ b/bin/bb/dump/dump.go@@ -15,7 +15,7 @@ import (// Dump exports the schema of a database instance.// All non-system databases will be exported to the input directory in the format of database_name.sql for each database.// When directory isn't specified, the schema will be exported to stdout.-func Dump(databaseType, username, password, hostname, port, database, directory string, tlsCfg TlsConfig) error {+func Dump(databaseType, username, password, hostname, port, database, directory string, tlsCfg TlsConfig, schemaOnly bool) error {if directory != "" {dirInfo, err := os.Stat(directory)if os.IsNotExist(err) {@@ -44,14 +44,14 @@ func Dump(databaseType, username, password, hostname, port, database, directory}defer dp.Close()- return dp.Dump(database, directory)+ return dp.Dump(database, directory, schemaOnly)case "pg":dp, err := pgdump.New(username, password, hostname, port, database, tlsCfg.SslCA, tlsCfg.SslCert, tlsCfg.SslKey)if err != nil {return fmt.Errorf("pgdump.New(%q, %q, %q, %q) got error: %v", username, password, hostname, port, err)}defer dp.Close()- return dp.Dump(database, directory)+ return dp.Dump(database, directory, schemaOnly)default:return fmt.Errorf("database type %q not supported; supported types: mysql, pg.", databaseType)}diff --git a/bin/bb/dump/mysqldump/mysql.go b/bin/bb/dump/mysqldump/mysql.goindex 40f370832e..2d70203eda 100644--- a/bin/bb/dump/mysqldump/mysql.go+++ b/bin/bb/dump/mysqldump/mysql.go@@ -92,7 +92,7 @@ func (dp *Dumper) Close() error {}// Dump dumps the schema of a MySQL instance.-func (dp *Dumper) Dump(database, directory string) error {+func (dp *Dumper) Dump(database, directory string, schemaOnly bool) error {dbNames, err := dp.getDatabases()if err != nil {return fmt.Errorf("failed to get databases: %s", err)@@ -134,7 +134,7 @@ func (dp *Dumper) Dump(database, directory string) error {}for _, tbl := range tables {content += fmt.Sprintf("%s\n", tbl.statement)- if tbl.tableType == "BASE TABLE" {+ if !schemaOnly && tbl.tableType == "BASE TABLE" {stmts, err := dp.getTableData(dbName, tbl.name)if err != nil {return errdiff --git a/bin/bb/dump/pgdump/pg.go b/bin/bb/dump/pgdump/pg.goindex 8666855292..10c5869c5b 100644--- a/bin/bb/dump/pgdump/pg.go+++ b/bin/bb/dump/pgdump/pg.go@@ -74,7 +74,7 @@ func (dp *Dumper) switchDatabase(dbName string) error {}// Dump dumps the schema of a Postgres instance.-func (dp *Dumper) Dump(database, directory string) error {+func (dp *Dumper) Dump(database, directory string, schemaOnly bool) error {dbNames, err := dp.getDatabases()if err != nil {return fmt.Errorf("failed to get databases: %s", err)@@ -132,15 +132,17 @@ func (dp *Dumper) Dump(database, directory string) error {}for _, tbl := range tables {content += fmt.Sprintf("%s\n", tbl.Statement())- stmts, err := dp.getTableData(tbl)- if err != nil {- return err- }- for _, stmt := range stmts {- content += stmt- }- if len(stmts) > 0 {- content += "\n"+ if !schemaOnly {+ stmts, err := dp.getTableData(tbl)+ if err != nil {+ return err+ }+ for _, stmt := range stmts {+ content += stmt+ }+ if len(stmts) > 0 {+ content += "\n"+ }}}