Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions simplexmq.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
|]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,5 +34,5 @@ SET sql = replace(
)
WHERE name = 'conn_invitations' AND type = 'table';

PRAGMA writable_schema=0;
PRAGMA writable_schema=RESET;
|]
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
|]
7 changes: 6 additions & 1 deletion tests/AgentTests/SchemaDump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading