Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(mysql): use MariaDB-native CHECK_CONSTRAINTS query in schema sync (#20643)
Vincent Huang8a00ddb367fa
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.goindex 870f7c3f6e..0cc0fc440a 100644--- a/backend/plugin/db/mysql/sync.go+++ b/backend/plugin/db/mysql/sync.go@@ -185,6 +185,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetadatLeast8_0_13 := semVersion.GE(semver.MustParse("8.0.13"))atLeast8_0_16 := semVersion.GE(semver.MustParse("8.0.16"))atLeast5_7_0 := semVersion.GE(semver.MustParse("5.7.0"))+ isMariaDB := strings.Contains(rest, "MariaDB")// Query index info.indexMap := make(map[db.TableKey]map[string]*storepb.IndexMetadata)@@ -208,7 +209,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad// https://dev.mysql.com/doc/refman/8.0/en/information-schema-statistics-table.html// MariaDB doesn't have the EXPRESSION column.// https://mariadb.com/docs/server/ref/mdb/information-schema/STATISTICS- if atLeast8_0_13 && !strings.Contains(rest, "MariaDB") {+ if atLeast8_0_13 && !isMariaDB {indexQuery = `SELECTTABLE_NAME,@@ -390,17 +391,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad// Check constraints info.checkMap := make(map[db.TableKey][]*storepb.CheckConstraintMetadata)if atLeast8_0_16 {- checkQuery := `- SELECT- tc.TABLE_NAME,- cc.CONSTRAINT_NAME,- cc.CHECK_CLAUSE- FROM information_schema.CHECK_CONSTRAINTS cc- JOIN information_schema.TABLE_CONSTRAINTS tc- ON cc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME- AND cc.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA- WHERE tc.CONSTRAINT_TYPE = 'CHECK' AND tc.TABLE_SCHEMA = ?- `+ checkQuery := getCheckConstraintQuery(isMariaDB)checkRows, err := d.db.QueryContext(ctx, checkQuery, d.databaseName)if err != nil {return nil, util.FormatErrorWithQuery(err, checkQuery)@@ -922,6 +913,34 @@ func unescapeCheckClause(clause string) string {return strings.ReplaceAll(clause, `\'`, `'`)}+func getCheckConstraintQuery(isMariaDB bool) string {+ if isMariaDB {+ // MariaDB includes TABLE_NAME in CHECK_CONSTRAINTS. Joining TABLE_CONSTRAINTS+ // by constraint name/schema is both slower and incorrect because MariaDB check+ // names are scoped to a table, not the whole schema.+ return `+ SELECT+ TABLE_NAME,+ CONSTRAINT_NAME,+ CHECK_CLAUSE+ FROM information_schema.CHECK_CONSTRAINTS+ WHERE CONSTRAINT_SCHEMA = ?+ `+ }++ return `+ SELECT+ tc.TABLE_NAME,+ cc.CONSTRAINT_NAME,+ cc.CHECK_CLAUSE+ FROM information_schema.CHECK_CONSTRAINTS cc+ JOIN information_schema.TABLE_CONSTRAINTS tc+ ON cc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME+ AND cc.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA+ WHERE tc.CONSTRAINT_TYPE = 'CHECK' AND tc.TABLE_SCHEMA = ?+ `+}+// stripReturnsCharset removes the " CHARSET ... [COLLATE ...]" attributes that// SHOW CREATE FUNCTION appends to the RETURNS clause. The search is anchored to// the RETURNS line because the parameter list is preserved verbatim from thediff --git a/backend/plugin/db/mysql/sync_test.go b/backend/plugin/db/mysql/sync_test.goindex 9158d08a4e..99a7a82167 100644--- a/backend/plugin/db/mysql/sync_test.go+++ b/backend/plugin/db/mysql/sync_test.go@@ -2,6 +2,7 @@ package mysqlimport ("database/sql"+ "strings""testing""github.com/stretchr/testify/require"@@ -470,6 +471,27 @@ func TestUnescapeCheckClause(t *testing.T) {}}+func TestGetCheckConstraintQuery(t *testing.T) {+ t.Run("MariaDB uses native table name column", func(t *testing.T) {+ query := strings.ToUpper(getCheckConstraintQuery(true))++ require.Contains(t, query, "TABLE_NAME")+ require.Contains(t, query, "FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS")+ require.Contains(t, query, "WHERE CONSTRAINT_SCHEMA = ?")+ require.NotContains(t, query, "JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS")+ require.NotContains(t, query, "TC.TABLE_SCHEMA")+ })++ t.Run("MySQL keeps table constraints join", func(t *testing.T) {+ query := strings.ToUpper(getCheckConstraintQuery(false))++ require.Contains(t, query, "TC.TABLE_NAME")+ require.Contains(t, query, "JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS")+ require.Contains(t, query, "TC.CONSTRAINT_TYPE = 'CHECK'")+ require.Contains(t, query, "TC.TABLE_SCHEMA = ?")+ })+}+func TestStripReturnsCharset(t *testing.T) {testCases := []struct {name string