Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions selda-tests/test/Tests/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Database.Selda.Unsafe (unsafeSelector, rawStm)
import Test.HUnit
import Utils
import Tables
import Data.UUID.Types (nil)
#if !MIN_VERSION_base(4, 11, 0)
import Data.Semigroup
#endif
Expand Down Expand Up @@ -71,6 +72,7 @@ mutableTests freshEnv = test
, "auto-migrate no-op" ~: freshEnv (migrationTest autoMigrateNoOp)
, "migrate aggregate" ~: freshEnv (migrationTest migrateAggregate)
, "auto-migrate multi-step" ~: freshEnv (migrationTest autoMigrateOneStep)
, "migrate tables with uuid col" ~: freshEnv migrateUuidTableTest
, "multi-unique insert" ~: freshEnv multiUnique
, "uuid inserts" ~: freshEnv uuidInserts
, "uuid queries" ~: freshEnv uuidQueries
Expand Down Expand Up @@ -681,6 +683,34 @@ migrationTable3 :: Table (Only Int)
migrationTable3 = table "table3" [Single mt3_1 :- primary]
mt3_1 = selectors migrationTable3


migrateUuidTableTest = do
tryDropTable migrationUuid1
createTable migrationUuid1
insert_ migrationUuid1 [Only nil]
autoMigrate True stepsUuid
res <- query $ do
select migrationUuid2
assEq "migration went wrong" [nil] (fmap snd res)
tryDropTable migrationUuid1
tryDropTable migrationUuid2

migrationUuid1 :: Table (Only UUID)
migrationUuid1 = table "tableUuid" [Single mtUuid1_1 :- primary]
mtUuid1_1 = selectors migrationUuid1

migrationUuid2 :: Table (Text, UUID)
migrationUuid2 = table "tableUuid" [Single mtUuid2_1 :- primary]
mtUuid2_1 :*: mtUuid2_2 = selectors migrationUuid2

stepsUuid =
[ [Migration migrationUuid1 migrationUuid2 $ \foo -> pure $ new
[ mtUuid2_1 := toString (the foo)
, mtUuid2_2 := the foo
]
]
]

steps =
[ [Migration migrationTable1 migrationTable1 pure]
, [Migration migrationTable1 migrationTable2 $ \foo -> pure $ new
Expand Down
6 changes: 3 additions & 3 deletions selda/src/Database/Selda/Migrations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ migrateInternal t1 t2 upg = withBackend $ \b -> do
void . liftIO $ runStmt b renameQuery []
createTableIndexes Fail t2
where
t2' = t2 {tableName = mkTableName newName} `asTypeOf` t2
newName = mconcat ["__selda_migration_", rawTableName (tableName t2)]
t2' = t2 {tableName = newName} `asTypeOf` t2
newName = mkTableName $ mconcat ["__selda_migration_", rawTableName (tableName t2)]
renameQuery = mconcat
[ "ALTER TABLE ", newName
[ "ALTER TABLE ", fromTableName newName
Copy link
Collaborator

Choose a reason for hiding this comment

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

@YPares just curious, is there any reason why taking this through mk&from-TableName is more viable? (sounds same to me so probably OK.)

, " RENAME TO ", fromTableName (tableName t2), ";"
]
dropQuery t = mconcat ["DROP TABLE ", fromTableName t, ";"]
4 changes: 3 additions & 1 deletion selda/src/Database/Selda/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Database.Selda
Table(..),
MonadSelda )
import Database.Selda.Backend.Internal
( SqlTypeRep(TInt64, TRowID),
( SqlTypeRep(TInt64, TRowID, TBlob, TUUID),
SeldaBackend(getTableInfo),
ColumnInfo(colType, colIsAutoPrimary, colIsNullable, colHasIndex,
colFKs, colName),
Expand All @@ -34,6 +34,8 @@ import Database.Selda.Table.Validation (ValidationError (..), validateOrThrow)
isCompatibleWith :: SqlTypeRep -> SqlTypeRep -> Bool
isCompatibleWith TRowID TInt64 = True
isCompatibleWith TInt64 TRowID = True
isCompatibleWith TBlob TUUID = True
isCompatibleWith TUUID TBlob = True
isCompatibleWith a b = a == b

-- | Validate a table schema, and check it for consistency against the current
Expand Down
Loading