diff --git a/src/Simplex/Messaging/Agent/Store/SQLite.hs b/src/Simplex/Messaging/Agent/Store/SQLite.hs index 89650f2e4..3617caa6e 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite.hs @@ -7,3 +7,58 @@ import qualified Database.SQLite.Simple as DB -- createRcvConn conn connAlias q = do -- id <- query conn "INSERT ..." -- query conn "INSERT ..." + + + +-- sqlite queries to create server, queue and connection + +-- *** step 1 - insert server before create request to server +-- ! "INSERT OR REPLACE INTO" with autoincrement apparently would change id, +-- ! so going with "ON CONFLICT UPDATE" here + +-- INSERT INTO servers (host_address, port, key_hash) +-- VALUES ({host_address}, {port}, {key_hash}) +-- ON CONFLICT(host_address, port) DO UPDATE SET +-- host_address=excluded.host_address, +-- port=excluded.port, +-- key_hash=excluded.key_hash; + +-- *** step 2 - insert queue and connection after server's response +-- BEGIN TRANSACTION; + +-- INSERT INTO recipient_queues ( +-- server_id, +-- rcv_id, +-- rcv_private_key, +-- snd_id, +-- snd_key, +-- decrypt_key, +-- verify_key, +-- status, +-- ack_mode +-- ) +-- VALUES ( +-- {server_id}, +-- {rcv_id}, +-- {rcv_private_key}, +-- {snd_id}, +-- {snd_key}, +-- {decrypt_key}, +-- {verify_key}, +-- {status}, +-- {ack_mode} +-- ); + +-- INSERT INTO connections ( +-- conn_alias, +-- recipient_queue_id, +-- sender_queue_id +-- ) +-- VALUES ( +-- {conn_alias}, +-- {recipient_queue_id}, +-- {sender_queue_id} +-- ); + +-- COMMIT; +-- *** diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Schema.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Schema.hs index d5ef6e891..1188514b9 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Schema.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Schema.hs @@ -12,8 +12,11 @@ servers = [s| CREATE TABLE IF NOT EXISTS servers ( server_id INTEGER PRIMARY KEY, - host_address TEXT - ) + host_address TEXT, + port INT, + key_hash BLOB, + UNIQUE (host_address, port) + ) |] -- TODO unique constraints on (server_id, rcv_id) and (server_id, snd_id)