Skip to content

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

feat(PITR): implement the logical backup process (#1308)

Yilong Li7aebf5649cb1
diff --git a/api/backup.go b/api/backup.go
index 2243b99532..ecc3f068fc 100644
--- a/api/backup.go
+++ b/api/backup.go
@@ -151,6 +151,7 @@ type BackupPatch struct {
// Domain specific fields
Status string
Comment string
+ Payload string
}
// BackupSetting is the backup setting for a database.
diff --git a/bin/bb/cmd/dump.go b/bin/bb/cmd/dump.go
index 7d94bc0cc4..e4663fdc23 100644
--- a/bin/bb/cmd/dump.go
+++ b/bin/bb/cmd/dump.go
@@ -55,7 +55,7 @@ func dumpDatabase(ctx context.Context, u *dburl.URL, out io.Writer, schemaOnly b
}
defer db.Close(ctx)
- if err := db.Dump(ctx, getDatabase(u), out, schemaOnly); err != nil {
+ if _, err := db.Dump(ctx, getDatabase(u), out, schemaOnly); err != nil {
return fmt.Errorf("failed to create dump, got error: %w", err)
}
return nil
diff --git a/docs/design/pitr-mysql.md b/docs/design/pitr-mysql.md
index 6f9a37bd82..42587f4f7c 100644
--- a/docs/design/pitr-mysql.md
+++ b/docs/design/pitr-mysql.md
@@ -88,7 +88,7 @@ Currently, we keep the logical backup and binlog together in the local disk wher
The partial backup dump should be discarded if Bytebase encounters an unrecoverable error during the full backup process. The system should report a failed backup, and the user could choose to start another backup task manually. The partially dumped logical backup file will be automatically deleted by Bytebase.
-To check the integrity of the logical dump, Bytebase will write file size along with the table data into the backup file. When using a logical backup to recover, we should first validate the file size.
+To check the integrity of the logical dump, Bytebase will save file size in the backup metadata. When using a logical backup to recover, we should first validate the file size of the dump file and the metadata matches.
## Incremental Backup
@@ -168,11 +168,14 @@ An example of the PITR information is like this:
"binlog_info": {
"binlog_name": "binlog.000001",
"binlog_position": 1234,
- "created_ts": 1650957790
- }
+ "ts": 1650957790,
+ },
+ "file_size": 10240
}
```
+where the `ts` field is a UNIX timestamp in seconds, indicating the binlog event time in binlog.000001 at the position 1234.
+
We will add a field in api.Backup:
```go
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 36870756fd..3ed304ce3d 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -703,22 +703,22 @@ const (
)
// Dump dumps the database.
-func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) (string, error) {
txn, err := driver.db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
- return err
+ return "", err
}
defer txn.Rollback()
if err := dumpTxn(ctx, txn, database, out, schemaOnly); err != nil {
- return err
+ return "", err
}
if err := txn.Commit(); err != nil {
- return err
+ return "", err
}
- return nil
+ return "", nil
}
// dumpTxn will dump the input database. schemaOnly isn't supported yet and true by default.
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 2ac879bb89..5bb0167287 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -428,7 +428,9 @@ type Driver interface {
// Dump and restore
// Dump the database, if dbName is empty, then dump all databases.
- Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error
+ // The returned string is the JSON encoded metadata for the logical dump.
+ // For MySQL, the payload contains the binlog filename and position when the dump is generated.
+ Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) (string, error)
// Restore the database from sc, which is a full backup.
Restore(ctx context.Context, sc *bufio.Scanner) error
}
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index cf71f14f2f..30c035b57b 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -4,10 +4,12 @@ import (
"bufio"
"context"
"database/sql"
+ "encoding/json"
"fmt"
"io"
"regexp"
"strings"
+ "time"
// embed will embeds the migration schema.
_ "embed"
@@ -44,6 +46,20 @@ func init() {
db.Register(db.TiDB, newDriver)
}
+// BinlogInfo is the binlog coordination for MySQL.
+type BinlogInfo struct {
+ FileName string `json:"fileName"`
+ Position int64 `json:"position"`
+}
+
+// This is encoded in JSON and stored in the backup table, representing PITR related info.
+type backupPayload struct {
+ BinlogInfo BinlogInfo `json:"binlogInfo"`
+ // Imprecise UNIX timestamp to the second which is the rough time when this backup is taken.
+ // Mainly for UI purpose.
+ Ts int64 `json:"ts"`
+}
+
// Driver is the MySQL driver.
type Driver struct {
l *zap.Logger
@@ -889,29 +905,68 @@ const (
)
// Dump dumps the database.
-func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) (string, error) {
// mysqldump -u root --databases dbName --no-data --routines --events --triggers --compact
+ // We must use the same MySQL connection to lock and unlock tables.
+ conn, err := driver.db.Conn(ctx)
+ if err != nil {
+ return "", err
+ }
+ defer conn.Close()
+
+ driver.l.Debug("flush tables in database with read locks",
+ zap.String("database", database))
+ if err := flushTablesWithReadLock(ctx, conn, database); err != nil {
+ return "", err
+ }
+
+ binlog, err := getBinlogInfo(ctx, conn)
+ if err != nil {
+ return "", err
+ }
+ driver.l.Debug("binlog config at dump time",
+ zap.String("filename", binlog.FileName),
+ zap.Int64("position", binlog.Position))
+
+ ts, err := getServerTime(ctx, driver.db)
+ if err != nil {
+ return "", err
+ }
+
+ payload := backupPayload{
+ BinlogInfo: binlog,
+ Ts: ts,
+ }
+ payloadBytes, err := json.Marshal(payload)
+ if err != nil {
+ return "", err
+ }
+
options := sql.TxOptions{}
// TiDB does not support readonly, so we only set for MySQL.
if driver.dbType == "MYSQL" {
options.ReadOnly = true
}
- txn, err := driver.db.BeginTx(ctx, &options)
+ // Now we are still holding the tables' exclusive locks.
+ // Beginning a transaction in the same session will implicitly release existing table locks.
+ // ref: https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html, section "Interaction of Table Locking and Transactions".
+ txn, err := conn.BeginTx(ctx, &options)
if err != nil {
- return err
+ return "", err
}
defer txn.Rollback()
+ driver.l.Debug("begin to dump database")
if err := dumpTxn(ctx, txn, database, out, schemaOnly); err != nil {
- return err
+ return "", err
}
if err := txn.Commit(); err != nil {
- return err
+ return "", err
}
- return nil
+ return string(payloadBytes), nil
}
// Restore restores a database.
@@ -940,9 +995,56 @@ func (driver *Driver) Restore(ctx context.Context, sc *bufio.Scanner) (err error
return nil
}
+func getServerTime(ctx context.Context, db *sql.DB) (int64, error) {
+ var timestamp int64
+ rows, err := db.QueryContext(ctx, "SELECT UNIX_TIMESTAMP(CURRENT_TIMESTAMP());")
+ if err != nil {
+ return 0, err
+ }
+ rows.Next()
+ if err := rows.Scan(&timestamp); err != nil {
+ return 0, err
+ }
+ return timestamp, nil
+}
+
+func flushTablesWithReadLock(ctx context.Context, conn *sql.Conn, database string) error {
+ // The lock acquiring could take a long time if there are concurrent exclusive locks on the tables.
+ // We ensures that the execution is canceled after 30 seconds, otherwise we may get dead lock and stuck forever.
+ ctxWithTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
+ defer cancel()
+
+ txn, err := conn.BeginTx(ctxWithTimeout, nil)
+ if err != nil {
+ return err
+ }
+ defer txn.Rollback()
+
+ tables, err := GetTables(txn, database)
+ if err != nil {
+ return err
+ }
+
+ var tableNames []string
+ for _, table := range tables {
+ tableNames = append(tableNames, fmt.Sprintf("`%s`", table.Name))
+ }
+ flushTableStmt := fmt.Sprintf("FLUSH TABLES %s WITH READ LOCK;", strings.Join(tableNames, ", "))
+
+ if _, err := txn.ExecContext(ctxWithTimeout, flushTableStmt); err != nil {
+ return err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, schemaOnly bool) error {
// Find all dumpable databases
- dbNames, err := getDatabases(txn)
+ dbNames, err := getDatabases(ctx, txn)
if err != nil {
return fmt.Errorf("failed to get databases: %s", err)
}
@@ -1056,9 +1158,9 @@ func excludeSchemaAutoIncrementValue(s string) string {
}
// getDatabases gets all databases of an instance.
-func getDatabases(txn *sql.Tx) ([]string, error) {
+func getDatabases(ctx context.Context, txn *sql.Tx) ([]string, error) {
var dbNames []string
- rows, err := txn.Query("SHOW DATABASES;")
+ rows, err := txn.QueryContext(ctx, "SHOW DATABASES;")
if err != nil {
return nil, err
}
@@ -1074,6 +1176,23 @@ func getDatabases(txn *sql.Tx) ([]string, error) {
return dbNames, nil
}
+func getBinlogInfo(ctx context.Context, conn *sql.Conn) (BinlogInfo, error) {
+ rows, err := conn.QueryContext(ctx, "SHOW MASTER STATUS;")
+ if err != nil {
+ return BinlogInfo{}, err
+ }
+ defer rows.Close()
+
+ rows.Next()
+ binlogInfo := BinlogInfo{}
+ var unused interface{}
+ if err := rows.Scan(&binlogInfo.FileName, &binlogInfo.Position, &unused, &unused, &unused); err != nil {
+ return BinlogInfo{}, err
+ }
+
+ return binlogInfo, nil
+}
+
// getDatabaseStmt gets the create statement of a database.
func getDatabaseStmt(txn *sql.Tx, dbName string) (string, error) {
query := fmt.Sprintf("SHOW CREATE DATABASE IF NOT EXISTS `%s`;", dbName)
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index 5d4e85cfa1..fa9cdcc3d4 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -806,13 +806,13 @@ func (driver *Driver) updateMigrationHistoryStorageVersion(ctx context.Context)
// Dump and restore
// Dump dumps the database.
-func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) (string, error) {
// pg_dump -d dbName --schema-only+
// Find all dumpable databases
databases, err := driver.getDatabases()
if err != nil {
- return fmt.Errorf("failed to get databases: %s", err)
+ return "", fmt.Errorf("failed to get databases: %s", err)
}
var dumpableDbNames []string
@@ -825,7 +825,7 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,
}
}
if !exist {
- return fmt.Errorf("database %s not found", database)
+ return "", fmt.Errorf("database %s not found", database)
}
dumpableDbNames = []string{database}
} else {
@@ -840,11 +840,11 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,
for _, dbName := range dumpableDbNames {
includeUseDatabase := len(dumpableDbNames) > 1
if err := driver.dumpOneDatabase(ctx, dbName, out, schemaOnly, includeUseDatabase); err != nil {
- return err
+ return "", err
}
}
- return nil
+ return "", nil
}
// Restore restores a database.
diff --git a/plugin/db/snowflake/snowflake.go b/plugin/db/snowflake/snowflake.go
index ff243d6b54..941946320c 100644
--- a/plugin/db/snowflake/snowflake.go
+++ b/plugin/db/snowflake/snowflake.go
@@ -868,22 +868,22 @@ const (
)
// Dump dumps the database.
-func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) (string, error) {
txn, err := driver.db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
- return err
+ return "", err
}
defer txn.Rollback()
if err := dumpTxn(ctx, txn, database, out, schemaOnly); err != nil {
- return err
+ return "", err
}
if err := txn.Commit(); err != nil {
- return err
+ return "", err
}
- return nil
+ return "", nil
}
// dumpTxn will dump the input database. schemaOnly isn't supported yet and true by default.
diff --git a/plugin/db/sqlite/sqlite.go b/plugin/db/sqlite/sqlite.go
index ab6e5dc6c4..051f518e82 100644
--- a/plugin/db/sqlite/sqlite.go
+++ b/plugin/db/sqlite/sqlite.go
@@ -632,15 +632,15 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
}
// Dump dumps the database.
-func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) (string, error) {
if database == "" {
- return fmt.Errorf("SQLite can dump one database only at a time")
+ return "", fmt.Errorf("SQLite can dump one database only at a time")
}
// Find all dumpable databases and make sure the existence of the database to be dumped.
databases, err := driver.getDatabases()
if err != nil {
- return fmt.Errorf("failed to get databases: %s", err)
+ return "", fmt.Errorf("failed to get databases: %s", err)
}
exist := false
for _, n := range databases {
@@ -650,14 +650,14 @@ func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer,
}
}
if !exist {
- return fmt.Errorf("database %s not found", database)
+ return "", fmt.Errorf("database %s not found", database)
}
if err := driver.dumpOneDatabase(ctx, database, out, schemaOnly); err != nil {
- return err
+ return "", err
}
- return nil
+ return "", nil
}
type sqliteSchema struct {
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 24cdff6a13..a9a70b7425 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -131,7 +131,7 @@ func ExecuteMigration(ctx context.Context, l *zap.Logger, executor MigrationExec
if !m.CreateDatabase {
// For baseline migration, we also record the live schema to detect the schema drift.
// See https://bytebase.com/blog/what-is-database-schema-drift
- if err := executor.Dump(ctx, m.Database, &prevSchemaBuf, true /*schemaOnly*/); err != nil {
+ if _, err := executor.Dump(ctx, m.Database, &prevSchemaBuf, true /*schemaOnly*/); err != nil {
return -1, "", FormatError(err)
}
}
@@ -172,7 +172,7 @@ func ExecuteMigration(ctx context.Context, l *zap.Logger, executor MigrationExec
// Phase 4 - Dump the schema after migration
var afterSchemaBuf bytes.Buffer
- if err := executor.Dump(ctx, m.Database, &afterSchemaBuf, true /*schemaOnly*/); err != nil {
+ if _, err := executor.Dump(ctx, m.Database, &afterSchemaBuf, true /*schemaOnly*/); err != nil {
return -1, "", FormatError(err)
}
diff --git a/plugin/restore/mysql/mysql.go b/plugin/restore/mysql/mysql.go
index 01a1cfdc58..7acb196002 100644
--- a/plugin/restore/mysql/mysql.go
+++ b/plugin/restore/mysql/mysql.go
@@ -14,12 +14,6 @@ const (
MaxDatabaseNameLength = 64
)
-// BinlogConfig is the binlog coordination for MySQL.
-type BinlogConfig struct {
- Filename string
- Position int64
-}
-
// Restore implements recovery functions for MySQL
type Restore struct {
driver *mysql.Driver
@@ -33,12 +27,12 @@ func New(driver *mysql.Driver) *Restore {
}
// RestoreBinlog restores the database using incremental backup in time range of [config.Start, config.End).
-func (r *Restore) RestoreBinlog(ctx context.Context, config BinlogConfig) error {
+func (r *Restore) RestoreBinlog(ctx context.Context, config mysql.BinlogInfo) error {
return fmt.Errorf("Unimplemented")
}
// RestorePITR is a wrapper for restore a full backup and a range of incremental backup
-func (r *Restore) RestorePITR(ctx context.Context, fullBackup *bufio.Scanner, config BinlogConfig, database string, timestamp int64) error {
+func (r *Restore) RestorePITR(ctx context.Context, fullBackup *bufio.Scanner, binlog mysql.BinlogInfo, database string, timestamp int64) error {
pitrDatabaseName := GetPITRDatabaseName(database, timestamp)
query := fmt.Sprintf(""+
// Create the pitr database.
@@ -69,7 +63,7 @@ func (r *Restore) RestorePITR(ctx context.Context, fullBackup *bufio.Scanner, co
}
// TODO(dragonly): implement RestoreBinlog in mysql driver
- _ = r.RestoreBinlog(ctx, config)
+ _ = r.RestoreBinlog(ctx, binlog)
return nil
}
diff --git a/server/anomaly_scanner.go b/server/anomaly_scanner.go
index 2b36a2e1f2..3ef146bc49 100644
--- a/server/anomaly_scanner.go
+++ b/server/anomaly_scanner.go
@@ -286,7 +286,7 @@ func (s *AnomalyScanner) checkDatabaseAnomaly(ctx context.Context, instance *api
goto SchemaDriftEnd
}
var schemaBuf bytes.Buffer
- if err := driver.Dump(ctx, database.Name, &schemaBuf, true /*schemaOnly*/); err != nil {
+ if _, err := driver.Dump(ctx, database.Name, &schemaBuf, true /*schemaOnly*/); err != nil {
if common.ErrorCode(err) == common.NotFound {
s.l.Debug("Failed to check anomaly",
zap.String("instance", instance.Name),
diff --git a/server/database.go b/server/database.go
index 242a4676a5..ca4453667a 100644
--- a/server/database.go
+++ b/server/database.go
@@ -239,7 +239,7 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
}
var schemaBuf bytes.Buffer
- if err := driver.Dump(ctx, database.Name, &schemaBuf, true /* schemaOnly */); err != nil {
+ if _, err := driver.Dump(ctx, database.Name, &schemaBuf, true /* schemaOnly */); err != nil {
return fmt.Errorf("failed to get database schema for database %q: %w", database.Name, err)
}
if peerSchema != schemaBuf.String() {
diff --git a/server/issue.go b/server/issue.go
index 1703f3415c..33abb0aaad 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -1248,7 +1248,7 @@ func (s *Server) getSchemaFromPeerTenantDatabase(ctx context.Context, instance *
}
var schemaBuf bytes.Buffer
- if err := driver.Dump(ctx, similarDB.Name, &schemaBuf, true /* schemaOnly */); err != nil {
+ if _, err := driver.Dump(ctx, similarDB.Name, &schemaBuf, true /* schemaOnly */); err != nil {
return "", "", err
}
return schemaVersion, schemaBuf.String(), nil
diff --git a/server/task_executor_database_backup.go b/server/task_executor_database_backup.go
index 980d8b9c78..fc1ebb8045 100644
--- a/server/task_executor_database_backup.go
+++ b/server/task_executor_database_backup.go
@@ -55,19 +55,21 @@ func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *Ser
zap.String("backup", backup.Name),
)
- backupErr := exec.backupDatabase(ctx, task.Instance, task.Database.Name, backup, server.profile.DataDir)
+ backupPayload, backupErr := exec.backupDatabase(ctx, task.Instance, task.Database.Name, backup, server.profile.DataDir)
// Update the status of the backup.
newBackupStatus := string(api.BackupStatusDone)
comment := ""
if backupErr != nil {
newBackupStatus = string(api.BackupStatusFailed)
comment = backupErr.Error()
+ backupPayload = "{}"
}
if _, err := server.store.PatchBackup(ctx, &api.BackupPatch{
ID: backup.ID,
Status: newBackupStatus,
UpdaterID: api.SystemBotID,
Comment: comment,
+ Payload: backupPayload,
}); err != nil {
return true, nil, fmt.Errorf("failed to patch backup: %w", err)
}
@@ -82,24 +84,25 @@ func (exec *DatabaseBackupTaskExecutor) RunOnce(ctx context.Context, server *Ser
}
// backupDatabase will take a backup of a database.
-func (exec *DatabaseBackupTaskExecutor) backupDatabase(ctx context.Context, instance *api.Instance, databaseName string, backup *api.Backup, dataDir string) error {
+func (exec *DatabaseBackupTaskExecutor) backupDatabase(ctx context.Context, instance *api.Instance, databaseName string, backup *api.Backup, dataDir string) (string, error) {
driver, err := getAdminDatabaseDriver(ctx, instance, databaseName, exec.l)
if err != nil {
- return err
+ return "", err
}
defer driver.Close(ctx)
f, err := os.Create(filepath.Join(dataDir, backup.Path))
if err != nil {
- return fmt.Errorf("failed to open backup path: %s", backup.Path)
+ return "", fmt.Errorf("failed to open backup path: %s", backup.Path)
}
defer f.Close()
- if err := driver.Dump(ctx, databaseName, f, false /* schemaOnly */); err != nil {
- return err
+ payload, err := driver.Dump(ctx, databaseName, f, false /* schemaOnly */)
+ if err != nil {
+ return "", err
}
- return nil
+ return payload, nil
}
// getAndCreateBackupDirectory returns the path of a database backup.
diff --git a/server/task_executor_schema_update_ghost_sync.go b/server/task_executor_schema_update_ghost_sync.go
index af97d91633..337f426284 100644
--- a/server/task_executor_schema_update_ghost_sync.go
+++ b/server/task_executor_schema_update_ghost_sync.go
@@ -173,7 +173,7 @@ func executeSync(ctx context.Context, l *zap.Logger, task *api.Task, mi *db.Migr
executor := driver.(util.MigrationExecutor)
var prevSchemaBuf bytes.Buffer
- if err := driver.Dump(ctx, mi.Database, &prevSchemaBuf, true); err != nil {
+ if _, err := driver.Dump(ctx, mi.Database, &prevSchemaBuf, true); err != nil {
return -1, "", err
}
@@ -198,7 +198,7 @@ func executeSync(ctx context.Context, l *zap.Logger, task *api.Task, mi *db.Migr
}
var afterSchemaBuf bytes.Buffer
- if err := executor.Dump(ctx, mi.Database, &afterSchemaBuf, true /*schemaOnly*/); err != nil {
+ if _, err := executor.Dump(ctx, mi.Database, &afterSchemaBuf, true /*schemaOnly*/); err != nil {
return -1, "", util.FormatError(err)
}
diff --git a/store/backup.go b/store/backup.go
index d36fc97bee..87fb1af815 100644
--- a/store/backup.go
+++ b/store/backup.go
@@ -32,6 +32,7 @@ type backupRaw struct {
MigrationHistoryVersion string
Path string
Comment string
+ Payload string
}
// toBackup creates an instance of Backup based on the backupRaw.
@@ -512,6 +513,10 @@ func (s *Store) patchBackupImpl(ctx context.Context, tx *sql.Tx, patch *api.Back
set, args := []string{"updater_id = $1"}, []interface{}{patch.UpdaterID}
set, args = append(set, "status = $2"), append(args, patch.Status)
set, args = append(set, "comment = $3"), append(args, patch.Comment)
+ if patch.Payload == "" {
+ patch.Payload = "{}"
+ }
+ set, args = append(set, "payload = $4"), append(args, patch.Payload)
args = append(args, patch.ID)
@@ -519,8 +524,8 @@ func (s *Store) patchBackupImpl(ctx context.Context, tx *sql.Tx, patch *api.Back
row, err := tx.QueryContext(ctx, `
UPDATE backup
SET `+strings.Join(set, ", ")+`
- WHERE id = $4
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, database_id, name, status, type, storage_backend, migration_history_version, path, comment
+ WHERE id = $5
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, database_id, name, status, type, storage_backend, migration_history_version, path, comment, payload
`,
args...,
)
@@ -545,6 +550,7 @@ func (s *Store) patchBackupImpl(ctx context.Context, tx *sql.Tx, patch *api.Back
&backupRaw.MigrationHistoryVersion,
&backupRaw.Path,
&backupRaw.Comment,
+ &backupRaw.Payload,
); err != nil {
return nil, FormatError(err)
}
diff --git a/store/migration/dev/20220516000000__backup.sql b/store/migration/dev/20220516000000__backup.sql
new file mode 100644
index 0000000000..6f0b8a68f3
--- /dev/null
+++ b/store/migration/dev/20220516000000__backup.sql
@@ -0,0 +1 @@
+ALTER TABLE backup ADD payload JSONB NOT NULL DEFAULT '{}';
\ No newline at end of file
diff --git a/store/migration/dev/LATEST.sql b/store/migration/dev/LATEST.sql
index 609f1914b5..5f6318f077 100644
--- a/store/migration/dev/LATEST.sql
+++ b/store/migration/dev/LATEST.sql
@@ -513,7 +513,8 @@ CREATE TABLE backup (
storage_backend TEXT NOT NULL CHECK (storage_backend IN ('LOCAL', 'S3', 'GCS', 'OSS')),
migration_history_version TEXT NOT NULL,
path TEXT NOT NULL,
- comment TEXT NOT NULL DEFAULT ''
+ comment TEXT NOT NULL DEFAULT '',
+ payload JSONB NOT NULL DEFAULT '{}'
);
CREATE INDEX idx_backup_database_id ON backup(database_id);
diff --git a/tests/backup_restore_test.go b/tests/backup_restore_test.go
index 6c2969d26c..f9f1de9464 100644
--- a/tests/backup_restore_test.go
+++ b/tests/backup_restore_test.go
@@ -163,7 +163,7 @@ func TestPITR(t *testing.T) {
mysqlDriver, ok := driver.(*pluginmysql.Driver)
a.Equal(true, ok)
mysqlRestore := restoremysql.New(mysqlDriver)
- config := restoremysql.BinlogConfig{}
+ config := pluginmysql.BinlogInfo{}
err := mysqlRestore.RestorePITR(ctx, bufio.NewScanner(buf), config, database, timestamp)
a.NoError(err)
@@ -361,7 +361,7 @@ func doBackup(ctx context.Context, t *testing.T, driver dbplugin.Driver, databas
a := require.New(t)
var buf bytes.Buffer
- err := driver.Dump(ctx, database, &buf, false)
+ _, err := driver.Dump(ctx, database, &buf, false)
a.NoError(err)
return &buf