Skip to content

Commit 540106b

Browse files
committed
Added support for 'CREATE UNLOGGED TABLE' syntax in sql files
1 parent c0488e7 commit 540106b

File tree

12 files changed

+96
-18
lines changed

12 files changed

+96
-18
lines changed

IHP/IDE/SchemaDesigner/Compiler.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ compileSql statements = statements
2222
|> unlines
2323

2424
compileStatement :: Statement -> Text
25-
compileStatement (StatementCreateTable CreateTable { name, columns, primaryKeyConstraint, constraints }) = "CREATE TABLE " <> compileIdentifier name <> " (\n" <> intercalate ",\n" (map (\col -> " " <> compileColumn primaryKeyConstraint col) columns <> maybe [] ((:[]) . indent) (compilePrimaryKeyConstraint primaryKeyConstraint) <> map (indent . compileConstraint) constraints) <> "\n);"
25+
compileStatement (StatementCreateTable CreateTable { name, columns, primaryKeyConstraint, constraints, unlogged }) = "CREATE" <> (if unlogged then " UNLOGGED" else "") <> " TABLE " <> compileIdentifier name <> " (\n" <> intercalate ",\n" (map (\col -> " " <> compileColumn primaryKeyConstraint col) columns <> maybe [] ((:[]) . indent) (compilePrimaryKeyConstraint primaryKeyConstraint) <> map (indent . compileConstraint) constraints) <> "\n);"
2626
compileStatement CreateEnumType { name, values } = "CREATE TYPE " <> compileIdentifier name <> " AS ENUM (" <> intercalate ", " (values |> map TextExpression |> map compileExpression) <> ");"
2727
compileStatement CreateExtension { name, ifNotExists } = "CREATE EXTENSION " <> (if ifNotExists then "IF NOT EXISTS " else "") <> compileIdentifier name <> ";"
2828
compileStatement AddConstraint { tableName, constraint = UniqueConstraint { name = Nothing, columnNames } } = "ALTER TABLE " <> compileIdentifier tableName <> " ADD UNIQUE (" <> intercalate ", " columnNames <> ")" <> ";"

IHP/IDE/SchemaDesigner/Parser.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ createExtension = do
8888

8989
createTable = do
9090
lexeme "CREATE"
91+
unlogged <- isJust <$> optional (lexeme "UNLOGGED")
9192
lexeme "TABLE"
9293
name <- qualifiedIdentifier
9394

@@ -115,7 +116,7 @@ createTable = do
115116
_ -> Prelude.fail ("Primary key defined in both column and table constraints on table " <> cs name)
116117
_ -> Prelude.fail "Multiple columns with PRIMARY KEY constraint"
117118

118-
pure CreateTable { name, columns, primaryKeyConstraint, constraints }
119+
pure CreateTable { name, columns, primaryKeyConstraint, constraints, unlogged }
119120

120121
createEnumType = do
121122
lexeme "CREATE"

IHP/IDE/SchemaDesigner/SchemaOperations.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ addTable tableName list = list <> [StatementCreateTable CreateTable
2929
}]
3030
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
3131
, constraints = []
32+
, unlogged = False
3233
}]
3334

3435

IHP/IDE/SchemaDesigner/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ data CreateTable
8282
, columns :: [Column]
8383
, primaryKeyConstraint :: PrimaryKeyConstraint
8484
, constraints :: [Constraint]
85+
, unlogged :: !Bool
8586
}
8687
deriving (Eq, Show)
8788

Test/IDE/CodeGeneration/ControllerGenerator.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ tests = do
3030
]
3131
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
3232
, constraints = []
33+
, unlogged = False
3334
},
3435
StatementCreateTable CreateTable {
3536
name = "people"
@@ -63,6 +64,7 @@ tests = do
6364
]
6465
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
6566
, constraints = []
67+
, unlogged = False
6668
}
6769
]
6870
it "should build a controller with name \"pages\"" do

Test/IDE/CodeGeneration/MailGenerator.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tests = do
3131
]
3232
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
3333
, constraints = []
34+
, unlogged = False
3435
}
3536
]
3637
it "should build a mail with name \"PurchaseConfirmationMail\"" do

