From 464f8e0fd64059272291f3c0b5f5c8686e6a4ebf Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 25 Mar 2019 23:22:55 +0100 Subject: Mysql: add databaseRestored to create a database and populate it --- src/Propellor/Property/Mysql.hs | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/Propellor/Property/Mysql.hs') 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 -- cgit v1.2.3