Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement inherits for Schema.sql #1998

Draft
wants to merge 66 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
2431d6a
Implement `inherits` for Schema.sql
amitaibu Aug 13, 2024
9bee88c
Adapt `compileStatement`
amitaibu Aug 13, 2024
2b71e5e
Add tests
amitaibu Aug 13, 2024
c1844ac
Fix missing field
amitaibu Aug 13, 2024
0578ae1
Try to fix Running the development server
amitaibu Aug 13, 2024
bd1220d
Test fixes
amitaibu Aug 13, 2024
dc248f7
Adjust docs
amitaibu Aug 13, 2024
480d8f3
Another missing field
amitaibu Aug 13, 2024
70dadc5
Merge branch 'master' into core-inherits
amitaibu Aug 14, 2024
3b9df34
More test fixes
amitaibu Aug 14, 2024
518811a
More test fixes
amitaibu Aug 14, 2024
70c7e71
More fixes
amitaibu Aug 14, 2024
19c80b3
CompilerSpec fixes
amitaibu Aug 14, 2024
ec978c9
Fix typo
amitaibu Aug 14, 2024
1fd95fe
Fix tests
amitaibu Aug 14, 2024
591c40f
Replace space in compilerSpec
amitaibu Aug 14, 2024
cbea5de
Another fix
amitaibu Aug 14, 2024
53e7085
Update SchemaCompiler
amitaibu Aug 14, 2024
d8e7f28
Update docs
amitaibu Aug 14, 2024
bebd467
Revert "Added inline annotations for getQueryBuilder"
amitaibu Aug 14, 2024
765a969
Fix wrong comma when no parent table
amitaibu Aug 15, 2024
389df33
Remove duplicate MetaBag
amitaibu Aug 15, 2024
6abdbf1
Add comment
amitaibu Aug 15, 2024
83f652e
Try fix typeArguments
amitaibu Aug 25, 2024
c0ec985
Add todo
amitaibu Aug 25, 2024
1a0dc74
Fix docs typo
amitaibu Aug 25, 2024
b095996
Try to filter wrong vars
amitaibu Aug 25, 2024
d8c6662
filter out the ID
amitaibu Aug 25, 2024
48a3bda
Apply columnNames
amitaibu Aug 25, 2024
97047b0
compileTypePattern
amitaibu Aug 25, 2024
339374b
fix columnNames
amitaibu Aug 25, 2024
781417c
Add comment
amitaibu Aug 25, 2024
8f38d7a
Fix compileTypeAlias
amitaibu Aug 25, 2024
cb3a7d2
compileHasTableNameInstance
amitaibu Aug 25, 2024
22d71b0
compileUpdateFieldInstances
amitaibu Aug 25, 2024
29395ae
compileGetModelName
amitaibu Aug 25, 2024
1d046db
compileUpdateFieldInstances
amitaibu Aug 25, 2024
354208d
More careful compileUpdateFieldInstances
amitaibu Aug 26, 2024
bd2f544
compileDataTypePattern
amitaibu Aug 26, 2024
05fe2f2
Remove `meta` from compileUpdateFieldInstances
amitaibu Aug 26, 2024
2dc6e5d
Place the `meta` as the last value.
amitaibu Aug 26, 2024
b4d0001
Fix meta
amitaibu Aug 26, 2024
78da6fa
more work
amitaibu Aug 26, 2024
8ea918b
More consistent code
amitaibu Aug 26, 2024
97ede64
More Set and Update
amitaibu Aug 26, 2024
351f5b2
Adapt compileBuild
amitaibu Aug 26, 2024
b93791d
More fixes to compileBuild
amitaibu Aug 26, 2024
613bb0d
Add todo question
amitaibu Aug 26, 2024
27897e9
Revamp compileFromRowInstance
amitaibu Aug 26, 2024
f70d902
Fix compileInclude
amitaibu Aug 26, 2024
5a11fd3
Allow set parent fields
amitaibu Aug 26, 2024
fa31d5b
compileCreate
amitaibu Aug 26, 2024
95ea731
compileUpdate
amitaibu Aug 26, 2024
2d1601d
Start adding inherits info to renderColumnSelector
amitaibu Sep 1, 2024
b59e405
Show inherits on UI
amitaibu Sep 2, 2024
9d9c02c
Add padding
amitaibu Sep 2, 2024
9b9e976
Fix tests
amitaibu Sep 2, 2024
d4d20c3
Add comment
amitaibu Sep 2, 2024
a231a50
Add docs
amitaibu Sep 3, 2024
57621fa
Start adding tests
amitaibu Sep 14, 2024
a213547
Note on Constraints in Inherited Tables
amitaibu Sep 15, 2024
c957833
minor typo
amitaibu Sep 16, 2024
2ce5f4f
Remove duplicated func
amitaibu Sep 16, 2024
c727968
Revert "minor typo"
amitaibu Sep 16, 2024
c180627
Fix typo
amitaibu Sep 16, 2024
f2d002a
more readable code
amitaibu Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,16 @@ Note that it takes around 30 minutes for the IHP GitHub actions to prepare a bin


