diff --git a/IHP/IDE/SchemaDesigner/Compiler.hs b/IHP/IDE/SchemaDesigner/Compiler.hs index 9062836b0..494bad13c 100644 --- a/IHP/IDE/SchemaDesigner/Compiler.hs +++ b/IHP/IDE/SchemaDesigner/Compiler.hs @@ -81,6 +81,8 @@ compilePostgresType (PNumeric (Just precision) Nothing) = "NUMERIC(" <> show pre compilePostgresType (PNumeric Nothing _) = "NUMERIC" compilePostgresType (PVaryingN limit) = "CHARACTER VARYING(" <> show limit <> ")" compilePostgresType (PCharacterN length) = "CHARACTER(" <> show length <> ")" +compilePostgresType PSerial = "SERIAL" +compilePostgresType PBigserial = "BIGSERIAL" compilePostgresType (PCustomType theType) = theType compileIdentifier :: _ -> Text diff --git a/IHP/IDE/SchemaDesigner/Parser.hs b/IHP/IDE/SchemaDesigner/Parser.hs index c1707f36f..3fbdeb3fa 100644 --- a/IHP/IDE/SchemaDesigner/Parser.hs +++ b/IHP/IDE/SchemaDesigner/Parser.hs @@ -162,6 +162,8 @@ sqlType = choice , numeric , character , varchar + , serial + , bigserial , customType ] where @@ -264,6 +266,14 @@ sqlType = choice Just l -> pure (PCharacterN l) _ -> Prelude.fail "Failed to parse CHARACTER VARYING(..) expression" + serial = do + try (symbol' "SERIAL") + pure PSerial + + bigserial = do + try (symbol' "BIGSERIAL") + pure PBigserial + customType = do theType <- try (takeWhile1P (Just "Custom type") (\c -> isAlphaNum c || c == '_')) pure (PCustomType theType) diff --git a/IHP/IDE/SchemaDesigner/Types.hs b/IHP/IDE/SchemaDesigner/Types.hs index d87aa9dc2..f58700503 100644 --- a/IHP/IDE/SchemaDesigner/Types.hs +++ b/IHP/IDE/SchemaDesigner/Types.hs @@ -75,5 +75,7 @@ data PostgresType | PNumeric { precision :: Maybe Int, scale :: Maybe Int } | PVaryingN Int | PCharacterN Int + | PSerial + | PBigserial | PCustomType Text deriving (Eq, Show) \ No newline at end of file diff --git a/IHP/IDE/SchemaDesigner/View/Columns/Edit.hs b/IHP/IDE/SchemaDesigner/View/Columns/Edit.hs index f26967e95..200f7654e 100644 --- a/IHP/IDE/SchemaDesigner/View/Columns/Edit.hs +++ b/IHP/IDE/SchemaDesigner/View/Columns/Edit.hs @@ -111,6 +111,8 @@ typeSelector postgresType enumNames = [hsx| {option selected "DATE" "Date"} {option selected "BINARY" "Binary"} {option selected "Time" "Time"} + {option selected "SERIAL" "Serial"} + {option selected "BIGSERIAL" "Bigserial"} {forEach enumNames renderEnumType} |] diff --git a/Test/IDE/SchemaDesigner/CompilerSpec.hs b/Test/IDE/SchemaDesigner/CompilerSpec.hs index 37f26dae1..0a6dcfc7a 100644 --- a/Test/IDE/SchemaDesigner/CompilerSpec.hs +++ b/Test/IDE/SchemaDesigner/CompilerSpec.hs @@ -255,4 +255,22 @@ tests = do ] , constraints = [ UniqueConstraint { columnNames = [ "user_id", "follower_id" ] } ] } - compileSql [statement] `shouldBe` sql \ No newline at end of file + compileSql [statement] `shouldBe` sql + + it "should compile a CREATE TABLE statement with a serial id" do + let sql = cs [plain|CREATE TABLE orders (\n id SERIAL PRIMARY KEY NOT NULL\n);\n|] + let statement = CreateTable + { name = "orders" + , columns = [ col { name = "id", columnType = PSerial, notNull = True, primaryKey = True} ] + , constraints = [] + } + compileSql [statement] `shouldBe` sql + + it "should compile a CREATE TABLE statement with a bigserial id" do + let sql = cs [plain|CREATE TABLE orders (\n id BIGSERIAL PRIMARY KEY NOT NULL\n);\n|] + let statement = CreateTable + { name = "orders" + , columns = [ col { name = "id", columnType = PBigserial, notNull = True, primaryKey = True} ] + , constraints = [] + } + compileSql [statement] `shouldBe` sql \ No newline at end of file diff --git a/Test/IDE/SchemaDesigner/ParserSpec.hs b/Test/IDE/SchemaDesigner/ParserSpec.hs index ee93d6012..28fc8e808 100644 --- a/Test/IDE/SchemaDesigner/ParserSpec.hs +++ b/Test/IDE/SchemaDesigner/ParserSpec.hs @@ -253,6 +253,20 @@ tests = do (evaluate (parseSql "CREATE TABLE user_followers (id UUID, UNIQUE());")) `shouldThrow` anyException pure () + it "should parse a CREATE TABLE statement with a serial id" do + parseSql "CREATE TABLE orders (\n id SERIAL PRIMARY KEY NOT NULL\n);\n" `shouldBe` CreateTable + { name = "orders" + , columns = [ col { name = "id", columnType = PSerial, notNull = True, primaryKey = True} ] + , constraints = [] + } + + it "should parse a CREATE TABLE statement with a bigserial id" do + parseSql "CREATE TABLE orders (\n id BIGSERIAL PRIMARY KEY NOT NULL\n);\n" `shouldBe` CreateTable + { name = "orders" + , columns = [ col { name = "id", columnType = PBigserial, notNull = True, primaryKey = True} ] + , constraints = [] + } + col :: Column col = Column { name = ""