Skip to content

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

BB CLI: support restoring postgres schema.

spinningbota2e634c52a39
diff --git a/bin/bb/cmd/restore.go b/bin/bb/cmd/restore.go
index 2cadb7215e..97d99f04a6 100644
--- a/bin/bb/cmd/restore.go
+++ b/bin/bb/cmd/restore.go
@@ -8,6 +8,7 @@ import (
"github.com/bytebase/bytebase/bin/bb/connect"
"github.com/bytebase/bytebase/bin/bb/restore/mysqlrestore"
+ "github.com/bytebase/bytebase/bin/bb/restore/pgrestore"
"github.com/spf13/cobra"
)
@@ -82,6 +83,12 @@ func restoreDatabase(databaseType, username, password, hostname, port, database,
}
defer conn.Close()
+ if err := conn.SwitchDatabase(database); err != nil {
+ return fmt.Errorf("conn.SwitchDatabase(%q) got error: %v", database, err)
+ }
+ if err := pgrestore.Restore(conn, sc); err != nil {
+ return fmt.Errorf("mysqlrestore.Restore() got error: %v", err)
+ }
return nil
default:
return fmt.Errorf("database type %q not supported; supported types: mysql, pg.", databaseType)
diff --git a/bin/bb/restore/mysqlrestore/mysql.go b/bin/bb/restore/mysqlrestore/mysql.go
index facd8ce94e..2bacb20aef 100644
--- a/bin/bb/restore/mysqlrestore/mysql.go
+++ b/bin/bb/restore/mysqlrestore/mysql.go
@@ -1,4 +1,4 @@
-// mysqlrestore is a library for importing MySQL database schemas and data provided by bytebase.com.
+// mysqlrestore is a library for restoring MySQL database schemas and data provided by bytebase.com.
package mysqlrestore
import (
@@ -9,6 +9,7 @@ import (
"github.com/bytebase/bytebase/bin/bb/connect"
)
+// Restore restores the schema of a MySQL instance.
func Restore(conn *connect.MysqlConnect, sc *bufio.Scanner) error {
s := ""
delimiter := false
diff --git a/bin/bb/restore/pgrestore/pg.go b/bin/bb/restore/pgrestore/pg.go
index 617fc427e3..3400f2869d 100644
--- a/bin/bb/restore/pgrestore/pg.go
+++ b/bin/bb/restore/pgrestore/pg.go
@@ -1,2 +1,63 @@
-// pgrestore is a library for importing Postgres database schemas and data provided by bytebase.com.
+// pgrestore is a library for restoring Postgres database schemas and data provided by bytebase.com.
package pgrestore
+
+import (
+ "bufio"
+ "fmt"
+ "regexp"
+ "strings"
+
+ "github.com/bytebase/bytebase/bin/bb/connect"
+)
+
+var (
+ asToken = regexp.MustCompile("(AS )[$]([a-z]+)[$]")
+)
+
+// Restore restores the schema of a Postgres instance.
+func Restore(conn *connect.PostgresConnect, sc *bufio.Scanner) error {
+ s := ""
+ tokenName := ""
+ for sc.Scan() {
+ line := sc.Text()
+
+ execute := false
+
+ switch {
+ case s == "" && line == "":
+ continue
+ case strings.HasPrefix(line, "--"):
+ continue
+ case tokenName != "":
+ if strings.Contains(line, tokenName) {
+ tokenName = ""
+ }
+ default:
+ token := asToken.FindString(line)
+ if token != "" {
+ identifier := token[3:]
+ rest := line[strings.Index(line, identifier)+len(identifier):]
+ if !strings.Contains(rest, identifier) {
+ tokenName = identifier
+ }
+ }
+ }
+ s = s + line + "\n"
+ if strings.HasSuffix(line, ";") && tokenName == "" {
+ execute = true
+ }
+
+ if execute {
+ _, err := conn.DB.Exec(s)
+ if err != nil {
+ return fmt.Errorf("execute query %q failed: %v", s, err)
+ }
+ s = ""
+ }
+ }
+ if err := sc.Err(); err != nil {
+ return err
+ }
+
+ return nil
+}