When making changes to the development tooling, we have to start the server differently, without `devenv up`. We have to
use `make console` to load your application together with the framework located in `IHP`.
use `ghci` to load your application together with the framework located in `IHP`.

```
ghci
:l IHP/exe/IHP/IDE/DevServer.hs
main

-- Load the development server
:l ihp-ide/exe/IHP/IDE/DevServer.hs

-- Run the IHP project in the parent project directory
mainInParentDirectory
```

We don't need to start postgres as the IDE starts it automatically.
Expand Down
12 changes: 1 addition & 11 deletions IHP/QueryBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ class HasQueryBuilder queryBuilderProvider joinRegister | queryBuilderProvider -
getQueryBuilder :: queryBuilderProvider table -> QueryBuilder table
injectQueryBuilder :: QueryBuilder table -> queryBuilderProvider table
getQueryIndex :: queryBuilderProvider table -> Maybe ByteString
getQueryIndex _ = Nothing
{-# INLINE getQueryIndex #-}
getQueryIndex _ = Nothing

-- Wrapper for QueryBuilders resulting from joins. Associates a joinRegister type.
newtype JoinQueryBuilderWrapper joinRegister table = JoinQueryBuilderWrapper (QueryBuilder table)
Expand All @@ -179,31 +178,22 @@ newtype LabeledQueryBuilderWrapper foreignTable indexColumn indexValue table = L
-- QueryBuilders have query builders and the join register is empty.
instance HasQueryBuilder QueryBuilder EmptyModelList where
getQueryBuilder = id
{-# INLINE getQueryBuilder #-}
injectQueryBuilder = id
{-# INLINE injectQueryBuilder #-}

-- JoinQueryBuilderWrappers have query builders
instance HasQueryBuilder (JoinQueryBuilderWrapper joinRegister) joinRegister where
getQueryBuilder (JoinQueryBuilderWrapper queryBuilder) = queryBuilder
{-# INLINE getQueryBuilder #-}
injectQueryBuilder = JoinQueryBuilderWrapper
{-# INLINE injectQueryBuilder #-}

-- NoJoinQueryBuilderWrapper have query builders and the join register does not allow any joins
instance HasQueryBuilder NoJoinQueryBuilderWrapper NoJoins where
getQueryBuilder (NoJoinQueryBuilderWrapper queryBuilder) = queryBuilder
{-# INLINE getQueryBuilder #-}
injectQueryBuilder = NoJoinQueryBuilderWrapper
{-# INLINE injectQueryBuilder #-}

instance (KnownSymbol foreignTable, foreignModel ~ GetModelByTableName foreignTable , KnownSymbol indexColumn, HasField indexColumn foreignModel indexValue) => HasQueryBuilder (LabeledQueryBuilderWrapper foreignTable indexColumn indexValue) NoJoins where
getQueryBuilder (LabeledQueryBuilderWrapper queryBuilder) = queryBuilder
{-# INLINE getQueryBuilder #-}
injectQueryBuilder = LabeledQueryBuilderWrapper
{-# INLINE injectQueryBuilder #-}
getQueryIndex _ = Just $ symbolToByteString @foreignTable <> "." <> (Text.encodeUtf8 . fieldNameToColumnName) (symbolToText @indexColumn)
{-# INLINE getQueryIndex #-}


data QueryBuilder (table :: Symbol) =
Expand Down
2 changes: 2 additions & 0 deletions Test/IDE/CodeGeneration/ControllerGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Nothing
},
StatementCreateTable CreateTable {
name = "people"
Expand Down Expand Up @@ -65,6 +66,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Nothing
}
]

Expand Down
1 change: 1 addition & 0 deletions Test/IDE/CodeGeneration/MailGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Nothing
}
]
it "should build a mail with name \"PurchaseConfirmationMail\"" do
Expand Down
1 change: 1 addition & 0 deletions Test/IDE/CodeGeneration/ViewGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Nothing
}
]
it "should build a view with name \"EditView\"" do
Expand Down
49 changes: 40 additions & 9 deletions Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Test.IDE.SchemaDesigner.ParserSpec (col, parseSql)
tests = do
describe "The Schema.sql Compiler" do
it "should compile an empty CREATE TABLE statement" do
compileSql [StatementCreateTable CreateTable { name = "users", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [], unlogged = False }] `shouldBe` "CREATE TABLE users (\n\n);\n"
compileSql [StatementCreateTable CreateTable { name = "users", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints = [], unlogged = False, inherits = Nothing }] `shouldBe` "CREATE TABLE users (\n\n);\n"

it "should compile a CREATE EXTENSION for the UUID extension" do
compileSql [CreateExtension { name = "uuid-ossp", ifNotExists = True }] `shouldBe` "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";\n"
Expand Down Expand Up @@ -109,11 +109,12 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

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

it "should compile ALTER TABLE .. ADD FOREIGN KEY .. ON DELETE CASCADE" do
let statement = AddConstraint
Expand Down Expand Up @@ -478,6 +479,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint []
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand Down Expand Up @@ -530,6 +532,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint []
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -545,6 +548,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = [ UniqueConstraint { name = Nothing, columnNames = [ "user_id", "follower_id" ] } ]
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -556,6 +560,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -567,6 +572,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -581,6 +587,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint ["order_id", "truck_id"]
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -592,6 +599,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint []
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -603,6 +611,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint []
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -614,6 +623,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint []
, constraints = []
, unlogged = False
, inherits = Nothing
}
compileSql [statement] `shouldBe` sql

Expand All @@ -628,7 +638,7 @@ tests = do
, indexType = Nothing
}
compileSql [statement] `shouldBe` sql

it "should escape an index name inside a 'CREATE INDEX' statement" do
let sql = cs [plain|CREATE INDEX "Some Index" ON "Some Table" ("Some Col");\n|]
let statement = CreateIndex
Expand Down Expand Up @@ -787,12 +797,12 @@ tests = do

it "should compile a decimal default value with a type-cast" do
let sql = "CREATE TABLE a (\n electricity_unit_price DOUBLE PRECISION DEFAULT 0.17::DOUBLE PRECISION NOT NULL\n);\n"
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 }
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, inherits = Nothing }
compileSql [statement] `shouldBe` sql

