-
Notifications
You must be signed in to change notification settings - Fork 297
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 Migration Woes #907
Open
parsonsmatt
wants to merge
3
commits into
master
Choose a base branch
from
matt/primary-key-migration-add
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,10 @@ Source | |
field3 Int | ||
field4 TargetId | ||
|
||
PreUnique sql=pre_unique | ||
field5 Int | ||
field6 T.Text | ||
|
||
CustomSqlId | ||
pk Int sql=id | ||
Primary pk | ||
|
@@ -34,22 +38,67 @@ Source1 sql=source | |
field3 Int | ||
extra Int | ||
field4 Target1Id | ||
|
||
|] | ||
|
||
specsWith :: (MonadUnliftIO m) => RunDb SqlBackend m -> Spec | ||
specsWith runDb = describe "Migration" $ do | ||
share [mkPersist sqlSettings, mkMigrate "addPrimKey", mkDeleteCascade sqlSettings] [persistLowerCase| | ||
FromRawMigration | ||
name T.Text | ||
age Int | ||
|
||
Primary name | ||
|] | ||
|
||
share [mkPersist sqlSettings, mkMigrate "addUniqKey", mkDeleteCascade sqlSettings] [persistLowerCase| | ||
|
||
PostUnique sql=pre_unique | ||
field5 Int | ||
field6 T.Text | ||
|
||
UniquField5 field5 | ||
|
||
|] | ||
|
||
dropTables :: MonadIO m => SqlPersistT m () | ||
dropTables = do | ||
rawExecute "DROP TABLE IF EXISTS source" [] | ||
rawExecute "DROP TABLE IF EXISTS target" [] | ||
rawExecute "DROP TABLE IF EXISTS pre_unique" [] | ||
rawExecute "DROP TABLE IF EXISTS from_raw_migration" [] | ||
|
||
specsWith :: (MonadIO m, MonadFail m) => RunDb SqlBackend m -> Spec | ||
specsWith runDb = describe "Migration" $ before_ (runDb dropTables) $ do | ||
it "is idempotent" $ runDb $ do | ||
again <- getMigration migrationMigrate | ||
liftIO $ again @?= [] | ||
it "really is idempotent" $ runDb $ do | ||
runMigrationSilent migrationMigrate | ||
runMigrationSilent migrationMigrate | ||
again <- getMigration migrationMigrate | ||
liftIO $ again @?= [] | ||
_ <- runMigration migrationMigrate | ||
again <- getMigration migrationMigrate | ||
liftIO $ again @?= [] | ||
it "can add an extra column" $ runDb $ do | ||
-- Failing test case for #735. Foreign-key checking, switched on in | ||
-- version 2.6.1, caused persistent-sqlite to generate a `references` | ||
-- constraint in a *temporary* table during migration, which fails. | ||
_ <- runMigrationSilent migrationAddCol | ||
again <- getMigration migrationAddCol | ||
liftIO $ again @?= [] | ||
-- Failing test case for #735. Foreign-key checking, switched on in | ||
-- version 2.6.1, caused persistent-sqlite to generate a `references` | ||
-- constraint in a *temporary* table during migration, which fails. | ||
_ <- runMigration migrationMigrate | ||
_ <- runMigration migrationAddCol | ||
again <- getMigration migrationAddCol | ||
liftIO $ again @?= [] | ||
describe "Add Unique Key constraint" $ do | ||
it "should not be considered safe" $ runDb $ do | ||
_ <- runMigration migrationMigrate | ||
Right migration <- parseMigration addUniqKey | ||
liftIO $ migration | ||
`shouldSatisfy` | ||
(\cm -> True `elem` map fst cm) | ||
|
||
xdescribe "Add Primary key constraint on raw table" $ do | ||
it "should not be considered safe" $ runDb $ do | ||
rawExecute "CREATE TABLE from_raw_migration (name VARCHAR NOT NULL, age INT8 NOT NULL)" [] | ||
Right migration <- parseMigration addPrimKey | ||
liftIO $ migration | ||
`shouldSatisfy` | ||
(\cm -> True `elem` map fst cm) | ||
|
||
it "works" $ runDb $ do | ||
rawExecute "CREATE TABLE from_raw_migration (name VARCHAR NOT NULL, age INT NOT NULL)" [] | ||
Right migration <- parseMigration addPrimKey | ||
() <- runMigration addPrimKey | ||
again <- getMigration addPrimKey | ||
liftIO $ again @?= [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently fails on
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a primary key to a column is definitely not safe, because it's like adding a unique key to a column.