Skip to content

Commit 9bd38ab

Browse files
committed
Add drop tables and unique key checking
1 parent c24b2c0 commit 9bd38ab

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

persistent-test/src/MigrationTest.hs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Source
1818
field3 Int
1919
field4 TargetId
2020

21+
PreUnique sql=pre_unique
22+
field5 Int
23+
field6 T.Text
24+
2125
|]
2226

2327
share [mkPersist sqlSettings, mkMigrate "migrationAddCol", mkDeleteCascade sqlSettings] [persistLowerCase|
@@ -40,11 +44,27 @@ FromRawMigration
4044
age Int
4145

4246
Primary name
47+
|]
48+
49+
share [mkPersist sqlSettings, mkMigrate "addUniqKey", mkDeleteCascade sqlSettings] [persistLowerCase|
50+
51+
PostUnique sql=pre_unique
52+
field5 Int
53+
field6 T.Text
54+
55+
UniquField5 field5
4356

4457
|]
4558

59+
dropTables :: MonadIO m => SqlPersistT m ()
60+
dropTables = do
61+
rawExecute "DROP TABLE IF EXISTS source" []
62+
rawExecute "DROP TABLE IF EXISTS target" []
63+
rawExecute "DROP TABLE IF EXISTS pre_unique" []
64+
rawExecute "DROP TABLE IF EXISTS from_raw_migration" []
65+
4666
specsWith :: (MonadIO m, MonadFail m) => RunDb SqlBackend m -> Spec
47-
specsWith runDb = describe "Migration" $ do
67+
specsWith runDb = describe "Migration" $ before_ (runDb dropTables) $ do
4868
it "is idempotent" $ runDb $ do
4969
_ <- runMigration migrationMigrate
5070
again <- getMigration migrationMigrate
@@ -57,7 +77,15 @@ specsWith runDb = describe "Migration" $ do
5777
_ <- runMigration migrationAddCol
5878
again <- getMigration migrationAddCol
5979
liftIO $ again @?= []
60-
describe "Add Primary key constraint on raw table" $ do
80+
describe "Add Unique Key constraint" $ do
81+
it "should not be considered safe" $ runDb $ do
82+
_ <- runMigration migrationMigrate
83+
Right migration <- parseMigration addUniqKey
84+
liftIO $ migration
85+
`shouldSatisfy`
86+
(\cm -> True `elem` map fst cm)
87+
88+
xdescribe "Add Primary key constraint on raw table" $ do
6189
it "should not be considered safe" $ runDb $ do
6290
rawExecute "CREATE TABLE from_raw_migration (name VARCHAR NOT NULL, age INT8 NOT NULL)" []
6391
Right migration <- parseMigration addPrimKey

0 commit comments

Comments
 (0)