Skip to content

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

fix(strings): column names and constraint names are in different namespaces (#10452)

rebelicea03c2774edbe
diff --git a/backend/plugin/parser/tidb/strings.go b/backend/plugin/parser/tidb/strings.go
index d553c635aa..5065061eda 100644
--- a/backend/plugin/parser/tidb/strings.go
+++ b/backend/plugin/parser/tidb/strings.go
@@ -675,8 +675,17 @@ func (r *rewriter) EnterTableConstraintDef(ctx *parser.TableConstraintDefContext
return
}
- actions, exists := r.actions[secondName]
- if !exists {
+ var constraintActions []StringsManipulatorAction
+ if actions, exists := r.actions[secondName]; exists {
+ // column name and constraint name are in the different namespace for tidb
+ for _, action := range actions {
+ switch action := action.(type) {
+ case *StringsManipulatorActionDropTableConstraint, *StringsManipulatorActionModifyTableConstraint:
+ constraintActions = append(constraintActions, action)
+ }
+ }
+ }
+ if len(constraintActions) == 0 {
// We need to remain the original constraint definition with comments.
stop := getIndexUntilNewLine(ctx.GetStop().GetTokenIndex()+1, ctx.GetParser().GetTokenStream())
r.tableConstraints = append(r.tableConstraints, ctx.GetParser().GetTokenStream().GetTextFromInterval(
@@ -688,7 +697,7 @@ func (r *rewriter) EnterTableConstraintDef(ctx *parser.TableConstraintDefContext
return
}
// column name and constraint name are in the different namespace for tidb
- for _, action := range actions {
+ for _, action := range constraintActions {
switch action := action.(type) {
case *StringsManipulatorActionDropTableConstraint:
return
@@ -795,8 +804,21 @@ func (r *rewriter) EnterColumnDef(ctx *parser.ColumnDefContext) {
return
}
- actions, exists := r.actions[columnName]
- if !exists {
+ var columnActions []StringsManipulatorAction
+ if actions, exists := r.actions[columnName]; exists {
+ // column name and constraint name are in the different namespace for tidb
+ for _, action := range actions {
+ switch action := action.(type) {
+ case *StringsManipulatorActionDropColumn,
+ *StringsManipulatorActionModifyColumnType,
+ *StringsManipulatorActionDropColumnOption,
+ *StringsManipulatorActionAddColumnOption,
+ *StringsManipulatorActionModifyColumnOption:
+ columnActions = append(columnActions, action)
+ }
+ }
+ }
+ if len(columnActions) == 0 {
// We need to remain the original column definition with comments.
stop := getIndexUntilNewLine(ctx.GetStop().GetTokenIndex()+1, ctx.GetParser().GetTokenStream())
r.columnDefines = append(r.columnDefines, ctx.GetParser().GetTokenStream().GetTextFromInterval(
@@ -808,7 +830,7 @@ func (r *rewriter) EnterColumnDef(ctx *parser.ColumnDefContext) {
return
}
actionsMap := make(map[StringsManipulatorActionType][]StringsManipulatorAction)
- for _, action := range actions {
+ for _, action := range columnActions {
if action.GetType() == StringsManipulatorActionTypeDropColumn {
// drop column action is special, we need to handle it first
return
diff --git a/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml b/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml
index eace6796b0..5c2b86dedd 100644
--- a/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml
+++ b/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml
@@ -4,7 +4,8 @@
`id` int /*T![auto_rand] AUTO_RANDOM(6, 54) */,
`name` varchar(10) NOT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
- KEY `idx_name` (`name`(5))
+ KEY `idx_name` (`name`(5)),
+ UNIQUE KEY `name` (`name`)
);
actions:
- type: modifyColumnType
@@ -12,13 +13,20 @@
- t
- id
- varchar(10)
+ - type: addColumnOption
+ arguments:
+ - t
+ - name
+ - comment
+ - COMMENT 'this is comment'
want: |
-- Table structure for `t`
CREATE TABLE `t` (
`id` varchar(10) /*T![auto_rand] AUTO_RANDOM(6, 54) */,
- `name` varchar(10) NOT NULL,
+ `name` varchar(10) NOT NULL COMMENT 'this is comment',
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
- KEY `idx_name` (`name`(5))
+ KEY `idx_name` (`name`(5)),
+ UNIQUE KEY `name` (`name`)
);
- input: |-
-- Table structure for `t`