@@ -186,6 +186,26 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
186186 storeLogExists <- doesFileExist storeLogFilePath
187187 msgsFileExists <- doesFileExist storeMsgsFilePath
188188 case (cmd, tables) of
189+ (SCImport , DTAll )
190+ | not schemaExists && storeLogExists && msgsFileExists -> do
191+ storeLogFile <- getRequiredStoreLogFile ini
192+ confirmOrExit
193+ (" WARNING: store log file " <> storeLogFile <> " and message log file " <> storeMsgsFilePath <> " will be imported to PostrgreSQL database: " <> B. unpack connstr <> " , schema: " <> B. unpack schema)
194+ " Store logs not imported"
195+ (sCnt, qCnt) <- importStoreLogToDatabase logPath storeLogFile dbOpts
196+ putStrLn $ " Imported: " <> show sCnt <> " services, " <> show qCnt <> " queues"
197+ putStrLn " Importing messages..."
198+ mCnt <- importMessagesToDatabase storeMsgsFilePath dbOpts
199+ putStrLn $ " Import completed: " <> show mCnt <> " messages"
200+ putStrLn $ case readStoreType ini of
201+ Right (ASType SQSPostgres SMSPostgres ) -> " store_queues and store_messages set to `database`, start the server."
202+ Right _ -> " set store_queues and store_messages to `database` in INI file"
203+ Left e -> e <> " , configure storage correctly"
204+ | otherwise -> do
205+ when schemaExists $ putStrLn $ " Schema " <> B. unpack schema <> " already exists in PostrgreSQL database: " <> B. unpack connstr
206+ unless storeLogExists $ putStrLn $ storeLogFilePath <> " file does not exist."
207+ unless msgsFileExists $ putStrLn $ storeMsgsFilePath <> " file does not exist."
208+ exitFailure
189209 (SCImport , DTQueues )
190210 | schemaExists && storeLogExists -> exitConfigureQueueStore connstr schema
191211 | schemaExists -> do
@@ -224,8 +244,27 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
224244 putStrLn $ " Import completed: " <> show mCnt <> " messages"
225245 putStrLn $ case readStoreType ini of
226246 Right (ASType SQSPostgres SMSPostgres ) -> " store_queues and store_messages set to `database`, start the server."
227- Right _ -> " set store_queues and store_messages set to `database` in INI file"
247+ Right _ -> " set store_queues and store_messages to `database` in INI file"
228248 Left e -> e <> " , configure storage correctly"
249+ (SCExport , DTAll )
250+ | schemaExists && not storeLogExists && not msgsFileExists -> do
251+ confirmOrExit
252+ (" WARNING: PostrgreSQL schema " <> B. unpack schema <> " (database: " <> B. unpack connstr <> " ) will be exported to store log file " <> storeLogFilePath <> " and to message log file " <> storeMsgsFilePath)
253+ " Database store not exported"
254+ (sCnt, qCnt) <- exportDatabaseToStoreLog logPath dbOpts storeLogFilePath
255+ putStrLn $ " Exported: " <> show sCnt <> " services, " <> show qCnt <> " queues"
256+ putStrLn " Exporting messages..."
257+ let storeCfg = PostgresStoreCfg {dbOpts, dbStoreLogPath = Nothing , confirmMigrations = MCConsole , deletedTTL = 86400 * defaultDeletedTTL}
258+ ms <- newMsgStore $ PostgresMsgStoreCfg storeCfg defaultMsgQueueQuota
259+ withFile storeMsgsFilePath WriteMode (try . exportDbMessages True ms) >>= \ case
260+ Right mCnt -> putStrLn $ " Export completed: " <> show mCnt <> " messages"
261+ Left (e :: SomeException ) -> putStrLn $ " Error exporting messages: " <> show e
262+ closeMsgStore ms
263+ | otherwise -> do
264+ unless schemaExists $ putStrLn $ " Schema " <> B. unpack schema <> " does not exist in PostrgreSQL database: " <> B. unpack connstr
265+ when storeLogExists $ putStrLn $ storeLogFilePath <> " file already exists."
266+ when msgsFileExists $ putStrLn $ storeMsgsFilePath <> " file already exists."
267+ exitFailure
229268 (SCExport , DTQueues )
230269 | schemaExists && storeLogExists -> exitConfigureQueueStore connstr schema
231270 | not schemaExists -> do
@@ -262,6 +301,7 @@ smpServerCLI_ generateSite serveStaticFiles attachStaticFiles cfgPath logPath =
262301 putStrLn $ " Export completed: " <> show mCnt <> " messages"
263302 putStrLn " Export queues with `smp-server database export queues`"
264303 Left (e :: SomeException ) -> putStrLn $ " Error exporting messages: " <> show e
304+ closeMsgStore ms
265305 (SCDelete , _)
266306 | not schemaExists -> do
267307 putStrLn $ " Schema " <> B. unpack schema <> " does not exist in PostrgreSQL database: " <> B. unpack connstr
@@ -760,16 +800,18 @@ data CliCommand
760800
761801data StoreCmd = SCImport | SCExport | SCDelete
762802
763- data DatabaseTable = DTQueues | DTMessages
803+ data DatabaseTable = DTQueues | DTMessages | DTAll
764804
765805instance StrEncoding DatabaseTable where
766806 strEncode = \ case
767807 DTQueues -> " queues"
768808 DTMessages -> " messages"
809+ DTAll -> " all"
769810 strP =
770811 A. takeTill (== ' ' ) >>= \ case
771812 " queues" -> pure DTQueues
772813 " messages" -> pure DTMessages
814+ " all" -> pure DTAll
773815 _ -> fail " DatabaseTable"
774816
775817cliCommandP :: FilePath -> FilePath -> FilePath -> Parser CliCommand
@@ -940,6 +982,7 @@ cliCommandP cfgPath logPath iniFile =
940982 ( long " table"
941983 <> help " Database tables: queues/messages"
942984 <> metavar " TABLE"
985+ <> value DTAll
943986 )
944987 parseBasicAuth :: ReadM ServerPassword
945988 parseBasicAuth = eitherReader $ fmap ServerPassword . strDecode . B. pack
0 commit comments