Skip to content

Commit 318ddf6

Browse files
authored
smp server: import/export to/from PostgreSQL with one command, deprecation notice for journal storage (#1653)
1 parent 11a4859 commit 318ddf6

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/Simplex/Messaging/Server/Env/STM.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,10 @@ newEnv config@ServerConfig {smpCredentials, httpCredentials, serverStoreCfg, smp
567567
forM_ storePaths_ $ \StorePaths {storeLogFile = f} -> loadStoreLog (mkQueue ms True) f $ queueStore ms
568568
pure $ StoreMemory ms
569569
SSCMemoryJournal {storeLogFile, storeMsgsPath} -> do
570+
logWarn $
571+
"Journal message store is deprecated and will be removed soon.\n"
572+
<> "Please migrate to in-memory storage using `journal export` command.\n"
573+
<> "After that you can migrate to PostgreSQL using `database import` command."
570574
let qsCfg = MQStoreCfg
571575
cfg = mkJournalStoreConfig qsCfg storeMsgsPath msgQueueQuota maxJournalMsgCount maxJournalStateLines idleQueueInterval
572576
ms <- newMsgStore cfg

src/Simplex/Messaging/Server/Main.hs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

761801
data StoreCmd = SCImport | SCExport | SCDelete
762802

763-
data DatabaseTable = DTQueues | DTMessages
803+
data DatabaseTable = DTQueues | DTMessages | DTAll
764804

765805
instance 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

775817
cliCommandP :: 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

Comments
 (0)