Skip to content

Commit

Permalink
Allow for more value types in ALTER TABLE ALTER COLUMN SET DEFAULT st…
Browse files Browse the repository at this point in the history
…atement (#2101)
  • Loading branch information
derickl committed Dec 12, 2021
1 parent f99d9b3 commit 78a92a9
Show file tree
Hide file tree
Showing 3 changed files with 351 additions and 10 deletions.
13 changes: 12 additions & 1 deletion src/sqlfluff/dialects/dialect_postgres.py
Expand Up @@ -1272,7 +1272,18 @@ class AlterTableActionSegment(BaseSegment):
Sequence("COLLATE", Ref("QuotedLiteralSegment"), optional=True),
Sequence("USING", OneOf(Ref("ExpressionSegment")), optional=True),
),
Sequence("SET", "DEFAULT", Ref("ExpressionSegment")),
Sequence(
"SET",
"DEFAULT",
OneOf(
OneOf(
Ref("LiteralGrammar"),
Ref("FunctionSegment"),
Ref("BareFunctionSegment"),
Ref("ExpressionSegment"),
)
),
),
Sequence("DROP", "DEFAULT"),
Sequence(OneOf("SET", "DROP", optional=True), "NOT", "NULL"),
Sequence("DROP", "EXPRESSION", Sequence("IF", "EXISTS", optional=True)),
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/dialects/postgres/postgres_alter_table.sql
Expand Up @@ -25,6 +25,25 @@ ALTER TABLE foo
timestamp 'epoch' + foo_timestamp * interval '1 second',
ALTER COLUMN foo_timestamp SET DEFAULT now();

ALTER TABLE mytable ALTER date_column SET DEFAULT NOW();
ALTER TABLE mytable ALTER int_column SET DEFAULT 1;
ALTER TABLE mytable ALTER text_column SET DEFAULT 'value';
ALTER TABLE mytable ALTER bool_column SET DEFAULT false;
ALTER TABLE mytable ALTER other_column SET DEFAULT other_value;
ALTER TABLE mytable ALTER other_column SET DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE mytable ALTER other_column SET DEFAULT a_function(a_parameter);
ALTER TABLE mytable ALTER other_column SET DEFAULT a_function('a_parameter');
ALTER TABLE mytable ALTER other_column DROP DEFAULT;
ALTER TABLE IF EXISTS mytable ALTER date_column SET DEFAULT NOW();
ALTER TABLE IF EXISTS mytable ALTER int_column SET DEFAULT 1;
ALTER TABLE IF EXISTS mytable ALTER text_column SET DEFAULT 'value';
ALTER TABLE IF EXISTS mytable ALTER bool_column SET DEFAULT false;
ALTER TABLE IF EXISTS mytable ALTER other_column SET DEFAULT other_value;
ALTER TABLE IF EXISTS mytable ALTER other_column SET DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE IF EXISTS mytable ALTER other_column SET DEFAULT a_function(a_parameter);
ALTER TABLE IF EXISTS mytable ALTER other_column SET DEFAULT a_function('a_parameter');
ALTER TABLE IF EXISTS mytable ALTER other_column DROP DEFAULT;

ALTER TABLE distributors RENAME COLUMN address TO city;

ALTER TABLE distributors RENAME TO suppliers;
Expand Down Expand Up @@ -109,3 +128,4 @@ ALTER TABLE IF EXISTS ONLY public.test OWNER TO CURRENT_ROLE;
ALTER TABLE public.history ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME public.history_id_seq
);

0 comments on commit 78a92a9

Please sign in to comment.