Skip to content

Commit

Permalink
Merge pull request #1939 from digitallyinduced/fix-long-constraint-names
Browse files Browse the repository at this point in the history
fixed long constraint names cause verbose patches in migrations
  • Loading branch information
mpscholten authored Mar 25, 2024
2 parents 74ac074 + 77597c4 commit 57c9399
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 57c9399

Please sign in to comment.