Skip to content

Commit 51fa33c

Browse files
committed
agent: split migration to strict tables
1 parent a7b43b1 commit 51fa33c

File tree

5 files changed

+56
-34
lines changed

5 files changed

+56
-34
lines changed

simplexmq.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ library
218218
Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251009_queue_to_subscribe
219219
Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251010_client_notices
220220
Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251230_strict_tables
221+
Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251231_strict_tables_2
221222
Simplex.Messaging.Agent.Store.SQLite.Util
222223
if flag(client_postgres) || flag(server_postgres)
223224
exposed-modules:

src/Simplex/Messaging/Agent/Store/SQLite/Migrations/App.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20250702_conn_invitation
4747
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251009_queue_to_subscribe
4848
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251010_client_notices
4949
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251230_strict_tables
50+
import Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251231_strict_tables_2
5051
import Simplex.Messaging.Agent.Store.Shared (Migration (..))
5152

5253
schemaMigrations :: [(String, Query, Maybe Query)]
@@ -93,7 +94,8 @@ schemaMigrations =
9394
("m20250702_conn_invitations_remove_cascade_delete", m20250702_conn_invitations_remove_cascade_delete, Just down_m20250702_conn_invitations_remove_cascade_delete),
9495
("m20251009_queue_to_subscribe", m20251009_queue_to_subscribe, Just down_m20251009_queue_to_subscribe),
9596
("m20251010_client_notices", m20251010_client_notices, Just down_m20251010_client_notices),
96-
("m20251230_strict_tables", m20251230_strict_tables, Just down_m20251230_strict_tables)
97+
("m20251230_strict_tables", m20251230_strict_tables, Just down_m20251230_strict_tables),
98+
("m20251231_strict_tables_2", m20251231_strict_tables_2, Just down_m20251231_strict_tables_2)
9799
]
98100

99101
-- | The list of migrations in ascending order by date

src/Simplex/Messaging/Agent/Store/SQLite/Migrations/M20251230_strict_tables.hs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,11 @@ UPDATE ntf_tokens SET ntf_mode = CAST(ntf_mode as TEXT);
1313
UPDATE ntf_subscriptions
1414
SET ntf_sub_action = CAST(ntf_sub_action as TEXT),
1515
ntf_sub_smp_action = CAST(ntf_sub_smp_action as TEXT);
16-
17-
PRAGMA writable_schema=1;
18-
19-
UPDATE sqlite_master
20-
SET sql = CASE
21-
WHEN LOWER(SUBSTR(sql, -15)) = ') without rowid' THEN sql || ', STRICT'
22-
WHEN SUBSTR(sql, -1) = ')' THEN sql || ' STRICT'
23-
ELSE sql
24-
END
25-
WHERE type = 'table' AND name != 'sqlite_sequence';
26-
27-
UPDATE sqlite_master
28-
SET sql = replace(sql, 'device_token TEXT NOT NULL', 'device_token BLOB NOT NULL')
29-
WHERE type = 'table' AND name = 'ntf_tokens';
30-
31-
PRAGMA writable_schema=0;
3216
|]
3317

3418
down_m20251230_strict_tables :: Query
3519
down_m20251230_strict_tables =
3620
[sql|
37-
PRAGMA writable_schema=1;
38-
39-
UPDATE sqlite_master
40-
SET sql = CASE
41-
WHEN LOWER(SUBSTR(sql, -8)) = ', strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 8)
42-
WHEN LOWER(SUBSTR(sql, -7)) = ' strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 7)
43-
ELSE sql
44-
END
45-
WHERE type = 'table' AND name != 'sqlite_sequence';
46-
47-
UPDATE sqlite_master
48-
SET sql = replace(sql, 'device_token BLOB NOT NULL', 'device_token TEXT NOT NULL')
49-
WHERE type = 'table' AND name = 'ntf_tokens';
50-
51-
PRAGMA writable_schema=0;
52-
5321
UPDATE ntf_tokens SET ntf_mode = CAST(ntf_mode as BLOB);
5422

5523
UPDATE ntf_subscriptions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{-# LANGUAGE QuasiQuotes #-}
2+
3+
module Simplex.Messaging.Agent.Store.SQLite.Migrations.M20251231_strict_tables_2 where
4+
5+
import Database.SQLite.Simple (Query)
6+
import Database.SQLite.Simple.QQ (sql)
7+
8+
m20251231_strict_tables_2 :: Query
9+
m20251231_strict_tables_2 =
10+
[sql|
11+
PRAGMA writable_schema=1;
12+
13+
UPDATE sqlite_master
14+
SET sql = replace(sql, 'device_token TEXT NOT NULL', 'device_token BLOB NOT NULL')
15+
WHERE type = 'table' AND name = 'ntf_tokens';
16+
17+
UPDATE sqlite_master
18+
SET sql = CASE
19+
WHEN LOWER(SUBSTR(sql, -15)) = ') without rowid' THEN sql || ', STRICT'
20+
WHEN SUBSTR(sql, -1) = ')' THEN sql || ' STRICT'
21+
ELSE sql
22+
END
23+
WHERE type = 'table' AND name != 'sqlite_sequence';
24+
25+
PRAGMA writable_schema=0;
26+
|]
27+
28+
down_m20251231_strict_tables_2 :: Query
29+
down_m20251231_strict_tables_2 =
30+
[sql|
31+
PRAGMA writable_schema=1;
32+
33+
UPDATE sqlite_master
34+
SET sql = CASE
35+
WHEN LOWER(SUBSTR(sql, -8)) = ', strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 8)
36+
WHEN LOWER(SUBSTR(sql, -7)) = ' strict' THEN SUBSTR(sql, 1, LENGTH(sql) - 7)
37+
ELSE sql
38+
END
39+
WHERE type = 'table' AND name != 'sqlite_sequence';
40+
41+
UPDATE sqlite_master
42+
SET sql = replace(sql, 'device_token BLOB NOT NULL', 'device_token TEXT NOT NULL')
43+
WHERE type = 'table' AND name = 'ntf_tokens';
44+
45+
PRAGMA writable_schema=0;
46+
|]

tests/AgentTests/SchemaDump.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ testSchemaMigrations = do
8080
schema <- getSchema testDB testSchema
8181
Migrations.run st Nothing True $ MTRUp [m]
8282
schema' <- getSchema testDB testSchema
83-
schema' `shouldNotBe` schema
83+
unless (name m `elem` noSchemaChange) $ schema' `shouldNotBe` schema
8484
Migrations.run st Nothing True $ MTRDown [downMigr]
8585
unless (name m `elem` skipComparisonForDownMigrations) $ do
8686
schema'' <- getSchema testDB testSchema
@@ -114,6 +114,11 @@ testUsersMigrationOld = do
114114
`shouldReturn` ([Only (1 :: Int)])
115115
closeDBStore st'
116116

117+
noSchemaChange :: [String]
118+
noSchemaChange =
119+
[ "m20251230_strict_tables"
120+
]
121+
117122
skipComparisonForDownMigrations :: [String]
118123
skipComparisonForDownMigrations =
119124
[ -- on down migration idx_messages_internal_snd_id_ts index moves down to the end of the file

0 commit comments

Comments
 (0)