From 90d915d28e2c4b5f45a7a64262920f5b019bc25d Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:41:59 +0000 Subject: [PATCH] agent: autoincrement IDs in sqlite --- simplexmq.cabal | 1 + .../Agent/Store/SQLite/Migrations/App.hs | 4 +- .../M20261018_xftp_autoincrement.hs | 83 +++++++++++++++++++ .../Store/SQLite/Migrations/agent_schema.sql | 12 +-- 4 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20261018_xftp_autoincrement.hs diff --git a/simplexmq.cabal b/simplexmq.cabal index ec92f3f34..ea25ebb72 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -255,6 +255,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20260929_ratchet_indexes Simplex.Messaging.Agent.Store.SQLite.Migrations.M20261005_commands_autoincrement Simplex.Messaging.Agent.Store.SQLite.Migrations.M20261008_rcv_switch_status + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20261018_xftp_autoincrement Simplex.Messaging.Agent.Store.SQLite.Util if flag(client_postgres) || flag(server_postgres) exposed-modules: diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/App.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/App.hs index 5dbd314f9..eb0275116 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/App.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/App.hs @@ -55,6 +55,7 @@ import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20260919_ratchet_verify_ import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20260929_ratchet_indexes import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20261005_commands_autoincrement import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20261008_rcv_switch_status +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20261018_xftp_autoincrement import Simplex.Messaging.Agent.Store.Shared (Migration (..)) schemaMigrations :: [(String, Query, Maybe Query)] @@ -109,7 +110,8 @@ schemaMigrations = ("m20260919_ratchet_verify_codes", m20260919_ratchet_verify_codes, Just down_m20260919_ratchet_verify_codes), ("m20260929_ratchet_indexes", m20260929_ratchet_indexes, Just down_m20260929_ratchet_indexes), ("m20261005_commands_autoincrement", m20261005_commands_autoincrement, Just down_m20261005_commands_autoincrement), - ("m20261008_rcv_switch_status", m20261008_rcv_switch_status, Just down_m20261008_rcv_switch_status) + ("m20261008_rcv_switch_status", m20261008_rcv_switch_status, Just down_m20261008_rcv_switch_status), + ("m20261018_xftp_autoincrement", m20261018_xftp_autoincrement, Just down_m20261018_xftp_autoincrement) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20261018_xftp_autoincrement.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20261018_xftp_autoincrement.hs new file mode 100644 index 000000000..133c5e987 --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20261018_xftp_autoincrement.hs @@ -0,0 +1,83 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20261018_xftp_autoincrement where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20261018_xftp_autoincrement :: Query +m20261018_xftp_autoincrement = + [sql| +INSERT INTO sqlite_sequence (name, seq) +SELECT 'rcv_files', MAX(ROWID) FROM rcv_files; + +INSERT INTO sqlite_sequence (name, seq) +SELECT 'rcv_file_chunks', MAX(ROWID) FROM rcv_file_chunks; + +INSERT INTO sqlite_sequence (name, seq) +SELECT 'rcv_file_chunk_replicas', MAX(ROWID) FROM rcv_file_chunk_replicas; + +INSERT INTO sqlite_sequence (name, seq) +SELECT 'snd_files', MAX(ROWID) FROM snd_files; + +INSERT INTO sqlite_sequence (name, seq) +SELECT 'snd_file_chunks', MAX(ROWID) FROM snd_file_chunks; + +INSERT INTO sqlite_sequence (name, seq) +SELECT 'snd_file_chunk_replicas', MAX(ROWID) FROM snd_file_chunk_replicas; + +PRAGMA writable_schema=1; + +UPDATE sqlite_master SET sql = replace(sql, 'rcv_file_id INTEGER PRIMARY KEY,', 'rcv_file_id INTEGER PRIMARY KEY AUTOINCREMENT,') +WHERE name = 'rcv_files' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'rcv_file_chunk_id INTEGER PRIMARY KEY,', 'rcv_file_chunk_id INTEGER PRIMARY KEY AUTOINCREMENT,') +WHERE name = 'rcv_file_chunks' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'rcv_file_chunk_replica_id INTEGER PRIMARY KEY,', 'rcv_file_chunk_replica_id INTEGER PRIMARY KEY AUTOINCREMENT,') +WHERE name = 'rcv_file_chunk_replicas' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'snd_file_id INTEGER PRIMARY KEY,', 'snd_file_id INTEGER PRIMARY KEY AUTOINCREMENT,') +WHERE name = 'snd_files' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'snd_file_chunk_id INTEGER PRIMARY KEY,', 'snd_file_chunk_id INTEGER PRIMARY KEY AUTOINCREMENT,') +WHERE name = 'snd_file_chunks' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'snd_file_chunk_replica_id INTEGER PRIMARY KEY,', 'snd_file_chunk_replica_id INTEGER PRIMARY KEY AUTOINCREMENT,') +WHERE name = 'snd_file_chunk_replicas' AND type = 'table'; + +PRAGMA writable_schema=RESET; + |] + +down_m20261018_xftp_autoincrement :: Query +down_m20261018_xftp_autoincrement = + [sql| +DELETE FROM sqlite_sequence WHERE name = 'rcv_files'; +DELETE FROM sqlite_sequence WHERE name = 'rcv_file_chunks'; +DELETE FROM sqlite_sequence WHERE name = 'rcv_file_chunk_replicas'; +DELETE FROM sqlite_sequence WHERE name = 'snd_files'; +DELETE FROM sqlite_sequence WHERE name = 'snd_file_chunks'; +DELETE FROM sqlite_sequence WHERE name = 'snd_file_chunk_replicas'; + +PRAGMA writable_schema=1; + +UPDATE sqlite_master SET sql = replace(sql, 'rcv_file_id INTEGER PRIMARY KEY AUTOINCREMENT,', 'rcv_file_id INTEGER PRIMARY KEY,') +WHERE name = 'rcv_files' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'rcv_file_chunk_id INTEGER PRIMARY KEY AUTOINCREMENT,', 'rcv_file_chunk_id INTEGER PRIMARY KEY,') +WHERE name = 'rcv_file_chunks' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'rcv_file_chunk_replica_id INTEGER PRIMARY KEY AUTOINCREMENT,', 'rcv_file_chunk_replica_id INTEGER PRIMARY KEY,') +WHERE name = 'rcv_file_chunk_replicas' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'snd_file_id INTEGER PRIMARY KEY AUTOINCREMENT,', 'snd_file_id INTEGER PRIMARY KEY,') +WHERE name = 'snd_files' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'snd_file_chunk_id INTEGER PRIMARY KEY AUTOINCREMENT,', 'snd_file_chunk_id INTEGER PRIMARY KEY,') +WHERE name = 'snd_file_chunks' AND type = 'table'; + +UPDATE sqlite_master SET sql = replace(sql, 'snd_file_chunk_replica_id INTEGER PRIMARY KEY AUTOINCREMENT,', 'snd_file_chunk_replica_id INTEGER PRIMARY KEY,') +WHERE name = 'snd_file_chunk_replicas' AND type = 'table'; + +PRAGMA writable_schema=RESET; + |] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql index cdbcee7dd..7e97dd2d0 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/agent_schema.sql @@ -285,7 +285,7 @@ CREATE TABLE xftp_servers( UNIQUE(xftp_host, xftp_port, xftp_key_hash) ) STRICT; CREATE TABLE rcv_files( - rcv_file_id INTEGER PRIMARY KEY, + rcv_file_id INTEGER PRIMARY KEY AUTOINCREMENT, rcv_file_entity_id BLOB NOT NULL, user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE, size INTEGER NOT NULL, @@ -312,7 +312,7 @@ CREATE TABLE rcv_files( UNIQUE(rcv_file_entity_id) ) STRICT; CREATE TABLE rcv_file_chunks( - rcv_file_chunk_id INTEGER PRIMARY KEY, + rcv_file_chunk_id INTEGER PRIMARY KEY AUTOINCREMENT, rcv_file_id INTEGER NOT NULL REFERENCES rcv_files ON DELETE CASCADE, chunk_no INTEGER NOT NULL, chunk_size INTEGER NOT NULL, @@ -322,7 +322,7 @@ CREATE TABLE rcv_file_chunks( updated_at TEXT NOT NULL DEFAULT(datetime('now')) ) STRICT; CREATE TABLE rcv_file_chunk_replicas( - rcv_file_chunk_replica_id INTEGER PRIMARY KEY, + rcv_file_chunk_replica_id INTEGER PRIMARY KEY AUTOINCREMENT, rcv_file_chunk_id INTEGER NOT NULL REFERENCES rcv_file_chunks ON DELETE CASCADE, replica_number INTEGER NOT NULL, xftp_server_id INTEGER NOT NULL REFERENCES xftp_servers ON DELETE CASCADE, @@ -335,7 +335,7 @@ CREATE TABLE rcv_file_chunk_replicas( updated_at TEXT NOT NULL DEFAULT(datetime('now')) ) STRICT; CREATE TABLE snd_files( - snd_file_id INTEGER PRIMARY KEY, + snd_file_id INTEGER PRIMARY KEY AUTOINCREMENT, snd_file_entity_id BLOB NOT NULL, user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE, num_recipients INTEGER NOT NULL, @@ -358,7 +358,7 @@ CREATE TABLE snd_files( storage_time INTEGER ) STRICT; CREATE TABLE snd_file_chunks( - snd_file_chunk_id INTEGER PRIMARY KEY, + snd_file_chunk_id INTEGER PRIMARY KEY AUTOINCREMENT, snd_file_id INTEGER NOT NULL REFERENCES snd_files ON DELETE CASCADE, chunk_no INTEGER NOT NULL, chunk_offset INTEGER NOT NULL, @@ -368,7 +368,7 @@ CREATE TABLE snd_file_chunks( updated_at TEXT NOT NULL DEFAULT(datetime('now')) ) STRICT; CREATE TABLE snd_file_chunk_replicas( - snd_file_chunk_replica_id INTEGER PRIMARY KEY, + snd_file_chunk_replica_id INTEGER PRIMARY KEY AUTOINCREMENT, snd_file_chunk_id INTEGER NOT NULL REFERENCES snd_file_chunks ON DELETE CASCADE, replica_number INTEGER NOT NULL, xftp_server_id INTEGER NOT NULL REFERENCES xftp_servers ON DELETE CASCADE,