Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: refine setLineForPGCreateTableStmt (#2341)
rebelice164535cec568
diff --git a/plugin/parser/set_line_for_create_table_test.go b/plugin/parser/set_line_for_create_table_test.goindex 12903420da..3fd2c44858 100644--- a/plugin/parser/set_line_for_create_table_test.go+++ b/plugin/parser/set_line_for_create_table_test.go@@ -61,6 +61,18 @@ func TestPGCreateTableSetLine(t *testing.T) {columnLineList: []int{3, 4, 4},constraintLineList: []int{},},+ {+ statement: `-- complex example+ CREATE TABLE t(+ a int PRIMARY KEY,+ name varchar(255) DEFAULT 'UNIQUE on (a, b, c)(',+ UNIQUE(a, name),+ UNIQUE(name)+ )+ `,+ columnLineList: []int{3, 4},+ constraintLineList: []int{5, 6},+ },}for _, test := range tests {diff --git a/plugin/parser/tokenizer.go b/plugin/parser/tokenizer.goindex 15162411a4..7d455b7b8c 100644--- a/plugin/parser/tokenizer.go+++ b/plugin/parser/tokenizer.go@@ -40,8 +40,8 @@ func newTokenizer(statement string) *tokenizer {return t}-// setLineForCreateTableStmt sets the line for columns and table constraints in CREATE TABLE statements.-func (t *tokenizer) setLineForCreateTableStmt(node *ast.CreateTableStmt) error {+// setLineForPGCreateTableStmt sets the line for columns and table constraints in CREATE TABLE statements.+func (t *tokenizer) setLineForPGCreateTableStmt(node *ast.CreateTableStmt) error {// We assume that the parser will parse the columns and table constraints according to the order of the raw SQL statements// and the identifiers don't equal any keywords in CREATE TABLE statements.// If it breaks our assumption, we set the line for columns and table constraints to the first line of the CREATE TABLE statement.@@ -72,6 +72,10 @@ func (t *tokenizer) setLineForCreateTableStmt(node *ast.CreateTableStmt) error {case '\n':t.line++t.skip(1)+ case '\'':+ if err := t.scanString('\''); err != nil {+ return err+ }case '(':parentheses++t.skip(1)@@ -95,6 +99,17 @@ func (t *tokenizer) setLineForCreateTableStmt(node *ast.CreateTableStmt) error {}t.skip(1)case ',':+ // e.g. CREATE TABLE t(+ // a int,+ // b int,+ // UNIQUE(a, b),+ // UNIQUE(b)+ // )+ // We don't need to consider the ',' in UNIQUE(a, b)+ if parentheses > 1 {+ t.skip(1)+ continue+ }def := strings.ToLower(t.getString(startPos, t.pos()-startPos))if columnPos < len(node.ColumnList) &&strings.Contains(def, strings.ToLower(node.ColumnList[columnPos].ColumnName)) {diff --git a/plugin/parser/utils.go b/plugin/parser/utils.goindex 54669b7d48..b18c0c965a 100644--- a/plugin/parser/utils.go+++ b/plugin/parser/utils.go@@ -27,7 +27,7 @@ func SetLineForCreateTableStmt(engineType EngineType, node *ast.CreateTableStmt)switch engineType {case Postgres:t := newTokenizer(node.Text())- return t.setLineForCreateTableStmt(node)+ return t.setLineForPGCreateTableStmt(node)default:return errors.Errorf("engine type is not supported: %s", engineType)}