Skip to content

Commit

Permalink
fixed long constraint names cause verbose patches in migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten authored and MonaMayrhofer committed Mar 26, 2024
1 parent e4fdb97 commit 1e4c4e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ normalizeTable table@(CreateTable { .. }) = ( CreateTable { columns = fst normal
Left c -> Just c

normalizeConstraint :: Text -> Constraint -> Constraint
normalizeConstraint _ ForeignKeyConstraint { name, columnName, referenceTable, referenceColumn, onDelete } = ForeignKeyConstraint { name, columnName = Text.toLower columnName, referenceTable = Text.toLower referenceTable, referenceColumn = fmap Text.toLower referenceColumn, onDelete = Just (fromMaybe NoAction onDelete) }
normalizeConstraint _ ForeignKeyConstraint { name, columnName, referenceTable, referenceColumn, onDelete } = ForeignKeyConstraint { name = truncateIdentifier <$> name, columnName = Text.toLower columnName, referenceTable = Text.toLower referenceTable, referenceColumn = fmap Text.toLower referenceColumn, onDelete = Just (fromMaybe NoAction onDelete) }
normalizeConstraint tableName constraint@(UniqueConstraint { name = Just uniqueName, columnNames }) | length columnNames > 1 =
-- Single column UNIQUE constraints like:
--
Expand Down
11 changes: 11 additions & 0 deletions Test/IDE/CodeGeneration/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,17 @@ CREATE POLICY "Users can read and edit their own record" ON public.users USING (

diffSchemas targetSchema actualSchema `shouldBe` migration

it "should truncate very long constraint names" do
let actualSchema = sql $ cs [plain|
ALTER TABLE organization_num_employees_ranges ADD CONSTRAINT organization_num_employees_ranges_ref_prospect_search_request_id FOREIGN KEY (prospect_search_request_id) REFERENCES prospect_search_requests (id) ON DELETE NO ACTION;
|]
let targetSchema = sql $ cs [plain|
ALTER TABLE organization_num_employees_ranges ADD CONSTRAINT organization_num_employees_ranges_ref_prospect_search_request_i FOREIGN KEY (prospect_search_request_id) REFERENCES prospect_search_requests (id) ON DELETE NO ACTION;
|]
let migration = []

diffSchemas targetSchema actualSchema `shouldBe` migration

it "should deal with nested SELECT expressions inside a policy" do
let actualSchema = sql $ cs [plain|
CREATE POLICY "Allow users to see their own company" ON public.companies USING ((id = ( SELECT users.company_id
Expand Down

0 comments on commit 1e4c4e3

Please sign in to comment.