Skip to content

Commit

Permalink
Merge pull request #301 from digitallyinduced/danielsonnenberg/serial…
Browse files Browse the repository at this point in the history
…-bigserial

Serial and Bigserial column type
  • Loading branch information
mpscholten authored Aug 5, 2020
2 parents 71cbe97 + 5919aeb commit 2d7ecd3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ sqlType = choice
, numeric
, character
, varchar
, serial
, bigserial
, customType
]
where
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions IHP/IDE/SchemaDesigner/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ data PostgresType
| PNumeric { precision :: Maybe Int, scale :: Maybe Int }
| PVaryingN Int
| PCharacterN Int
| PSerial
| PBigserial
| PCustomType Text
deriving (Eq, Show)
2 changes: 2 additions & 0 deletions IHP/IDE/SchemaDesigner/View/Columns/Edit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
</select>
|]
Expand Down
20 changes: 19 additions & 1 deletion Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,22 @@ tests = do
]
, constraints = [ UniqueConstraint { columnNames = [ "user_id", "follower_id" ] } ]
}
compileSql [statement] `shouldBe` sql
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
14 changes: 14 additions & 0 deletions Test/IDE/SchemaDesigner/ParserSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down

0 comments on commit 2d7ecd3

Please sign in to comment.