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 5 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use `make console` to load your application together with the framework located

```
ghci
:l IHP/exe/IHP/IDE/DevServer.hs
:l IHP/ihp-ide/exe/IHP/IDE/DevServer.hs
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpscholten I think the doc is outdated.

  1. Is make console, mentioned above , needed?
  2. I'm getting an error
    image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could just be ghci. make console is just an alias for ghci.

Can you try :set -iIHP/ihp-ide in the ghci? This will add the IHP/ihp-ide directory to the load path. After that it should load without the error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it helped (adapted docs)

But now I get

image

Copy link
Member

@mpscholten mpscholten Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reproduce that. Just tried it locally and here's an approach how I got it working with latest IHP:

# Assuming workingDirectory is the project

cd IHP # switch to IHP
direnv allow # load direnv things if this is freshly cloned

ghci # Load ghci will all IHP dependencies
:cd .. # switch back to project directory
:set -iIHP
:set -iIHP/ihp-ide
 :l IHP/ihp-ide/exe/IHP/IDE/DevServer.hs

That should work now

locally i run into a ghc bug while doing that, I hope it works for you:

ghci> :l IHP/ihp-ide/exe/IHP/IDE/DevServer.hs
Ok, 164 modules loaded.
ghci> main
panic! (the 'impossible' happened)
  GHC version 9.8.2:
	nameModule
  internal $fHasQueryBuilderkJoinQueryBuilderWrapperjoinRegister1_iefl
  Call stack:
      CallStack (from HasCallStack):
        callStackDoc, called at compiler/GHC/Utils/Panic.hs:191:37 in ghc-9.8.2-inplace:GHC.Utils.Panic
        pprPanic, called at compiler/GHC/Types/Name.hs:341:3 in ghc-9.8.2-inplace:GHC.Types.Name
  CallStack (from HasCallStack):
    panic, called at compiler/GHC/Utils/Error.hs:503:29 in ghc-9.8.2-inplace:GHC.Utils.Error


Please report this as a GHC bug:  https://www.haskell.org/ghc/reportabug

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a workaround for the GHC bug I reverted the commit that introduced the bug by running git revert 4fe8b56e5a8c5dcb394a16b0dfb5eb2d6f77b18f from within the IHP directory

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've ran into more issues with the dev server right now and added a new helper called mainInParentDirectory to fix that.

After you've synced this branch with latest master it should now work like this:

# Assuming workingDirectory is the project

cd IHP # switch to IHP
direnv allow # load direnv things if this is freshly cloned

ghci # Load ghci will all IHP dependencies
 :l ihp-ide/exe/IHP/IDE/DevServer.hs
mainInParentDirectory # Will run the IHP project in the parent project directory

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for looking into it 🙏

I'm confused about which URL should be in my flake.nix

ihp/CONTRIBUTING.md

Lines 43 to 67 in 27a696a

### Nix Changes
If you're testing local nix changes, you need to change your `flake.nix` to point to the IHP project instead of pulling it from github:
```nix
{
inputs = {
# The path needs to be absolute
ihp.url = "path:///Users/myuser/someproject/IHP";
# ...
};
```
After that run `nix flake update`.
### Running the latest IHP `master`
When contributing to IHP core you will want to have your PRs synced with `master`. Your `flake.nix` should have this line:
```nix
{
ihp.url = "github:digitallyinduced/ihp";
}
```

I'm guessing we should always tell folks to point to the local IHP, or is there a different use case?

main
```

Expand Down
27 changes: 23 additions & 4 deletions Test/IDE/SchemaDesigner/CompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,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 @@ -841,7 +841,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 @@ -1040,7 +1040,7 @@ tests = do
it "should compile 'CREATE UNLOGGED TABLE' statements" do
let sql = [trimming|
CREATE UNLOGGED TABLE pg_large_notifications (

);
|] <> "\n"
let statements = [
Expand All @@ -1067,4 +1067,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
2 changes: 1 addition & 1 deletion ihp-ide/IHP/IDE/SchemaDesigner/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ compileSql statements = statements
|> unlines

compileStatement :: Statement -> Text
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);"
compileStatement (StatementCreateTable CreateTable { name, columns, primaryKeyConstraint, constraints, unlogged, inherits }) = "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)" <> maybe "" (\parentTable -> " INHERITS (" <> compileIdentifier parentTable <> ")") inherits <> ";"
compileStatement CreateEnumType { name, values } = "CREATE TYPE " <> compileIdentifier name <> " AS ENUM (" <> intercalate ", " (values |> map TextExpression |> map compileExpression) <> ");"
compileStatement CreateExtension { name, ifNotExists } = "CREATE EXTENSION " <> (if ifNotExists then "IF NOT EXISTS " else "") <> compileIdentifier name <> ";"
compileStatement AddConstraint { tableName, constraint = UniqueConstraint { name = Nothing, columnNames } } = "ALTER TABLE " <> compileIdentifier tableName <> " ADD UNIQUE (" <> intercalate ", " columnNames <> ")" <> ";"
Expand Down
10 changes: 9 additions & 1 deletion ihp-ide/IHP/IDE/SchemaDesigner/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ createTable = do
columnsAndConstraints <- ((Right <$> parseTableConstraint) <|> (Left <$> parseColumn)) `sepBy` (char ',' >> space)
pure (lefts columnsAndConstraints, rights columnsAndConstraints)

inherits <- optional parseInheritsClause

char ';'

-- Check that either there is a single column with a PRIMARY KEY constraint,
Expand All @@ -116,7 +118,7 @@ createTable = do
_ -> Prelude.fail ("Primary key defined in both column and table constraints on table " <> cs name)
_ -> Prelude.fail "Multiple columns with PRIMARY KEY constraint"

pure CreateTable { name, columns, primaryKeyConstraint, constraints, unlogged }
pure CreateTable { name, columns, primaryKeyConstraint, constraints, unlogged, inherits }

createEnumType = do
lexeme "CREATE"
Expand Down Expand Up @@ -222,6 +224,12 @@ parseOnDelete = choice
, (lexeme "CASCADE" >> pure Cascade)
]

parseInheritsClause :: Parser Text
parseInheritsClause = do
lexeme "INHERITS"
parentTable <- between (char '(' >> space) (char ')' >> space) qualifiedIdentifier
pure parentTable

parseColumn :: Parser (Bool, Column)
parseColumn = do
name <- identifier
Expand Down
1 change: 1 addition & 0 deletions ihp-ide/IHP/IDE/SchemaDesigner/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ data CreateTable
, primaryKeyConstraint :: PrimaryKeyConstraint
, constraints :: [Constraint]
, unlogged :: !Bool
, inherits :: Maybe Text
}
deriving (Eq, Show)

Expand Down
Loading