@@ -6,6 +6,7 @@ module Snapshot where
66import Control.Monad (when )
77import Control.Monad.Except
88import Database.SQLite.Simple
9+ import Database.SQLite.Simple.ToField
910import Data.List.Split (splitOn )
1011import Data.Monoid ((<>) )
1112import System.Exit (ExitCode (ExitSuccess , ExitFailure ))
@@ -27,15 +28,20 @@ instance Show SnapshotDate where
2728 show d = printf " %04d-%02d-%02d-%02d%02d%02d" (year d) (month d) (day d)
2829 (hour d) (minute d) (second d)
2930
30- -- What categories the current snapshot will fall under
31- data SnapshotType = SnapshotType
32- { yearly :: Bool
33- , monthly :: Bool
34- , weekly :: Bool
35- , daily :: Bool
36- , hourly :: Bool
37- , quater_hourly :: Bool
38- } deriving (Show )
31+ data SnapshotType = Yearly
32+ | Monthly
33+ | Weekly
34+ | Daily
35+ | Hourly
36+ | QuaterHourly
37+
38+ instance ToField SnapshotType where
39+ toField Yearly = toField (" yearly" :: String )
40+ toField Monthly = toField (" monthly" :: String )
41+ toField Weekly = toField (" weekly" :: String )
42+ toField Daily = toField (" daily" :: String )
43+ toField Hourly = toField (" hourly" :: String )
44+ toField QuaterHourly = toField (" quater_hourly" :: String )
3945
4046
4147parseSnapshotDates :: (MonadError String m ) => [String ] -> m [SnapshotDate ]
@@ -88,8 +94,8 @@ instance FromRow IdField where
8894
8995-- TODO switch to opaleye or something that gives us better type support
9096-- storeSnapshot :: (MonadError String m, MonadIO m) -> SnapshotDate -> SnapshotType -> m ()
91- storeSnapshot :: SnapshotDate -> SnapshotType -> IO ()
92- storeSnapshot s flags = do
97+ storeSnapshot :: SnapshotDate -> [ SnapshotType ] -> IO ()
98+ storeSnapshot s types = do
9399 -- Open db with foreign key support
94100 conn <- open " apfs-auto-snapshot.db"
95101 execute_ conn " PRAGMA foreign_keys = ON"
@@ -98,34 +104,16 @@ storeSnapshot s flags = do
98104 execute conn " INSERT INTO snapshots (name) VALUES (?)"
99105 (Only (show s :: String ))
100106 res <- query_ conn " SELECT last_insert_rowid()" :: IO [IdField ]
101- let snapshot_id = primaryKey $ head res
102-
103- -- Insert the types of snapshots this is for in the timelines. Can be
104- -- multiple timelines at the same time.
105- when (yearly flags)
106- (execute conn
107- " INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'yearly')"
108- (Only (show snapshot_id :: String )))
109- when (monthly flags)
110- (execute conn
111- " INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'monthly')"
112- (Only (show snapshot_id :: String )))
113- when (weekly flags)
114- (execute conn
115- " INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'weekly')"
116- (Only (show snapshot_id :: String )))
117- when (daily flags)
118- (execute conn
119- " INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'daily')"
120- (Only (show snapshot_id :: String )))
121- when (hourly flags)
122- (execute conn
123- " INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'hourly')"
124- (Only (show snapshot_id :: String )))
125- when (quater_hourly flags)
126- (execute conn
127- " INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, 'quater-hourly')"
128- (Only (show snapshot_id :: String )))
107+ let snapshotId = primaryKey $ head res
108+
109+ -- Insert what type of snapshot this is
110+ mapM_ (storeSnapshotType conn snapshotId) types
129111
130112 -- Close the connection
131113 close conn
114+ where
115+ storeSnapshotType :: Connection -> Int -> SnapshotType -> IO ()
116+ storeSnapshotType conn snapshotId snapshotType = execute conn query args
117+ where
118+ query = " INSERT INTO timelines (snapshot_id, snapshot_type) VALUES (?, ?)"
119+ args = (snapshotId, snapshotType)
0 commit comments