Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: simplify advisor test utilities and remove redundant metadata logic (#18387)
Danny Xu9f3fbdd6b0a6
diff --git a/backend/plugin/advisor/mssql/mssql_rules_test.go b/backend/plugin/advisor/mssql/mssql_rules_test.goindex be696791dd..11f7870287 100644--- a/backend/plugin/advisor/mssql/mssql_rules_test.go+++ b/backend/plugin/advisor/mssql/mssql_rules_test.go@@ -9,7 +9,7 @@ import ()func TestMSSQLRules(t *testing.T) {- mssqlRules := []storepb.SQLReviewRule_Type{+ for _, rule := range []storepb.SQLReviewRule_Type{storepb.SQLReviewRule_STATEMENT_SELECT_NO_SELECT_ALL,storepb.SQLReviewRule_NAMING_TABLE,storepb.SQLReviewRule_NAMING_TABLE_NO_KEYWORD,@@ -31,17 +31,7 @@ func TestMSSQLRules(t *testing.T) {storepb.SQLReviewRule_STATEMENT_DISALLOW_CROSS_DB_QUERIES,storepb.SQLReviewRule_STATEMENT_WHERE_DISALLOW_FUNCTIONS_AND_CALCULATIONS,storepb.SQLReviewRule_INDEX_NOT_REDUNDANT,+ } {+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_MSSQL, false /* record */)}-- for _, rule := range mssqlRules {- _, needMockData := advisorNeedMockData[rule]- advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_MSSQL, needMockData, false /* record */)- }-}--// Add SQL review type here if you need metadata for test.-var advisorNeedMockData = map[storepb.SQLReviewRule_Type]bool{- storepb.SQLReviewRule_STATEMENT_DISALLOW_CROSS_DB_QUERIES: true,- storepb.SQLReviewRule_SCHEMA_BACKWARD_COMPATIBILITY: true,- storepb.SQLReviewRule_INDEX_NOT_REDUNDANT: true,}diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.goindex 58f35a8863..2fd8930d28 100644--- a/backend/plugin/advisor/mysql/mysql_rules_test.go+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go@@ -8,7 +8,7 @@ import ()func TestMySQLRules(t *testing.T) {- mysqlRules := []storepb.SQLReviewRule_Type{+ for _, rule := range []storepb.SQLReviewRule_Type{// storepb.SQLReviewRule_ENGINE_MYSQL_USE_INNODB enforce the innodb engine.storepb.SQLReviewRule_ENGINE_MYSQL_USE_INNODB,@@ -163,9 +163,7 @@ func TestMySQLRules(t *testing.T) {storepb.SQLReviewRule_SYSTEM_FUNCTION_DISALLOW_CREATE,// storepb.SQLReviewRule_SYSTEM_FUNCTION_DISALLOWED_LIST enforce the function disallow list.storepb.SQLReviewRule_SYSTEM_FUNCTION_DISALLOWED_LIST,- }-- for _, rule := range mysqlRules {- advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_MYSQL, false, false /* record */)+ } {+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_MYSQL, false /* record */)}}diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.goindex b27e3c3bac..6874fed3c0 100644--- a/backend/plugin/advisor/oracle/oracle_rules_test.go+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go@@ -9,7 +9,7 @@ import ()func TestOracleRules(t *testing.T) {- oracleRules := []storepb.SQLReviewRule_Type{+ for _, rule := range []storepb.SQLReviewRule_Type{storepb.SQLReviewRule_TABLE_REQUIRE_PK,storepb.SQLReviewRule_TABLE_NO_FOREIGN_KEY,storepb.SQLReviewRule_NAMING_TABLE,@@ -31,9 +31,7 @@ func TestOracleRules(t *testing.T) {storepb.SQLReviewRule_NAMING_IDENTIFIER_CASE,storepb.SQLReviewRule_TABLE_COMMENT,storepb.SQLReviewRule_COLUMN_COMMENT,- }-- for _, rule := range oracleRules {- advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_ORACLE, false, false /* record */)+ } {+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_ORACLE, false /* record */)}}diff --git a/backend/plugin/advisor/pg/pg_rules_test.go b/backend/plugin/advisor/pg/pg_rules_test.goindex 7ab82182e2..512b9286d8 100644--- a/backend/plugin/advisor/pg/pg_rules_test.go+++ b/backend/plugin/advisor/pg/pg_rules_test.go@@ -8,7 +8,7 @@ import ()func TestPostgreSQLRules(t *testing.T) {- postgresRules := []storepb.SQLReviewRule_Type{+ for _, rule := range []storepb.SQLReviewRule_Type{storepb.SQLReviewRule_BUILTIN_PRIOR_BACKUP_CHECK,storepb.SQLReviewRule_SYSTEM_CHARSET_ALLOWLIST,storepb.SQLReviewRule_SYSTEM_COLLATION_ALLOWLIST,@@ -61,9 +61,7 @@ func TestPostgreSQLRules(t *testing.T) {storepb.SQLReviewRule_TABLE_NO_FOREIGN_KEY,storepb.SQLReviewRule_TABLE_REQUIRE_PK,storepb.SQLReviewRule_NAMING_INDEX_UK,- }-- for _, rule := range postgresRules {- advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_POSTGRES, true /* needMetaData */, false /* record */)+ } {+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_POSTGRES, false /* record */)}}diff --git a/backend/plugin/advisor/snowflake/snowflake_rules_test.go b/backend/plugin/advisor/snowflake/snowflake_rules_test.goindex 8cc1349722..9e17fa3c35 100644--- a/backend/plugin/advisor/snowflake/snowflake_rules_test.go+++ b/backend/plugin/advisor/snowflake/snowflake_rules_test.go@@ -9,7 +9,7 @@ import ()func TestSnowflakeRules(t *testing.T) {- snowflakeRules := []storepb.SQLReviewRule_Type{+ for _, rule := range []storepb.SQLReviewRule_Type{storepb.SQLReviewRule_NAMING_TABLE,storepb.SQLReviewRule_TABLE_REQUIRE_PK,storepb.SQLReviewRule_TABLE_NO_FOREIGN_KEY,@@ -24,9 +24,7 @@ func TestSnowflakeRules(t *testing.T) {storepb.SQLReviewRule_STATEMENT_SELECT_NO_SELECT_ALL,storepb.SQLReviewRule_TABLE_DROP_NAMING_CONVENTION,storepb.SQLReviewRule_SCHEMA_BACKWARD_COMPATIBILITY,- }-- for _, rule := range snowflakeRules {- advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_SNOWFLAKE, false, false /* record */)+ } {+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_SNOWFLAKE, false /* record */)}}diff --git a/backend/plugin/advisor/tidb/tidb_rules_test.go b/backend/plugin/advisor/tidb/tidb_rules_test.goindex f8ac927e67..d9fe8211a6 100644--- a/backend/plugin/advisor/tidb/tidb_rules_test.go+++ b/backend/plugin/advisor/tidb/tidb_rules_test.go@@ -8,7 +8,7 @@ import ()func TestTiDBRules(t *testing.T) {- tidbRules := []storepb.SQLReviewRule_Type{+ for _, rule := range []storepb.SQLReviewRule_Type{// storepb.SQLReviewRule_NAMING_TABLE enforce the table name format.storepb.SQLReviewRule_NAMING_TABLE,// storepb.SQLReviewRule_NAMING_COLUMN enforce the column name format.@@ -113,9 +113,7 @@ func TestTiDBRules(t *testing.T) {// storepb.SQLReviewRule_SYSTEM_COLLATION_ALLOWLIST enforce the collation allowlist.storepb.SQLReviewRule_SYSTEM_COLLATION_ALLOWLIST,- }-- for _, rule := range tidbRules {- advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_TIDB, false, false /* record */)+ } {+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_TIDB, false /* record */)}}diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex fae1fd2af9..ebab716228 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -3,7 +3,6 @@ package advisorimport ("context""database/sql"- "fmt""io""os""path/filepath"@@ -156,7 +155,7 @@ type TestCase struct {}// RunSQLReviewRuleTest helps to test the SQL review rule.-func RunSQLReviewRuleTest(t *testing.T, rule storepb.SQLReviewRule_Type, dbType storepb.Engine, needMetaData bool, record bool) {+func RunSQLReviewRuleTest(t *testing.T, rule storepb.SQLReviewRule_Type, dbType storepb.Engine, record bool) {var tests []TestCasefileName := strings.Map(func(r rune) rune {@@ -179,49 +178,36 @@ func RunSQLReviewRuleTest(t *testing.T, rule storepb.SQLReviewRule_Type, dbTypesm := sheet.NewManager(nil)for i, tc := range tests {- // Add engine types here for mocked database metadata.+ // Set metadata and database-specific settings based on engine typevar schemaMetadata *storepb.DatabaseSchemaMetadatacurDB := "TEST_DB"- if needMetaData {- switch dbType {- case storepb.Engine_POSTGRES:- schemaMetadata = MockPostgreSQLDatabase- case storepb.Engine_MSSQL:- curDB = "master"- schemaMetadata = MockMSSQLDatabase- case storepb.Engine_MYSQL:- schemaMetadata = MockMySQLDatabase- default:- panic(fmt.Sprintf("%s doesn't have mocked metadata support", storepb.Engine_name[int32(dbType)]))- }- }-- // Use the schemaMetadata if available, otherwise use mock database for catalog creation- catalogMetadata := schemaMetadata- if catalogMetadata == nil {- if dbType == storepb.Engine_POSTGRES {- catalogMetadata = MockPostgreSQLDatabase- } else {- catalogMetadata = MockMySQLDatabase- }- }-isCaseSensitive := false- if dbType == storepb.Engine_POSTGRES {++ switch dbType {+ case storepb.Engine_POSTGRES:+ schemaMetadata = MockPostgreSQLDatabaseisCaseSensitive = true+ case storepb.Engine_MSSQL:+ schemaMetadata = MockMSSQLDatabase+ curDB = "master"+ case storepb.Engine_MYSQL:+ schemaMetadata = MockMySQLDatabase+ default:+ // Fallback to MySQL for engines without specific mock+ schemaMetadata = MockMySQLDatabase}// Create OriginalMetadata as DatabaseMetadata (read-only)// Clone to avoid mutations affecting future test cases- originalCatalogClone, ok := proto.Clone(catalogMetadata).(*storepb.DatabaseSchemaMetadata)- require.True(t, ok, "failed to clone catalog metadata")- originalMetadata := model.NewDatabaseMetadata(originalCatalogClone, nil, nil, dbType, isCaseSensitive)+ metadata, ok := proto.Clone(schemaMetadata).(*storepb.DatabaseSchemaMetadata)+ require.True(t, ok, "failed to clone metadata")+ originalMetadata := model.NewDatabaseMetadata(metadata, nil, nil, dbType, isCaseSensitive)// Create FinalMetadata as DatabaseMetadata (mutable for walk-through)// Clone to avoid mutations affecting future test cases- finalCatalogClone, ok := proto.Clone(catalogMetadata).(*storepb.DatabaseSchemaMetadata)- require.True(t, ok, "failed to clone catalog metadata")- finalMetadata := model.NewDatabaseMetadata(finalCatalogClone, nil, nil, dbType, isCaseSensitive)+ metadata, ok = proto.Clone(schemaMetadata).(*storepb.DatabaseSchemaMetadata)+ require.True(t, ok, "failed to clone metadata")+ finalMetadata := model.NewDatabaseMetadata(metadata, nil, nil, dbType, isCaseSensitive)sqlRule := &storepb.SQLReviewRule{Type: rule,