Skip to content

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

fix: reconcile view def (#11330)

h3n4l4e2e2152f1f1
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.go
index 66ecd81c33..89bda99cc9 100644
--- a/backend/plugin/db/mysql/sync.go
+++ b/backend/plugin/db/mysql/sync.go
@@ -55,7 +55,7 @@ var (
return strings.Join(l, ", ")
}()
- viewDefMatcher = regexp.MustCompile("CREATE ALGORITHM=(UNDEFINED|MERGE|TEMPTABLE) DEFINER=`([^`]+)`@`([^`]+)` SQL SECURITY DEFINER VIEW `([^`]+)` AS (?P<def>.+)")
+ viewDefMatcher = regexp.MustCompile("CREATE ALGORITHM=(UNDEFINED|MERGE|TEMPTABLE) DEFINER=`([^`]+)`@`([^`]+)` SQL SECURITY (DEFINER|INVOKER) VIEW `([^`]+)`( \\((`([^`]+)`)+\\))? AS (?P<def>.+)")
)
// SyncInstance syncs the instance.
@@ -317,7 +317,9 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
if err != nil {
return nil, err
}
- viewMap[key].Definition = def
+ if def != "" {
+ viewMap[key].Definition = def
+ }
}
// Query foreign key info.
@@ -504,16 +506,12 @@ func (driver *Driver) reconcileViewDefinition(ctx context.Context, databaseName,
if err := viewDefRows.Scan(&unused, &createStmt, &unused, &unused); err != nil {
return "", err
}
- viewDefMatching := viewDefMatcher.FindStringSubmatch(createStmt)
- if len(viewDefMatching) == 0 {
- slog.Warn("failed to match view definition", slog.String("query", query), slog.String("createStmt", createStmt))
- }
- // Get the def group
- for i, name := range viewDefMatcher.SubexpNames() {
- if name == "def" {
- return viewDefMatching[i], nil
- }
+ def, err := getViewDefFromCreateView(createStmt)
+ if err != nil {
+ slog.Warn("failed to get view definition", slog.String("viewName", viewName), slog.String("databaseName", databaseName), log.BBError(err))
+ return "", nil
}
+ return def, nil
}
if err := viewDefRows.Err(); err != nil {
return "", util.FormatErrorWithQuery(err, query)
@@ -522,6 +520,19 @@ func (driver *Driver) reconcileViewDefinition(ctx context.Context, databaseName,
return "", nil
}
+func getViewDefFromCreateView(createView string) (string, error) {
+ viewDefMatching := viewDefMatcher.FindStringSubmatch(createView)
+ if len(viewDefMatching) == 0 {
+ return "", errors.Errorf("failed to match view definition, %s", createView)
+ }
+ for i, name := range viewDefMatcher.SubexpNames() {
+ if name == "def" && i < len(viewDefMatching) {
+ return viewDefMatching[i], nil
+ }
+ }
+ return "", errors.Errorf("failed to match view definition, %s", createView)
+}
+
func (driver *Driver) listPartitionTables(ctx context.Context, databaseName string) (map[db.TableKey][]*storepb.TablePartitionMetadata, error) {
const query string = `
SELECT
diff --git a/backend/plugin/db/mysql/sync_test.go b/backend/plugin/db/mysql/sync_test.go
index b43719590c..7c53c98510 100644
--- a/backend/plugin/db/mysql/sync_test.go
+++ b/backend/plugin/db/mysql/sync_test.go
@@ -431,3 +431,25 @@ func TestSetColumnMetadataDefault(t *testing.T) {
a.Equal(tc.want, column, tc.name)
}
}
+
+func TestGetViewDefFromCreateView(t *testing.T) {
+ testCases := []struct {
+ stmt string
+ def string
+ }{
+ {
+ stmt: "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY INVOKER VIEW `actor_info` AS select `a`.`actor_id` AS `actor_id`,`a`.`first_name` AS `first_name`,`a`.`last_name` AS `last_name`,group_concat(distinct concat(`c`.`name`,': ',(select group_concat(`f`.`title` order by `f`.`title` ASC separator ', ') from ((`film` `f` join `film_category` `fc` on((`f`.`film_id` = `fc`.`film_id`))) join `film_actor` `fa` on((`f`.`film_id` = `fa`.`film_id`))) where ((`fc`.`category_id` = `c`.`category_id`) and (`fa`.`actor_id` = `a`.`actor_id`)))) order by `c`.`name` ASC separator '; ') AS `film_info` from (((`actor` `a` left join `film_actor` `fa` on((`a`.`actor_id` = `fa`.`actor_id`))) left join `film_category` `fc` on((`fa`.`film_id` = `fc`.`film_id`))) left join `category` `c` on((`fc`.`category_id` = `c`.`category_id`))) group by `a`.`actor_id`,`a`.`first_name`,`a`.`last_name`",
+ def: "select `a`.`actor_id` AS `actor_id`,`a`.`first_name` AS `first_name`,`a`.`last_name` AS `last_name`,group_concat(distinct concat(`c`.`name`,': ',(select group_concat(`f`.`title` order by `f`.`title` ASC separator ', ') from ((`film` `f` join `film_category` `fc` on((`f`.`film_id` = `fc`.`film_id`))) join `film_actor` `fa` on((`f`.`film_id` = `fa`.`film_id`))) where ((`fc`.`category_id` = `c`.`category_id`) and (`fa`.`actor_id` = `a`.`actor_id`)))) order by `c`.`name` ASC separator '; ') AS `film_info` from (((`actor` `a` left join `film_actor` `fa` on((`a`.`actor_id` = `fa`.`actor_id`))) left join `film_category` `fc` on((`fa`.`film_id` = `fc`.`film_id`))) left join `category` `c` on((`fc`.`category_id` = `c`.`category_id`))) group by `a`.`actor_id`,`a`.`first_name`,`a`.`last_name`",
+ },
+ {
+ stmt: "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY INVOKER VIEW `actor_info` (`id`) AS select idid from t",
+ def: "select idid from t",
+ },
+ }
+
+ for _, tc := range testCases {
+ got, err := getViewDefFromCreateView(tc.stmt)
+ require.NoError(t, err)
+ require.Equal(t, tc.def, got)
+ }
+}