summaryrefslogtreecommitdiff
path: root/src/Propellor
diff options
context:
space:
mode:
authorNicolas Schodet2019-03-25 23:22:55 +0100
committerNicolas Schodet2019-03-26 00:26:09 +0100
commit464f8e0fd64059272291f3c0b5f5c8686e6a4ebf (patch)
tree650f7921f8696f7975535301a54279134f0b07d1 /src/Propellor
parentc76a38b1f595d56b6972d053f41bc38a80f36923 (diff)
Mysql: add databaseRestored to create a database and populate itmysql
Diffstat (limited to 'src/Propellor')
-rw-r--r--src/Propellor/Property/Mysql.hs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/Propellor/Property/Mysql.hs b/src/Propellor/Property/Mysql.hs
index fff87337..0d6161f3 100644
--- a/src/Propellor/Property/Mysql.hs
+++ b/src/Propellor/Property/Mysql.hs
@@ -13,6 +13,7 @@ module Propellor.Property.Mysql (
installed,
installedAndReady,
databaseExists,
+ databaseRestored,
userGrantedOnDatabase,
userGranted,
) where
@@ -157,6 +158,49 @@ databaseExists (Database dbname) =
qdbname = sqlQuote '\'' dbname
trueResult = dbname ++ "\n"
+-- | Restore a database from a gziped backup.
+--
+-- Only does anything if the database does not exist, or is completely empty.
+--
+-- If the backup does not exists yet, only make sure the database exists.
+databaseRestored :: Database -> FilePath -> Property DebianLike
+databaseRestored (Database dbname) gzFile =
+ restored `requires` databaseExists (Database dbname)
+ where
+ restored :: Property UnixLike
+ restored = property desc $ ifM (liftIO $ doesFileExist gzFile)
+ ( ifM (liftIO dbEmpty)
+ ( do
+ warningMessage restoringMessage
+ liftIO restore
+ , noChange
+ )
+ , do
+ warningMessage nobackupMessage
+ noChange
+ )
+
+ dbEmpty :: IO Bool
+ dbEmpty = (== "") <$> readProcess "mysql" ["-BNre", sql]
+ where
+ sql = "SHOW TABLES FROM " ++ qdbname
+ qdbname = sqlQuote '`' dbname
+
+ restore :: IO Result
+ restore = restoreResult <$> boolSystem "sh" [
+ Param "-c"
+ , Param $ "zcat " ++ shellEscape gzFile
+ ++ " | mysql " ++ shellEscape dbname
+ ]
+
+ restoreResult False = FailedChange
+ restoreResult True = MadeChange
+
+ desc = "database " ++ dbname ++ " restored"
+ restoringMessage =
+ "database " ++ dbname ++ " is empty; restoring from backup ..."
+ nobackupMessage = "no backup for database " ++ dbname
+
-- | Create an user and make sure he has grants on the specific database but
-- no other grant.
userGrantedOnDatabase