Skip to content

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

driver: fix FindMigrationHistoryList, use backtick to escape schema field for mysql.

spinningbotfbbbe3f13ec0
diff --git a/plugin/db/mysql/mysql.go b/plugin/db/mysql/mysql.go
index 55ebf5449a..fd05d3267b 100644
--- a/plugin/db/mysql/mysql.go
+++ b/plugin/db/mysql/mysql.go
@@ -500,9 +500,9 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
updated_ts,
namespace,
sequence,
- ` + "`engine`," + `
- ` + "`type`," + `
- ` + "`status`," + `
+ engine,
+ type,
+ status,
version,
description,
statement,
@@ -514,14 +514,14 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
VALUES (?, unix_timestamp(), ?, unix_timestamp(), ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, 0, ?, ?)
`
updateHistoryQuery := `
- UPDATE ` +
- `bytebase.migration_history ` +
- `SET
- ` + "`status` = 'DONE'," + `
- ` + "execution_duration = ?," + `
- ` + "`schema` = ?" + `
- WHERE id = ?
-`
+ UPDATE
+ bytebase.migration_history
+ SET
+ status = 'DONE',
+ execution_duration = ?,
+ ` + "`schema` = ?" + `
+ WHERE id = ?
+ `
args := util.MigrationExecutionArgs{
InsertHistoryQuery: insertHistoryQuery,
UpdateHistoryQuery: updateHistoryQuery,
@@ -531,7 +531,27 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
}
func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return util.FindMigrationHistoryList(ctx, db.MySQL, driver, find, "bytebase.")
+ baseQuery := `
+ SELECT
+ id,
+ created_by,
+ created_ts,
+ updated_by,
+ updated_ts,
+ namespace,
+ sequence,
+ engine,
+ type,
+ status,
+ version,
+ description,
+ statement,
+ ` + "`schema`," + `
+ execution_duration,
+ issue_id,
+ payload
+ FROM bytebase.migration_history `
+ return util.FindMigrationHistoryList(ctx, db.MySQL, driver, find, baseQuery)
}
// Dump and restore
diff --git a/plugin/db/pg/pg.go b/plugin/db/pg/pg.go
index 5086cfcd0e..11d9fbf9d1 100644
--- a/plugin/db/pg/pg.go
+++ b/plugin/db/pg/pg.go
@@ -593,7 +593,27 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
}
func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
- return util.FindMigrationHistoryList(ctx, db.Postgres, driver, find, "")
+ baseQuery := `
+ SELECT
+ id,
+ created_by,
+ created_ts,
+ updated_by,
+ updated_ts,
+ namespace,
+ sequence,
+ engine,
+ type,
+ status,
+ version,
+ description,
+ statement,
+ ` + `"schema",` + `
+ execution_duration,
+ issue_id,
+ payload
+ FROM migration_history `
+ return util.FindMigrationHistoryList(ctx, db.Postgres, driver, find, baseQuery)
}
// Dump and restore
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 44f46a063e..b7f9bee627 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -323,7 +323,7 @@ func findNextSequence(ctx context.Context, dbType db.Type, tx *sql.Tx, namespace
}
// FindMigrationHistoryList will find the list of migration history.
-func FindMigrationHistoryList(ctx context.Context, dbType db.Type, driver db.Driver, find *db.MigrationHistoryFind, tablePrefix string) ([]*db.MigrationHistory, error) {
+func FindMigrationHistoryList(ctx context.Context, dbType db.Type, driver db.Driver, find *db.MigrationHistoryFind, baseQuery string) ([]*db.MigrationHistory, error) {
sqldb, err := driver.GetDbConnection(ctx, bytebaseDatabase)
if err != nil {
return nil, err
@@ -345,27 +345,7 @@ func FindMigrationHistoryList(ctx context.Context, dbType db.Type, driver db.Dri
queryParams.AddParam("version", *v)
}
- var query = `
- SELECT
- id,
- created_by,
- created_ts,
- updated_by,
- updated_ts,
- namespace,
- sequence,
- engine,
- type,
- status,
- version,
- description,
- statement,
- ` + `"schema",` + `
- execution_duration,
- issue_id,
- payload
- FROM ` +
- tablePrefix + `migration_history ` +
+ var query = baseQuery +
queryParams.QueryString() +
`ORDER BY created_ts DESC`
if v := find.Limit; v != nil {