]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/DbImport.hs
Rewrite TSN.DbImport.run_dbmigrate for groundhog-0.7.
[dead/htsn-import.git] / src / TSN / DbImport.hs
index 62cc8a10032eac904742f48d312454001b731209..066b5a0c36e7e8ec2a72dfc687224a03ff3b1b79 100644 (file)
 module TSN.DbImport (
   DbImport(..),
   ImportResult(..),
-  dbimport_generic,
   run_dbmigrate )
 where
 
 -- System imports
-import Control.Monad.IO.Class ( MonadIO )
-import Database.Groundhog (
-  runMigration )
+import Control.Monad ( forM_ )
+import Control.Monad.IO.Class ( MonadIO( liftIO ) )
+import qualified Data.Map as Map ( elems )
+import Database.Groundhog ( executeRaw )
+import Database.Groundhog.Core ( NamedMigrations )
+import Database.Groundhog.Generic (
+  createMigration,
+  getQueries,
+  mergeMigrations )
 import Database.Groundhog.Core ( Migration, PersistBackend )
 import Network.Services.TSN.Report ( report_info )
 
--- Local imports
-import TSN.XmlImport ( XmlImport(..) )
-
 
 -- | The type that will be returned from every file import attempt.
 --
@@ -60,21 +62,19 @@ class DbImport a where
   dbmigrate :: (MonadIO m, PersistBackend m) => a -> m ()
 
 
--- | The simplest possible implementation of 'dbimport', for types
---   which happen to be members of the 'XmlImport' typeclass.
---
-dbimport_generic :: (XmlImport a, MonadIO m, PersistBackend m)
-                 => a
-                 -> m ImportResult
-dbimport_generic x = insert_xml x >> return ImportSucceeded
-
-
 -- | A migration runner that will use our normal info reporting
---   mechanism.
+--   mechanism. The top-level code was stolen from 'runMigration' in
+--   "Data.Groundhog.Generic" and the 'execute_pretty' code was stolen
+--   from 'executeMigration'' in the same module.
 --
 run_dbmigrate :: (MonadIO m, PersistBackend m) => Migration m -> m ()
-run_dbmigrate =
-  runMigration pretty_migration_logger
+run_dbmigrate migration = createMigration migration >>= execute_pretty
   where
-    pretty_migration_logger x =
-      report_info ("Migration: " ++ x ++ ";")
+    execute_pretty :: (PersistBackend m, MonadIO m) => NamedMigrations -> m ()
+    execute_pretty m = do
+      let migs = getQueries False $ mergeMigrations $ Map.elems m
+      case migs of
+        Left errs -> fail $ unlines errs
+        Right qs -> forM_ qs $ \q -> do
+                         liftIO $ report_info ("Migration: " ++ q ++ ";")
+                         executeRaw False q []