Skip to content

Commit

Permalink
Fixed rename table operation doesn't correctly rename other DDL state…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
mpscholten committed Oct 6, 2022
1 parent ff520a0 commit a02b71b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 14 additions & 1 deletion IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ diffSchemas targetSchema' actualSchema' = (drop <> create)
applyRenameTable :: [Statement] -> [Statement]
applyRenameTable ((s@DropTable { tableName }):statements) =
case createTable of
Just createTable@(StatementCreateTable { unsafeGetCreateTable = createTable' }) -> (RenameTable { from = tableName, to = get #name createTable' }):(applyRenameTable (delete createTable statements))
Just createTable@(StatementCreateTable { unsafeGetCreateTable = createTable' }) ->
let
from = tableName
to = get #name createTable'
in
(RenameTable { from, to }):(applyRenameTable (fixIdentifiers from to (delete createTable statements)))
Nothing -> s:(applyRenameTable statements)
where
createTable :: Maybe Statement
Expand All @@ -146,6 +151,14 @@ diffSchemas targetSchema' actualSchema' = (drop <> create)
actualTable' :: CreateTable
actualTable' = case actualTable of
StatementCreateTable { unsafeGetCreateTable = table } -> table

fixIdentifiers :: Text -> Text -> [Statement] -> [Statement]
fixIdentifiers tableFrom tableTo statements = map fixIdentifier statements
where
fixIdentifier :: Statement -> Statement
fixIdentifier s@(DropConstraint { tableName }) | tableName == tableFrom = s { tableName = tableTo }
fixIdentifier s@(DropPolicy { tableName }) | tableName == tableFrom = s { tableName = tableTo }
fixIdentifier o = o
applyRenameTable (s:rest) = s:(applyRenameTable rest)
applyRenameTable [] = []

Expand Down
12 changes: 12 additions & 0 deletions Test/IDE/CodeGeneration/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,18 @@ CREATE POLICY "Users can read and edit their own record" ON public.users USING (
|]
let migration = sql [i|
ALTER TABLE media RENAME TO artefacts;

DROP INDEX media_created_at_index;
DROP INDEX media_user_id_index;
ALTER TABLE artefacts DROP CONSTRAINT media_ref_user_id;
DROP POLICY "Users can manage their media" ON artefacts;

CREATE INDEX artefacts_created_at_index ON artefacts (created_at);
CREATE TRIGGER update_artefacts_updated_at BEFORE UPDATE ON artefacts FOR EACH ROW EXECUTE FUNCTION set_updated_at_to_now();
CREATE INDEX artefacts_user_id_index ON artefacts (user_id);
ALTER TABLE artefacts ADD CONSTRAINT artefacts_ref_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION;
ALTER TABLE artefacts ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage their artefacts" ON artefacts USING (user_id = ihp_user_id()) WITH CHECK (user_id = ihp_user_id());
|]

diffSchemas targetSchema actualSchema `shouldBe` migration
Expand Down

0 comments on commit a02b71b

Please sign in to comment.