it "should compile a integer default value" do
let sql = "CREATE TABLE a (\n electricity_unit_price INT DEFAULT 0 NOT NULL\n);\n"
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 }
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, inherits = Nothing }
compileSql [statement] `shouldBe` sql

it "should compile a partial index" do
Expand Down Expand Up @@ -841,7 +851,7 @@ tests = do
)
}
compileSql [policy] `shouldBe` sql

it "should compile 'CREATE POLICY' statements with a 'ihp_user_id() IS NOT NULL' expression" do
-- https://github.com/digitallyinduced/ihp/issues/1412
let sql = "CREATE POLICY \"Users can manage tasks if logged in\" ON tasks USING (ihp_user_id() IS NOT NULL) WITH CHECK (ihp_user_id() IS NOT NULL);\n"
Expand Down Expand Up @@ -1030,6 +1040,7 @@ tests = do
, primaryKeyConstraint = PrimaryKeyConstraint []
, constraints = []
, unlogged = False
, inherits = Nothing
}
]
compileSql statements `shouldBe` sql
Expand All @@ -1040,16 +1051,17 @@ tests = do
it "should compile 'CREATE UNLOGGED TABLE' statements" do
let sql = [trimming|
CREATE UNLOGGED TABLE pg_large_notifications (

);
|] <> "\n"
let statements = [
StatementCreateTable CreateTable
{ name = "pg_large_notifications"
, columns = []
, primaryKeyConstraint = PrimaryKeyConstraint []
, constraints = []
, unlogged = True
, primaryKeyConstraint = PrimaryKeyConstraint []
, inherits = Nothing
}
]
compileSql statements `shouldBe` sql
Expand All @@ -1067,4 +1079,23 @@ tests = do
, check = Just (VarExpression "false")
}
]
compileSql statements `shouldBe` sql
compileSql statements `shouldBe` sql

it "should compile a CREATE TABLE statement with INHERITS" do
let sql = "CREATE TABLE child_table (\n id UUID PRIMARY KEY\n) INHERITS (parent_table);\n"
let statement = StatementCreateTable CreateTable
{ name = "child_table"
, columns = [Column
{ name = "id"
, columnType = PUUID
, defaultValue = Nothing
, notNull = False
, isUnique = False
, generator = Nothing
}]
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
, constraints = []
, unlogged = False
, inherits = Just "parent_table"
}
compileSql [statement] `shouldBe` sql
4 changes: 2 additions & 2 deletions Test/IDE/SchemaDesigner/Controller/HelperSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ tests = do
getAllObjectNames [ CreateExtension { name ="a", ifNotExists = True } ] `shouldBe` []
getAllObjectNames [ CreateEnumType { name = "first_enum", values=["a", "b", "c"] }] `shouldBe` ["first_enum"]
getAllObjectNames [ StatementCreateTable CreateTable
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[], unlogged = False }
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[], unlogged = False, inherits = Nothing }
]
`shouldBe` ["table_name"]
getAllObjectNames
[ CreateEnumType {name = "first_enum", values = ["a", "b"]}
, CreateExtension {name = "extension", ifNotExists = True}
, StatementCreateTable CreateTable
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[], unlogged = False }
{ name = "table_name", columns = [], primaryKeyConstraint = PrimaryKeyConstraint [], constraints=[], unlogged = False, inherits = Nothing }
, CreateEnumType {name = "second_enum", values = []}
]
`shouldBe` ["first_enum","table_name","second_enum"]
Loading