Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion snowflake/parser/alter_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,22 @@ func (p *Parser) parseAlterTableDropAction(action *ast.AlterTableAction) error {
return nil

case kwPRIMARY:
// DROP PRIMARY KEY [CASCADE|RESTRICT]
// DROP PRIMARY KEY (cols)? [CASCADE|RESTRICT] — the column list is not
// in current docs but the legacy ANTLR parser tolerated it (real
// migrations use it), mirroring the UNIQUE/FOREIGN KEY forms. It was
// previously absorbed by the silent trailing-token drop; strict Parse
// (#303) made the gap visible.
p.advance() // consume PRIMARY
if _, err := p.expect(kwKEY); err != nil {
return err
}
action.Kind = ast.AlterTableDropConstraint
action.IsPrimaryKey = true
if p.cur.Type == '(' {
if err := p.skipParenthesized(); err != nil {
return err
}
}
p.parseDropCascadeRestrict(action)
return nil

Expand Down
17 changes: 17 additions & 0 deletions snowflake/parser/alter_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,20 @@ func TestAlterTable_LocTracking(t *testing.T) {
t.Errorf("stmt.Loc.End (%d) <= Start (%d)", stmt.Loc.End, stmt.Loc.Start)
}
}

// TestAlterTable_DropPrimaryKeyWithColumns covers the legacy-tolerated
// `DROP PRIMARY KEY (cols)` form (strict Parse #303 exposed that the column
// list was previously absorbed by the silent trailing-token drop).
func TestAlterTable_DropPrimaryKeyWithColumns(t *testing.T) {
file, err := Parse("ALTER TABLE CUSTOMER DROP PRIMARY KEY(ID);")
if err != nil {
t.Fatalf("parse error: %v", err)
}
stmt, ok := file.Stmts[0].(*ast.AlterTableStmt)
if !ok {
t.Fatalf("expected AlterTableStmt, got %T", file.Stmts[0])
}
if len(stmt.Actions) != 1 || stmt.Actions[0].Kind != ast.AlterTableDropConstraint || !stmt.Actions[0].IsPrimaryKey {
t.Fatalf("expected DROP PRIMARY KEY action, got %+v", stmt.Actions[0])
}
}
Loading