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

Allow sql=id to be declared for columns #1066

Merged
merged 16 commits into from
Sep 18, 2020
Merged

Conversation

parsonsmatt
Copy link
Collaborator

@parsonsmatt parsonsmatt commented Mar 31, 2020

Before submitting your PR, check that you've:

After submitting your PR:

  • Update the Changelog.md file with a link to your PR
  • Check that CI passes (or if it fails, for reasons unrelated to your change, like CI timeouts)

Fixes #1059

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, I'm happy with these changes. This is a good step towards non-privileged id fields in persistent.

@@ -960,6 +991,7 @@ findAlters defs _tablename col@(Column name isNull sqltype def _defConstraintNam
| otherwise = [(name, ChangeType sqltype "")]
modDef =
if def == def'
|| isJust (T.stripPrefix "nextval" =<< def')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

kind of a nasty hack :\ this will keep it from removing a nextval default value as picked up from the database.

in v `seq` vs `seq` (v:vs)
let !v = g col
!vs = use gs cols
in (v:vs)
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 really like a strict map but I am not entirely sure where that lives?

return $ Right $ map showAlterDb $ acs' ++ ats'
-- Errors
(_, _, (errs, _)) -> return $ Left errs
case ([], old, partitionEithers old) of
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry for the syntactic noise here but I just couldn't read this code. I formatted it and made it a bit more spaced out to make it easier to read.

let refTarget =
addReference allDefs refConstraintName refTblName cname

guard $ refTblName /= name && cname /= fieldDB (entityId val)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Now that the id column is part of the columns returned by mkColumn we have to treat the reference separately. We don't want a self-reference on the ID column treated as a foreign key.

@@ -119,7 +119,7 @@ main = do
, Recursive.recursiveMigrate
, CompositeTest.compositeMigrate
, PersistUniqueTest.migration
, RenameTest.migration
-- , RenameTest.migration
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently broken on mysql because DEFAULT=CURRENT_DATE is not supported on mysql ???

]
where
nonIdCols =
filter (\c -> cName c /= fieldDB (entityId entity) ) cols
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We filter out the id column here,because it is treated specially in idtxt. And unfortunately it would be pretty difficult/annoying to set a default at the Column level because that bit isn't database agnostic.


CustomSqlId
pk Int sql=id
Primary pk
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 actual test case for the issue in question.

@@ -38,6 +43,7 @@ specsWith runDb = describe "Migration" $ do
again <- getMigration migrationMigrate
liftIO $ again @?= []
it "really is idempotent" $ runDb $ do
runMigrationSilent migrationMigrate
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

reall,y really, REALLY is idempotent

@parsonsmatt
Copy link
Collaborator Author

@tysonzero want to give this a shot?

@tysonzero
Copy link

It seems to have fixed the issue.

Although now it's trying to add a lot of foreign keys from tables to themselves:

ALTER TABLE "<table>" ADD CONSTRAINT "<table>_id_fkey" FOREIGN KEY("id") REFERENCES "<table>"("id") ;
...

@parsonsmatt
Copy link
Collaborator Author

I think I've fixed the self-reference hack, thanks for noticing that :D

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.

sql=id breaks migrations
2 participants