From b0cabdac1586fb1868785827fcbbfb213d77035f Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Wed, 29 Aug 2018 10:57:08 +0200 Subject: [PATCH] Quotes in tableExists for Migration --- src/Model/Migration.hs | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/Model/Migration.hs b/src/Model/Migration.hs index 76a93e5ac..b835b7960 100644 --- a/src/Model/Migration.hs +++ b/src/Model/Migration.hs @@ -87,30 +87,19 @@ migrateAll = do customMigrations :: MonadIO m => Map (Key AppliedMigration) (ReaderT SqlBackend m ()) customMigrations = Map.fromListWith (>>) [ ( AppliedMigrationKey [migrationVersion|initial|] [version|0.0.0|] - , do -- New theme format - haveUserTable <- [sqlQQ| SELECT to_regclass('user'); |] - - case haveUserTable :: [Maybe (Single Text)] of - [Just _] -> do - userThemes <- [sqlQQ| SELECT 'id', 'theme' FROM 'user'; |] - forM_ userThemes $ \(uid, Single str) -> case stripPrefix "theme--" str of - Just v - | Just theme <- fromPathPiece v -> update uid [UserTheme =. theme] - other -> error $ "Could not parse theme: " <> show other - _other -> return () + , whenM (tableExists "user") $ do -- New theme format + userThemes <- [sqlQQ| SELECT 'id', 'theme' FROM 'user'; |] + forM_ userThemes $ \(uid, Single str) -> case stripPrefix "theme--" str of + Just v + | Just theme <- fromPathPiece v -> update uid [UserTheme =. theme] + other -> error $ "Could not parse theme: " <> show other ) , ( AppliedMigrationKey [migrationVersion|0.0.0|] [version|1.0.0|] - , do -- Better JSON encoding - - haveSheetTable <- [sqlQQ| SELECT to_regclass('sheet'); |] - - case haveSheetTable :: [Maybe (Single Text)] of - [Just _] -> - [executeQQ| - ALTER TABLE 'sheet' ALTER COLUMN 'type' TYPE json USING 'type'::json; - ALTER TABLE 'sheet' ALTER COLUMN 'grouping' TYPE json USING 'grouping'::json; - |] - _other -> return () + , whenM (tableExists "sheet") $ -- Better JSON encoding + [executeQQ| + ALTER TABLE 'sheet' ALTER COLUMN 'type' TYPE json USING 'type'::json; + ALTER TABLE 'sheet' ALTER COLUMN 'grouping' TYPE json USING 'grouping'::json; + |] ) , ( AppliedMigrationKey [migrationVersion|1.0.0|] [version|2.0.0|] , whenM (tableExists "school") $ do -- SchoolId is the Shorthand CI Text now @@ -168,7 +157,7 @@ customMigrations = Map.fromListWith (>>) tableExists :: MonadIO m => Text -> ReaderT SqlBackend m Bool tableExists table = do - haveSchoolTable <- [sqlQQ| SELECT to_regclass(#{table}); |] + haveSchoolTable <- [sqlQQ| SELECT to_regclass('#{table}'); |] case haveSchoolTable :: [Maybe (Single Text)] of [Just _] -> return True _other -> return False