Skip to content

Commit

Permalink
Fixed table deletion causing bad DROP POLICY statement
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Nov 21, 2022
1 parent 395c717 commit c244aa5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ removeImplicitDeletions actualSchema (statement@dropStatement:rest) | isDropStat
)
Nothing -> True
isImplicitlyDeleted (DropConstraint { tableName = constraintTableName }) = constraintTableName /= dropTableName
isImplicitlyDeleted (DropPolicy { tableName = policyTableName }) = not (isNothing dropColumnName && policyTableName == dropTableName)
isImplicitlyDeleted otherwise = True

findIndexByName :: Text -> Maybe Statement
Expand Down
19 changes: 19 additions & 0 deletions Test/IDE/CodeGeneration/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,25 @@ CREATE POLICY "Users can read and edit their own record" ON public.users USING (
DROP POLICY "Users can manage their artefacts" ON artefacts;
|]

diffSchemas targetSchema actualSchema `shouldBe` migration

it "should not explicitly delete policies when the table is deleted" do
-- https://github.com/digitallyinduced/thin-backend/issues/69
let actualSchema = sql $ cs [plain|
CREATE TABLE tests (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
user_id UUID DEFAULT ihp_user_id() NOT NULL
);
CREATE INDEX tests_user_id_index ON tests (user_id);
ALTER TABLE tests ADD CONSTRAINT tests_ref_user_id FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION;
ALTER TABLE tests ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage their tests" ON tests USING (user_id = ihp_user_id()) WITH CHECK (user_id = ihp_user_id());
|]
let targetSchema = []
let migration = sql [i|
DROP TABLE tests;
|]

diffSchemas targetSchema actualSchema `shouldBe` migration
sql :: Text -> [Statement]
sql code = case Megaparsec.runParser Parser.parseDDL "" code of
Expand Down

0 comments on commit c244aa5

Please sign in to comment.