mirror of
https://github.com/simplex-chat/simplexmq.git
synced 2026-11-04 06:33:14 +00:00
agent: autoincrement IDs in sqlite (#1951)
Co-authored-by: Evgeny @ SimpleX Chat <259188159+evgeny-simplex@users.noreply.github.com>
This commit is contained in:
1 parent
793f6721f6
commit
aabf4cdab9
4 files changed
+93
-7
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|]
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user