Test/IDE/CodeGeneration/ViewGenerator.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ tests = do
3131
]
3232
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
3333
, constraints = []
34+
, unlogged = False
3435
}
3536
]
3637
it "should build a view with name \"EditView\"" do

Test/IDE/SchemaDesigner/CompilerSpec.hs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Test.IDE.SchemaDesigner.ParserSpec (col, parseSql)
1515
tests = do
1616
describe "The Schema.sql Compiler" do
1717
it "should compile an empty CREATE TABLE statement" do
18-
compileSql [StatementCreateTable CreateTable { name = "users", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [] }] `shouldBe` "CREATE TABLE users (\n\n);\n"
18+
compileSql [StatementCreateTable CreateTable { name = "users", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [], unlogged = False }] `shouldBe` "CREATE TABLE users (\n\n);\n"
1919

2020
it "should compile a CREATE EXTENSION for the UUID extension" do
2121
compileSql [CreateExtension { name = "uuid-ossp", ifNotExists = True }] `shouldBe` "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";\n"
@@ -108,11 +108,12 @@ tests = do
108108
]
109109
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
110110
, constraints = []
111+
, unlogged = False
111112
}
112113
compileSql [statement] `shouldBe` sql
113114

114115
it "should compile a CREATE TABLE with quoted identifiers" do
115-
compileSql [StatementCreateTable CreateTable { name = "quoted name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [] }] `shouldBe` "CREATE TABLE \"quoted name\" (\n\n);\n"
116+
compileSql [StatementCreateTable CreateTable { name = "quoted name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [], unlogged = False }] `shouldBe` "CREATE TABLE \"quoted name\" (\n\n);\n"
116117

117118
it "should compile ALTER TABLE .. ADD FOREIGN KEY .. ON DELETE CASCADE" do
118119
let statement = AddConstraint
@@ -476,6 +477,7 @@ tests = do
476477
]
477478
, primaryKeyConstraint = PrimaryKeyConstraint []
478479
, constraints = []
480+
, unlogged = False
479481
}
480482
compileSql [statement] `shouldBe` sql
481483

@@ -527,6 +529,7 @@ tests = do
527529
]
528530
, primaryKeyConstraint = PrimaryKeyConstraint []
529531
, constraints = []
532+
, unlogged = False
530533
}
531534
compileSql [statement] `shouldBe` sql
532535

@@ -541,6 +544,7 @@ tests = do
541544
]
542545
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
543546
, constraints = [ UniqueConstraint { name = Nothing, columnNames = [ "user_id", "follower_id" ] } ]
547+
, unlogged = False
544548
}
545549
compileSql [statement] `shouldBe` sql
546550

@@ -551,6 +555,7 @@ tests = do
551555
, columns = [ col { name = "id", columnType = PSerial, notNull = True} ]
552556
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
553557
, constraints = []
558+
, unlogged = False
554559
}
555560
compileSql [statement] `shouldBe` sql
556561

@@ -561,6 +566,7 @@ tests = do
561566
, columns = [ col { name = "id", columnType = PBigserial, notNull = True} ]
562567
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
563568
, constraints = []
569+
, unlogged = False
564570
}
565571
compileSql [statement] `shouldBe` sql
566572

@@ -574,6 +580,7 @@ tests = do
574580
]
575581
, primaryKeyConstraint = PrimaryKeyConstraint ["order_id", "truck_id"]
576582
, constraints = []
583+
, unlogged = False
577584
}
578585
compileSql [statement] `shouldBe` sql
579586

@@ -584,6 +591,7 @@ tests = do
584591
, columns = [ col { name = "pay_by_quarter", columnType = PArray PInt } ]
585592
, primaryKeyConstraint = PrimaryKeyConstraint []
586593
, constraints = []
594+
, unlogged = False
587595
}
588596
compileSql [statement] `shouldBe` sql
589597

@@ -594,6 +602,7 @@ tests = do
594602
, columns = [ col { name = "pos", columnType = PPoint } ]
595603
, primaryKeyConstraint = PrimaryKeyConstraint []
596604
, constraints = []
605+
, unlogged = False
597606
}
598607
compileSql [statement] `shouldBe` sql
599608

