Skip to content

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

fix: don't use with clause for mysql (#3007)

Danny Xuda5e9aad9aff
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 25c8749568..bfa4272504 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -319,7 +319,12 @@ func EndMigration(ctx context.Context, executor MigrationExecutor, startedNs int
// Query will execute a readonly / SELECT query.
func Query(ctx context.Context, dbType db.Type, sqldb *sql.DB, statement string, limit int) ([]interface{}, error) {
// Limit SQL query result size.
- statement = getStatementWithResultLimit(statement, limit)
+ if dbType == db.MySQL {
+ // MySQL 5.7 doesn't support WITH clause.
+ statement = getMySQLStatementWithResultLimit(statement, limit)
+ } else {
+ statement = getStatementWithResultLimit(statement, limit)
+ }
// Not all sql engines support ReadOnly flag, so we will use tx rollback semantics to enforce readonly.
readOnly := true
@@ -430,6 +435,18 @@ func getStatementWithResultLimit(stmt string, limit int) string {
return stmt
}
+func getMySQLStatementWithResultLimit(stmt string, limit int) string {
+ stmt = strings.TrimRight(stmt, " \n\t;")
+ if !strings.HasPrefix(stmt, "EXPLAIN") {
+ limitPart := ""
+ if limit > 0 {
+ limitPart = fmt.Sprintf(" LIMIT %d", limit)
+ }
+ return fmt.Sprintf("SELECT * FROM (%s) result%s;", stmt, limitPart)
+ }
+ return stmt
+}
+
// FindMigrationHistoryList will find the list of migration history.
func FindMigrationHistoryList(ctx context.Context, findMigrationHistoryListQuery string, queryParams []interface{}, driver db.Driver, database string) ([]*db.MigrationHistory, error) {
// To support `pg` option, the util layer will not know which database where `migration_history` table is,
diff --git a/plugin/db/util/driverutil_test.go b/plugin/db/util/driverutil_test.go
index b47b11b09c..444a1be685 100644
--- a/plugin/db/util/driverutil_test.go
+++ b/plugin/db/util/driverutil_test.go
@@ -124,6 +124,57 @@ func TestGetStatementWithResultLimit(t *testing.T) {
}
}
+func TestGetMySQLStatementWithResultLimit(t *testing.T) {
+ tests := []struct {
+ sqlStatement string
+ limit int
+ want string
+ }{
+ {
+ sqlStatement: " seLeCT * FROM test;",
+ limit: 123,
+ want: "SELECT * FROM ( seLeCT * FROM test) result LIMIT 123;",
+ },
+ {
+ sqlStatement: " seLeCT * FROM test;",
+ limit: 0,
+ want: "SELECT * FROM ( seLeCT * FROM test) result;",
+ },
+ {
+ sqlStatement: " \n \r SELEct * from test ",
+ limit: 100,
+ want: "SELECT * FROM ( \n \r SELEct * from test) result LIMIT 100;",
+ },
+ {
+ sqlStatement: "SELECT\n*\nFROM\ntest ;\n",
+ limit: 100,
+ want: "SELECT * FROM (SELECT\n*\nFROM\ntest) result LIMIT 100;",
+ },
+ {
+ sqlStatement: "SELECT\n*\nFROM\ntest ;;;\n",
+ limit: 100,
+ want: "SELECT * FROM (SELECT\n*\nFROM\ntest) result LIMIT 100;",
+ },
+ {
+ sqlStatement: "SELECT\n*\nFROM\n`test;` ;;;\n",
+ limit: 100,
+ want: "SELECT * FROM (SELECT\n*\nFROM\n`test;`) result LIMIT 100;",
+ },
+ {
+ sqlStatement: "EXPLAIN \n \r SELEct * from test ",
+ limit: 0,
+ want: "EXPLAIN \n \r SELEct * from test",
+ },
+ }
+
+ for _, test := range tests {
+ got := getMySQLStatementWithResultLimit(test.sqlStatement, test.limit)
+ if got != test.want {
+ t.Errorf("trimSQLStatement %q: got result %v, want %v.", test.sqlStatement, got, test.want)
+ }
+ }
+}
+
func TestApplyMultiStatements(t *testing.T) {
type testData struct {
statement string