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

Primary key implies a Unique constraint #1383

Merged
merged 7 commits into from
Apr 12, 2022

Conversation

parsonsmatt
Copy link
Collaborator

@parsonsmatt parsonsmatt commented Apr 12, 2022

Before submitting your PR, check that you've:

  • Documented new APIs with Haddock markup
  • Added @since declarations to the Haddock
  • Ran stylish-haskell on any changed files.
  • Adhered to the code style (see the .editorconfig file for details)

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Bumped the version number if there isn't an (unreleased) on the Changelog
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

Copy link
Collaborator Author

@parsonsmatt parsonsmatt left a comment

Choose a reason for hiding this comment

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

overall very happy with how easy this was to knock out

Comment on lines -34 to -48
it "checkUniqueUpdateable" $ runDb $ do
let f = 3
let b = 5
let fo = Fo f b
k <- insert fo
Just _ <- checkUnique fo -- conflicts with itself

let fo' = Fo (f + 1) b
Just _ <- checkUnique fo' -- conflicts with fo
Nothing <- checkUniqueUpdateable $ Entity k fo' -- but fo can be updated to fo'

let fo'' = Fo (f + 1) (b + 1)
insert_ fo''
Just (UniqueBar conflict) <- checkUniqueUpdateable $ Entity k fo'' -- fo can't be updated to fo''
conflict @== b + 1
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the old insertUnique code was actually broken on this, since it would be totally fine doing an insert based on the UniqueBar being actually unique, but then fail with the Primary key constraint violation.

@@ -1067,7 +1074,7 @@ takeId ps entityName texts =
--
-- @since.2.13.0.0
data UnboundCompositeDef = UnboundCompositeDef
{ unboundCompositeCols :: [FieldNameHS]
{ unboundCompositeCols :: NonEmpty FieldNameHS
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Might as well - we can't have a composite key without at least one field.

Comment on lines +995 to +996
, entityConstraintDefsUniques =
Just $ pure $ compositeToUniqueDef entityName defs unboundComposite
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the real change. We're adding a UniqueDef based on the composite primary key.

@@ -81,7 +81,7 @@ mkColumns
-> BackendSpecificOverrides
-> ([Column], [UniqueDef], [ForeignDef])
mkColumns allDefs t overrides =
(cols, getEntityUniques t, getEntityForeignDefs t)
(cols, getEntityUniquesNoPrimaryKey t, getEntityForeignDefs t)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In order to preserve migration compatibility, we don't return the composite primary key in the UniqueDef for migrations.

where
parentKeyFieldNames
:: [(FieldNameHS, FieldNameDB)]
:: NonEmpty (FieldNameHS, FieldNameDB)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

just pushing this as far as we can.. even if it's toListed about two lines above.

Comment on lines +397 to +408
entityUniques (unboundEntityDef user) `shouldBe`
[ UniqueDef
{ uniqueHaskell =
ConstraintNameHS "UserPrimaryKey"
, uniqueDBName =
ConstraintNameDB "primary_key"
, uniqueFields =
pure (FieldNameHS "ref", FieldNameDB "ref")
, uniqueAttrs =
[]
}
]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

super simple test for the Quasi parser

@parsonsmatt
Copy link
Collaborator Author

Fixes #1323

@parsonsmatt parsonsmatt linked an issue Apr 12, 2022 that may be closed by this pull request
@parsonsmatt
Copy link
Collaborator Author

Fixes #985

@parsonsmatt parsonsmatt added this to the 2.14 milestone Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a Unique constructor for Primary keys
1 participant