diff --git a/simplexmq.cabal b/simplexmq.cabal index 4dd8d9101..b963ed431 100644 --- a/simplexmq.cabal +++ b/simplexmq.cabal @@ -218,6 +218,7 @@ library Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251009_queue_to_subscribe Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251010_client_notices Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251230_strict_tables + Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251231_strict_tables_2 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 26df43bc8..c44e84b05 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/App.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/App.hs @@ -47,6 +47,7 @@ import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20250702_conn_invitation import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251009_queue_to_subscribe import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251010_client_notices import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251230_strict_tables +import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251231_strict_tables_2 import Simplex.Messaging.Agent.Store.Shared (Migration (..)) schemaMigrations :: [(String, Query, Maybe Query)] @@ -93,7 +94,8 @@ schemaMigrations = ("m20250702_conn_invitations_remove_cascade_delete", m20250702_conn_invitations_remove_cascade_delete, Just down_m20250702_conn_invitations_remove_cascade_delete), ("m20251009_queue_to_subscribe", m20251009_queue_to_subscribe, Just down_m20251009_queue_to_subscribe), ("m20251010_client_notices", m20251010_client_notices, Just down_m20251010_client_notices), - ("m20251230_strict_tables", m20251230_strict_tables, Just down_m20251230_strict_tables) + ("m20251230_strict_tables", m20251230_strict_tables, Just down_m20251230_strict_tables), + ("m20251231_strict_tables_2", m20251231_strict_tables_2, Just down_m20251231_strict_tables_2) ] -- | The list of migrations in ascending order by date diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20240624_snd_secure.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20240624_snd_secure.hs index 7f82d4ecf..2331dd4aa 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20240624_snd_secure.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20240624_snd_secure.hs @@ -17,7 +17,7 @@ UPDATE sqlite_master SET sql = replace(sql, 'sender_key BLOB NOT NULL,', 'sender_key BLOB,') WHERE name = 'conn_confirmations' AND type = 'table'; -PRAGMA writable_schema=0; +PRAGMA writable_schema=RESET; |] down_m20240624_snd_secure :: Query @@ -32,5 +32,5 @@ UPDATE sqlite_master SET sql = replace(sql, 'sender_key BLOB,', 'sender_key BLOB NOT NULL,') WHERE name = 'conn_confirmations' AND type = 'table'; -PRAGMA writable_schema=0; +PRAGMA writable_schema=RESET; |] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20250702_conn_invitations_remove_cascade_delete.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20250702_conn_invitations_remove_cascade_delete.hs index 9abccb476..72d2502e1 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20250702_conn_invitations_remove_cascade_delete.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20250702_conn_invitations_remove_cascade_delete.hs @@ -18,7 +18,7 @@ SET sql = replace( ) WHERE name = 'conn_invitations' AND type = 'table'; -PRAGMA writable_schema=0; +PRAGMA writable_schema=RESET; |] down_m20250702_conn_invitations_remove_cascade_delete :: Query @@ -34,5 +34,5 @@ SET sql = replace( ) WHERE name = 'conn_invitations' AND type = 'table'; -PRAGMA writable_schema=0; +PRAGMA writable_schema=RESET; |] diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251230_strict_tables.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251230_strict_tables.hs index 3d05e054a..ec5a1f06c 100644 --- a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251230_strict_tables.hs +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251230_strict_tables.hs @@ -13,43 +13,11 @@ UPDATE ntf_tokens SET ntf_mode = CAST(ntf_mode as TEXT); UPDATE ntf_subscriptions SET ntf_sub_action = CAST(ntf_sub_action as TEXT), ntf_sub_smp_action = CAST(ntf_sub_smp_action as TEXT); - -PRAGMA writable_schema=1; - -UPDATE sqlite_master -SET sql = CASE - WHEN LOWER(SUBSTR(sql, -15)) = ') without rowid' THEN sql || ', STRICT' - WHEN SUBSTR(sql, -1) = ')' THEN sql || ' STRICT' - ELSE sql -END -WHERE type = 'table' AND name != 'sqlite_sequence'; - -UPDATE sqlite_master -SET sql = replace(sql, 'device_token TEXT NOT NULL', 'device_token BLOB NOT NULL') -WHERE type = 'table' AND name = 'ntf_tokens'; - -PRAGMA writable_schema=0; |] down_m20251230_strict_tables :: Query down_m20251230_strict_tables = [sql| -PRAGMA writable_schema=1; - -UPDATE sqlite_master -SET sql = CASE - WHEN LOWER(SUBSTR(sql, -8)) = ', strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 8) - WHEN LOWER(SUBSTR(sql, -7)) = ' strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 7) - ELSE sql -END -WHERE type = 'table' AND name != 'sqlite_sequence'; - -UPDATE sqlite_master -SET sql = replace(sql, 'device_token BLOB NOT NULL', 'device_token TEXT NOT NULL') -WHERE type = 'table' AND name = 'ntf_tokens'; - -PRAGMA writable_schema=0; - UPDATE ntf_tokens SET ntf_mode = CAST(ntf_mode as BLOB); UPDATE ntf_subscriptions diff --git a/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251231_strict_tables_2.hs b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251231_strict_tables_2.hs new file mode 100644 index 000000000..f5af30de2 --- /dev/null +++ b/src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251231_strict_tables_2.hs @@ -0,0 +1,46 @@ +{-# LANGUAGE QuasiQuotes #-} + +module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251231_strict_tables_2 where + +import Database.SQLite.Simple (Query) +import Database.SQLite.Simple.QQ (sql) + +m20251231_strict_tables_2 :: Query +m20251231_strict_tables_2 = + [sql| +PRAGMA writable_schema=1; + +UPDATE sqlite_master +SET sql = replace(sql, 'device_token TEXT NOT NULL', 'device_token BLOB NOT NULL') +WHERE type = 'table' AND name = 'ntf_tokens'; + +UPDATE sqlite_master +SET sql = CASE + WHEN LOWER(SUBSTR(sql, -15)) = ') without rowid' THEN sql || ', STRICT' + WHEN SUBSTR(sql, -1) = ')' THEN sql || ' STRICT' + ELSE sql +END +WHERE type = 'table' AND name != 'sqlite_sequence'; + +PRAGMA writable_schema=RESET; +|] + +down_m20251231_strict_tables_2 :: Query +down_m20251231_strict_tables_2 = + [sql| +PRAGMA writable_schema=1; + +UPDATE sqlite_master +SET sql = CASE + WHEN LOWER(SUBSTR(sql, -8)) = ', strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 8) + WHEN LOWER(SUBSTR(sql, -7)) = ' strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 7) + ELSE sql +END +WHERE type = 'table' AND name != 'sqlite_sequence'; + +UPDATE sqlite_master +SET sql = replace(sql, 'device_token BLOB NOT NULL', 'device_token TEXT NOT NULL') +WHERE type = 'table' AND name = 'ntf_tokens'; + +PRAGMA writable_schema=RESET; +|] diff --git a/tests/AgentTests/SchemaDump.hs b/tests/AgentTests/SchemaDump.hs index d9aa79513..f2fe11aac 100644 --- a/tests/AgentTests/SchemaDump.hs +++ b/tests/AgentTests/SchemaDump.hs @@ -80,7 +80,7 @@ testSchemaMigrations = do schema <- getSchema testDB testSchema Migrations.run st Nothing True $ MTRUp [m] schema' <- getSchema testDB testSchema - schema' `shouldNotBe` schema + unless (name m `elem` noSchemaChange) $ schema' `shouldNotBe` schema Migrations.run st Nothing True $ MTRDown [downMigr] unless (name m `elem` skipComparisonForDownMigrations) $ do schema'' <- getSchema testDB testSchema @@ -114,6 +114,11 @@ testUsersMigrationOld = do `shouldReturn` ([Only (1 :: Int)]) closeDBStore st' +noSchemaChange :: [String] +noSchemaChange = + [ "m20251230_strict_tables" + ] + skipComparisonForDownMigrations :: [String] skipComparisonForDownMigrations = [ -- on down migration idx_messages_internal_snd_id_ts index moves down to the end of the file