Skip to content

Commit

Permalink
Parser supports more variants of CREATE SEQUENCE statements now
Browse files Browse the repository at this point in the history
Also allow parsing of ALTER SEQUENCE statements
  • Loading branch information
mpscholten committed Jan 10, 2022
1 parent 7b02f55 commit e3a5c8f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
36 changes: 35 additions & 1 deletion IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ statement = do
let create = try createExtension <|> try (StatementCreateTable <$> createTable) <|> try createIndex <|> try createFunction <|> try createTrigger <|> try createEnumType <|> try createPolicy <|> try createSequence
let alter = do
lexeme "ALTER"
alterTable <|> alterType
alterTable <|> alterType <|> alterSequence
s <- setStatement <|> create <|> alter <|> selectStatement <|> try dropTable <|> try dropIndex <|> try dropPolicy <|> dropType <|> commentStatement <|> comment
space
pure s
Expand Down Expand Up @@ -572,6 +572,11 @@ alterType = do
typeName <- qualifiedIdentifier
addValue typeName

alterSequence = do
lexeme "SEQUENCE"
raw <- cs <$> someTill (anySingle) (char ';')
pure UnknownStatement { raw = "ALTER SEQUENCE " <> raw };

-- | ALTER TABLE users ALTER COLUMN email DROP NOT NULL;
-- ALTER TABLE users ALTER COLUMN email SET NOT NULL;
-- ALTER TABLE users ALTER COLUMN email SET DEFAULT 'value';
Expand Down Expand Up @@ -734,6 +739,35 @@ createSequence = do
lexeme "CREATE"
lexeme "SEQUENCE"
name <- qualifiedIdentifier

-- We accept all the following SEQUENCE attributes, but don't save them
-- This is mostly to void issues in migrations when parsing the pg_dump output
optional do
lexeme "AS"
sqlType

optional do
lexeme "START"
lexeme "WITH"
expression

optional do
lexeme "INCREMENT"
lexeme "BY"
expression

optional do
lexeme "NO"
lexeme "MINVALUE"

optional do
lexeme "NO"
lexeme "MAXVALUE"

optional do
lexeme "CACHE"
expression

char ';'
pure CreateSequence { name }

Expand Down
16 changes: 16 additions & 0 deletions Test/IDE/SchemaDesigner/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,18 @@ $$;

it "should parse 'CREATE SEQUENCE ..' statements with qualified name" do
parseSql "CREATE SEQUENCE public.a;" `shouldBe` CreateSequence { name = "a" }

it "should parse 'CREATE SEQUENCE .. AS .. START WITH .. INCREMENT BY .. NO MINVALUE NO MAXVALUE CACHE ..;'" do
let sql = [trimming|
CREATE SEQUENCE public.a
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
|]
parseSql sql `shouldBe` CreateSequence { name = "a" }

it "should parse 'SET' statements" do
parseSql "SET statement_timeout = 0;" `shouldBe` Set { name = "statement_timeout", value = IntExpression 0 }
Expand Down Expand Up @@ -735,6 +747,10 @@ COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UU
, arguments = [TextExpression "hello"]
}

it "should parse 'ALTER SEQUENCE ..' statements" do
let sql = cs [plain|ALTER SEQUENCE public.a OWNED BY public.b.serial_number;|]
parseSql sql `shouldBe` UnknownStatement { raw = "ALTER SEQUENCE public.a OWNED BY public.b.serial_number" }

col :: Column
col = Column
{ name = ""
Expand Down

0 comments on commit e3a5c8f

Please sign in to comment.