@@ -604,6 +613,7 @@ tests = do
604613
, columns = [ col { name = "poly", columnType = PPolygon } ]
605614
, primaryKeyConstraint = PrimaryKeyConstraint []
606615
, constraints = []
616+
, unlogged = False
607617
}
608618
compileSql [statement] `shouldBe` sql
609619

@@ -777,12 +787,12 @@ tests = do
777787

778788
it "should compile a decimal default value with a type-cast" do
779789
let sql = "CREATE TABLE a (\n electricity_unit_price DOUBLE PRECISION DEFAULT 0.17::DOUBLE PRECISION NOT NULL\n);\n"
780-
let statement = StatementCreateTable CreateTable { name = "a", columns = [Column {name = "electricity_unit_price", columnType = PDouble, defaultValue = Just (TypeCastExpression (DoubleExpression 0.17) PDouble), notNull = True, isUnique = False, generator = Nothing}], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [] }
790+
let statement = StatementCreateTable CreateTable { name = "a", columns = [Column {name = "electricity_unit_price", columnType = PDouble, defaultValue = Just (TypeCastExpression (DoubleExpression 0.17) PDouble), notNull = True, isUnique = False, generator = Nothing}], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [], unlogged = False }
781791
compileSql [statement] `shouldBe` sql
782792

783793
it "should compile a integer default value" do
784794
let sql = "CREATE TABLE a (\n electricity_unit_price INT DEFAULT 0 NOT NULL\n);\n"
785-
let statement = StatementCreateTable CreateTable { name = "a", columns = [Column {name = "electricity_unit_price", columnType = PInt, defaultValue = Just (IntExpression 0), notNull = True, isUnique = False, generator = Nothing}], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [] }
795+
let statement = StatementCreateTable CreateTable { name = "a", columns = [Column {name = "electricity_unit_price", columnType = PInt, defaultValue = Just (IntExpression 0), notNull = True, isUnique = False, generator = Nothing}], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [], unlogged = False }
786796
compileSql [statement] `shouldBe` sql
787797

788798
it "should compile a partial index" do
@@ -1019,10 +1029,27 @@ tests = do
10191029
]
10201030
, primaryKeyConstraint = PrimaryKeyConstraint []
10211031
, constraints = []
1032+
, unlogged = False
10221033
}
10231034
]
10241035
compileSql statements `shouldBe` sql
10251036
it "should compile 'DROP FUNCTION ..;' statements" do
10261037
let sql = "DROP FUNCTION my_function;\n"
10271038
let statements = [ DropFunction { functionName = "my_function" } ]
10281039
compileSql statements `shouldBe` sql
1040+
it "should compile 'CREATE UNLOGGED TABLE' statements" do
1041+
let sql = [trimming|
1042+
CREATE UNLOGGED TABLE pg_large_notifications (
1043+
1044+
);
1045+
|] <> "\n"
1046+
let statements = [
1047+
StatementCreateTable CreateTable
1048+
{ name = "pg_large_notifications"
1049+
, columns = []
1050+
, constraints = []
1051+
, unlogged = True
1052+
, primaryKeyConstraint = PrimaryKeyConstraint []
1053+
}
1054+
]
1055+
compileSql statements `shouldBe` sql

Test/IDE/SchemaDesigner/Controller/HelperSpec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ tests = do
1414
getAllObjectNames [ CreateExtension { name ="a", ifNotExists = True } ] `shouldBe` []
1515
getAllObjectNames [ CreateEnumType { name = "first_enum", values=["a", "b", "c"] }] `shouldBe` ["first_enum"]
1616
getAllObjectNames [ StatementCreateTable CreateTable
17-
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[]}
17+
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[], unlogged = False }
1818
]
1919
`shouldBe` ["table_name"]
2020
getAllObjectNames
2121
[ CreateEnumType {name = "first_enum", values = ["a", "b"]}
2222
, CreateExtension {name = "extension", ifNotExists = True}
2323
, StatementCreateTable CreateTable
24-
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[]}
24+
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[], unlogged = False }
2525
, CreateEnumType {name = "second_enum", values = []}
2626
]
2727
`shouldBe` ["first_enum","table_name","second_enum"]

0 commit comments

Comments
 (0)