From c306e9bcd3ae52bd2b44fed1d74aab37a4d9173d Mon Sep 17 00:00:00 2001 From: shum Date: Thu, 2 Apr 2026 12:45:52 +0000 Subject: [PATCH] fix: check for existing data before database import importFileStore now checks if the target database already contains files and aborts with an error. Previously, importing into a non-empty database would fail mid-COPY on duplicate primary keys, leaving the database in a partially imported state. --- src/Simplex/FileTransfer/Server/Store/Postgres.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Simplex/FileTransfer/Server/Store/Postgres.hs b/src/Simplex/FileTransfer/Server/Store/Postgres.hs index 30f75d7df..8922149bf 100644 --- a/src/Simplex/FileTransfer/Server/Store/Postgres.hs +++ b/src/Simplex/FileTransfer/Server/Store/Postgres.hs @@ -241,6 +241,11 @@ importFileStore storeLogFilePath dbCfg = do putStrLn $ "Loaded " <> show fileCount <> " files, " <> show rcpCount <> " recipients." let dbCfg' = dbCfg {dbOpts = (dbOpts dbCfg) {createSchema = True}, confirmMigrations = MCYesUp} pgStore <- newFileStore dbCfg' :: IO PostgresFileStore + existingCount <- getFileCount pgStore + when (existingCount > 0) $ do + putStrLn $ "WARNING: database already contains " <> show existingCount <> " files. Import will fail on duplicate keys." + putStrLn "Drop the existing schema first or use a fresh database." + exitFailure putStrLn "Importing files..." fCnt <- withTransaction (dbStore pgStore) $ \db -> do DB.copy_