Skip to content

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

refactor: move insert history logic into its functor (#527)

Danny Xud1028d733672
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 72d0d13648..02e1fa88df 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -495,14 +495,62 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
WHERE namespace = ?
`
+ insertPendingFunc := func(tx *sql.Tx, sequence int, prevSchema string) (int64, error) {
+ var insertedID int64
+ maxIDQuery := "SELECT MAX(id)+1 FROM bytebase.migration_history"
+ rows, err := tx.QueryContext(ctx, maxIDQuery)
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, maxIDQuery)
+ }
+ defer rows.Close()
+ for rows.Next() {
+ if err := rows.Scan(
+ &insertedID,
+ ); err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, maxIDQuery)
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, maxIDQuery)
+ }
+ // Clickhouse sql driver doesn't support taking now() as prepared value.
+ now := time.Now().Unix()
+ _, err = tx.ExecContext(ctx, insertHistoryQuery,
+ insertedID,
+ m.Creator,
+ now,
+ m.Creator,
+ now,
+ m.ReleaseVersion,
+ m.Namespace,
+ sequence,
+ m.Engine,
+ m.Type,
+ "PENDING",
+ m.Version,
+ m.Description,
+ statement,
+ prevSchema,
+ prevSchema,
+ 0,
+ m.IssueID,
+ m.Payload,
+ )
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, insertHistoryQuery)
+ }
+
+ return insertedID, nil
+ }
+
args := util.MigrationExecutionArgs{
- InsertHistoryQuery: insertHistoryQuery,
UpdateHistoryAsDoneQuery: updateHistoryAsDoneQuery,
UpdateHistoryAsFailedQuery: updateHistoryAsFailedQuery,
CheckDuplicateVersionQuery: checkDuplicateVersionQuery,
FindBaselineQuery: findBaselineQuery,
CheckOutofOrderVersionQuery: checkOutofOrderVersionQuery,
FindNextSequenceQuery: findNextSequenceQuery,
+ InsertPendingHistoryFunc: insertPendingFunc,
}
return util.ExecuteMigration(ctx, driver.l, db.ClickHouse, driver, m, statement, args)
}
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index dc5ebe3581..3b59b22759 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -654,14 +654,42 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
WHERE namespace = ?
`
+ insertPendingFunc := func(tx *sql.Tx, sequence int, prevSchema string) (int64, error) {
+ res, err := tx.ExecContext(ctx, insertHistoryQuery,
+ m.Creator,
+ m.Creator,
+ m.ReleaseVersion,
+ m.Namespace,
+ sequence,
+ m.Engine,
+ m.Type,
+ m.Version,
+ m.Description,
+ statement,
+ prevSchema,
+ prevSchema,
+ m.IssueID,
+ m.Payload,
+ )
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, insertHistoryQuery)
+ }
+
+ insertedID, err := res.LastInsertId()
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, insertHistoryQuery)
+ }
+ return insertedID, nil
+ }
+
args := util.MigrationExecutionArgs{
- InsertHistoryQuery: insertHistoryQuery,
UpdateHistoryAsDoneQuery: updateHistoryAsDoneQuery,
UpdateHistoryAsFailedQuery: updateHistoryAsFailedQuery,
CheckDuplicateVersionQuery: checkDuplicateVersionQuery,
FindBaselineQuery: findBaselineQuery,
CheckOutofOrderVersionQuery: checkOutofOrderVersionQuery,
FindNextSequenceQuery: findNextSequenceQuery,
+ InsertPendingHistoryFunc: insertPendingFunc,
}
return util.ExecuteMigration(ctx, driver.l, db.MySQL, driver, m, statement, args)
}
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index 42fa86fc80..67dedc951b 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -714,14 +714,35 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
WHERE namespace = $1
`
+ insertPendingFunc := func(tx *sql.Tx, sequence int, prevSchema string) (int64, error) {
+ var insertedID int64
+ tx.QueryRowContext(ctx, insertHistoryQuery,
+ m.Creator,
+ m.Creator,
+ m.ReleaseVersion,
+ m.Namespace,
+ sequence,
+ m.Engine,
+ m.Type,
+ m.Version,
+ m.Description,
+ statement,
+ prevSchema,
+ prevSchema,
+ m.IssueID,
+ m.Payload,
+ ).Scan(&insertedID)
+ return insertedID, nil
+ }
+
args := util.MigrationExecutionArgs{
- InsertHistoryQuery: insertHistoryQuery,
UpdateHistoryAsDoneQuery: updateHistoryAsDoneQuery,
UpdateHistoryAsFailedQuery: updateHistoryAsFailedQuery,
CheckDuplicateVersionQuery: checkDuplicateVersionQuery,
FindBaselineQuery: findBaselineQuery,
CheckOutofOrderVersionQuery: checkOutofOrderVersionQuery,
FindNextSequenceQuery: findNextSequenceQuery,
+ InsertPendingHistoryFunc: insertPendingFunc,
}
return util.ExecuteMigration(ctx, driver.l, db.Postgres, driver, m, statement, args)
}
diff --git a/plugin/db/snowflake/snowflake.go b/plugin/db/snowflake/snowflake.go
index d02785688b..ff86d526df 100644
--- a/plugin/db/snowflake/snowflake.go
+++ b/plugin/db/snowflake/snowflake.go
@@ -649,14 +649,61 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
WHERE namespace = ?
`
+ insertPendingFunc := func(tx *sql.Tx, sequence int, prevSchema string) (int64, error) {
+ var insertedID int64
+ maxIDQuery := "SELECT MAX(id)+1 FROM bytebase.public.migration_history"
+ rows, err := tx.QueryContext(ctx, maxIDQuery)
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, maxIDQuery)
+ }
+ defer rows.Close()
+ var id sql.NullInt64
+ for rows.Next() {
+ if err := rows.Scan(
+ &id,
+ ); err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, maxIDQuery)
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, maxIDQuery)
+ }
+ if id.Valid {
+ insertedID = id.Int64
+ } else {
+ insertedID = 1
+ }
+
+ _, err = tx.ExecContext(ctx, insertHistoryQuery,
+ m.Creator,
+ m.Creator,
+ m.ReleaseVersion,
+ m.Namespace,
+ sequence,
+ m.Engine,
+ m.Type,
+ m.Version,
+ m.Description,
+ statement,
+ prevSchema,
+ prevSchema,
+ m.IssueID,
+ m.Payload,
+ )
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, insertHistoryQuery)
+ }
+ return insertedID, nil
+ }
+
args := util.MigrationExecutionArgs{
- InsertHistoryQuery: insertHistoryQuery,
UpdateHistoryAsDoneQuery: updateHistoryAsDoneQuery,
UpdateHistoryAsFailedQuery: updateHistoryAsFailedQuery,
CheckDuplicateVersionQuery: checkDuplicateVersionQuery,
FindBaselineQuery: findBaselineQuery,
CheckOutofOrderVersionQuery: checkOutofOrderVersionQuery,
FindNextSequenceQuery: findNextSequenceQuery,
+ InsertPendingHistoryFunc: insertPendingFunc,
}
return util.ExecuteMigration(ctx, driver.l, db.Snowflake, driver, m, statement, args)
}
diff --git a/plugin/db/sqlite/sqlite.go b/plugin/db/sqlite/sqlite.go
index d411bd3e85..b572ba1146 100644
--- a/plugin/db/sqlite/sqlite.go
+++ b/plugin/db/sqlite/sqlite.go
@@ -322,14 +322,42 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
WHERE namespace = ?
`
+ insertPendingFunc := func(tx *sql.Tx, sequence int, prevSchema string) (int64, error) {
+ res, err := tx.ExecContext(ctx, insertHistoryQuery,
+ m.Creator,
+ m.Creator,
+ m.ReleaseVersion,
+ m.Namespace,
+ sequence,
+ m.Engine,
+ m.Type,
+ m.Version,
+ m.Description,
+ statement,
+ prevSchema,
+ prevSchema,
+ m.IssueID,
+ m.Payload,
+ )
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, insertHistoryQuery)
+ }
+
+ insertedID, err := res.LastInsertId()
+ if err != nil {
+ return int64(0), util.FormatErrorWithQuery(err, insertHistoryQuery)
+ }
+ return insertedID, nil
+ }
+
args := util.MigrationExecutionArgs{
- InsertHistoryQuery: insertHistoryQuery,
UpdateHistoryAsDoneQuery: updateHistoryAsDoneQuery,
UpdateHistoryAsFailedQuery: updateHistoryAsFailedQuery,
CheckDuplicateVersionQuery: checkDuplicateVersionQuery,
FindBaselineQuery: findBaselineQuery,
CheckOutofOrderVersionQuery: checkOutofOrderVersionQuery,
FindNextSequenceQuery: findNextSequenceQuery,
+ InsertPendingHistoryFunc: insertPendingFunc,
}
return util.ExecuteMigration(ctx, driver.l, db.SQLite, driver, m, statement, args)
}
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 44318471b3..1de292fd25 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -111,7 +111,6 @@ func NeedsSetupMigrationSchema(ctx context.Context, sqldb *sql.DB, query string)
// MigrationExecutionArgs includes the arguments for ExecuteMigration().
type MigrationExecutionArgs struct {
- InsertHistoryQuery string
UpdateHistoryAsDoneQuery string
UpdateHistoryAsFailedQuery string
CheckDuplicateVersionQuery string
@@ -119,6 +118,7 @@ type MigrationExecutionArgs struct {
CheckOutofOrderVersionQuery string
FindNextSequenceQuery string
FindMigrationHistoryListQuery string
+ InsertPendingHistoryFunc func(tx *sql.Tx, sequence int, prevSchema string) (int64, error)
}
// ExecuteMigration will execute the database migration.
@@ -186,136 +186,9 @@ func ExecuteMigration(ctx context.Context, l *zap.Logger, dbType db.Type, driver
// MySQL runs DDL in its own transaction, so we can't commit migration history together with DDL in a single transaction.
// Thus we sort of doing a 2-phase commit, where we first write a PENDING migration record, and after migration completes, we then
// update the record to DONE together with the updated schema.
- insertedID := int64(-1)
- if dbType == db.Postgres {
- tx.QueryRowContext(ctx, args.InsertHistoryQuery,
- m.Creator,
- m.Creator,
- m.ReleaseVersion,
- m.Namespace,
- sequence,
- m.Engine,
- m.Type,
- m.Version,
- m.Description,
- statement,
- prevSchemaBuf.String(),
- prevSchemaBuf.String(),
- m.IssueID,
- m.Payload,
- ).Scan(&insertedID)
- } else if dbType == db.ClickHouse {
- maxIDQuery := "SELECT MAX(id)+1 FROM bytebase.migration_history"
- rows, err := tx.QueryContext(ctx, maxIDQuery)
- if err != nil {
- return -1, "", FormatErrorWithQuery(err, maxIDQuery)
- }
- defer rows.Close()
- for rows.Next() {
- if err := rows.Scan(
- &insertedID,
- ); err != nil {
- return -1, "", FormatErrorWithQuery(err, maxIDQuery)
- }
- }
- if err := rows.Err(); err != nil {
- return -1, "", FormatErrorWithQuery(err, maxIDQuery)
- }
- // Clickhouse sql driver doesn't support taking now() as prepared value.
- now := time.Now().Unix()
- _, err = tx.ExecContext(ctx, args.InsertHistoryQuery,
- insertedID,
- m.Creator,
- now,
- m.Creator,
- now,
- m.ReleaseVersion,
- m.Namespace,
- sequence,
- m.Engine,
- m.Type,
- "PENDING",
- m.Version,
- m.Description,
- statement,
- prevSchemaBuf.String(),
- prevSchemaBuf.String(),
- 0,
- m.IssueID,
- m.Payload,
- )
- if err != nil {
- return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)
- }
- } else if dbType == db.Snowflake {
- maxIDQuery := "SELECT MAX(id)+1 FROM bytebase.public.migration_history"
- rows, err := tx.QueryContext(ctx, maxIDQuery)
- if err != nil {
- return -1, "", FormatErrorWithQuery(err, maxIDQuery)
- }
- defer rows.Close()
- var id sql.NullInt64
- for rows.Next() {
- if err := rows.Scan(
- &id,
- ); err != nil {
- return -1, "", FormatErrorWithQuery(err, maxIDQuery)
- }
- }
- if err := rows.Err(); err != nil {
- return -1, "", FormatErrorWithQuery(err, maxIDQuery)
- }
- if id.Valid {
- insertedID = id.Int64
- } else {
- insertedID = 1
- }
-
- _, err = tx.ExecContext(ctx, args.InsertHistoryQuery,
- m.Creator,
- m.Creator,
- m.ReleaseVersion,
- m.Namespace,
- sequence,
- m.Engine,
- m.Type,
- m.Version,
- m.Description,
- statement,
- prevSchemaBuf.String(),
- prevSchemaBuf.String(),
- m.IssueID,
- m.Payload,
- )
- if err != nil {
- return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)
- }
- } else {
- res, err := tx.ExecContext(ctx, args.InsertHistoryQuery,
- m.Creator,
- m.Creator,
- m.ReleaseVersion,
- m.Namespace,
- sequence,
- m.Engine,
- m.Type,
- m.Version,
- m.Description,
- statement,
- prevSchemaBuf.String(),
- prevSchemaBuf.String(),
- m.IssueID,
- m.Payload,
- )
-
- if err != nil {
- return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)
- }
-
- insertedID, err = res.LastInsertId()
- if err != nil {
- return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)
- }
+ insertedID, err := args.InsertPendingHistoryFunc(tx, sequence, prevSchemaBuf.String())
+ if err != nil {
+ return -1, "", err
}
if err := tx.Commit(); err != nil {