diff --git a/simplex-chat.cabal b/simplex-chat.cabal index cc98e1a8f4..916cceb589 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -136,6 +136,7 @@ library Simplex.Chat.Migrations.M20240122_indexes Simplex.Chat.Migrations.M20240214_redirect_file_id Simplex.Chat.Migrations.M20240222_app_settings + Simplex.Chat.Migrations.M20240226_users_restrict Simplex.Chat.Mobile Simplex.Chat.Mobile.File Simplex.Chat.Mobile.Shared diff --git a/src/Simplex/Chat/Migrations/M20240226_users_restrict.hs b/src/Simplex/Chat/Migrations/M20240226_users_restrict.hs new file mode 100644 index 0000000000..a68923142c --- /dev/null +++ b/src/Simplex/Chat/Migrations/M20240226_users_restrict.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Chat.Migrations.M20240226_users_restrict where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20240226_users_restrict :: Query +m20240226_users_restrict = + [sql| +PRAGMA writable_schema=1; + +UPDATE sqlite_master +SET sql = replace(sql, 'ON DELETE CASCADE', 'ON DELETE RESTRICT') +WHERE name = 'users' AND type = 'table'; + +PRAGMA writable_schema=0; +|] + +down_m20240226_users_restrict :: Query +down_m20240226_users_restrict = + [sql| +PRAGMA writable_schema=1; + +UPDATE sqlite_master +SET sql = replace(sql, 'ON DELETE RESTRICT', 'ON DELETE CASCADE') +WHERE name = 'users' AND type = 'table'; + +PRAGMA writable_schema=0; +|] diff --git a/src/Simplex/Chat/Migrations/chat_schema.sql b/src/Simplex/Chat/Migrations/chat_schema.sql index 36f01d06b1..98b9cfcc12 100644 --- a/src/Simplex/Chat/Migrations/chat_schema.sql +++ b/src/Simplex/Chat/Migrations/chat_schema.sql @@ -22,7 +22,7 @@ CREATE TABLE contact_profiles( ); CREATE TABLE users( user_id INTEGER PRIMARY KEY, - contact_id INTEGER NOT NULL UNIQUE REFERENCES contacts ON DELETE CASCADE + contact_id INTEGER NOT NULL UNIQUE REFERENCES contacts ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED, local_display_name TEXT NOT NULL UNIQUE, active_user INTEGER NOT NULL DEFAULT 0, @@ -37,7 +37,7 @@ CREATE TABLE users( user_member_profile_updated_at TEXT, -- 1 for active user FOREIGN KEY(user_id, local_display_name) REFERENCES display_names(user_id, local_display_name) - ON DELETE CASCADE + ON DELETE RESTRICT ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED ); diff --git a/src/Simplex/Chat/Store/Migrations.hs b/src/Simplex/Chat/Store/Migrations.hs index 32b003afd6..6d3a7a9a4f 100644 --- a/src/Simplex/Chat/Store/Migrations.hs +++ b/src/Simplex/Chat/Store/Migrations.hs @@ -100,6 +100,7 @@ import Simplex.Chat.Migrations.M20240115_block_member_for_all import Simplex.Chat.Migrations.M20240122_indexes import Simplex.Chat.Migrations.M20240214_redirect_file_id import Simplex.Chat.Migrations.M20240222_app_settings +import Simplex.Chat.Migrations.M20240226_users_restrict import Simplex.Messaging.Agent.Store.SQLite.Migrations (Migration (..)) schemaMigrations :: [(String, Query, Maybe Query)] @@ -199,7 +200,8 @@ schemaMigrations = ("20240115_block_member_for_all", m20240115_block_member_for_all, Just down_m20240115_block_member_for_all), ("20240122_indexes", m20240122_indexes, Just down_m20240122_indexes), ("20240214_redirect_file_id", m20240214_redirect_file_id, Just down_m20240214_redirect_file_id), - ("20240222_app_settings", m20240222_app_settings, Just down_m20240222_app_settings) + ("20240222_app_settings", m20240222_app_settings, Just down_m20240222_app_settings), + ("20240226_users_restrict", m20240226_users_restrict, Just down_m20240226_users_restrict) ] -- | The list of migrations in ascending order by date