Skip to content

Commit 73f4ce0

Browse files
vkatsubavkatsuba
authored and
vkatsuba
committed
Mnesia migration: Fix logic of CT and CTL for correct MSSQL migration
1 parent 3cd6b42 commit 73f4ce0

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

big_tests/tests/mnesia_migration_SUITE.erl

+29-29
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ end_per_testcase(CaseName, Config) ->
113113
%%% ==================================================================
114114

115115
migrate_pubsub_nodes(_Config) ->
116-
Nidx = create_migration_node(),
116+
_ = create_migration_node(),
117117
{ok, _} = ?RPC_MIGRATE(<<"pubsub_nodes">>),
118118
SqlData = #{table => <<"pubsub_nodes">>, where => <<"name='", ?NODE_NAME/binary, "'">>},
119-
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
120-
{selected, [{Nidx}]} ->
119+
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"name">>}) of
120+
{selected, [{?NODE_NAME}]} ->
121121
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
122122
_ = clear_tables(),
123-
ct:comment("Migration of 'pubsub_nodes' is successful for Nidx: ~p", [Nidx]);
123+
ct:comment("Migration of 'pubsub_nodes' is successful for 'name': ~p", [?NODE_NAME]);
124124
Any ->
125125
ct:fail("Unexpected result of 'pubsub_nodes' migration ~p~n", [Any])
126126
end.
@@ -133,12 +133,12 @@ migrate_pubsub_subscriptions(_Config) ->
133133
Nidx = create_migration_node(),
134134
ok = mongoose_helper:successful_rpc(mod_pubsub_db_mnesia, transaction, [#{name => add_subscription, args => [Nidx, ?JID, 'subscribed', <<"0000-0000-0000000">>, []]}]),
135135
{ok, _} = ?RPC_MIGRATE(<<"pubsub_subscriptions">>),
136-
SqlData = #{table => <<"pubsub_subscriptions">>, where => <<"nidx=", (list_to_binary(integer_to_list(Nidx)))/binary>>},
137-
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
138-
{selected, [{Nidx}]} ->
136+
SqlData = #{table => <<"pubsub_subscriptions">>, where => <<"luser='", ?USERNAME/binary, "'">>},
137+
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"luser">>}) of
138+
{selected, [{?USERNAME}]} ->
139139
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
140140
_ = clear_tables(),
141-
ct:comment("Migration of 'pubsub_subscriptions' is successful for Nidx: ~p", [Nidx]);
141+
ct:comment("Migration of 'pubsub_subscriptions' is successful for 'luser': ~p", [?USERNAME]);
142142
Any ->
143143
ct:fail("Unexpected result of 'pubsub_subscriptions' migration ~p~n", [Any])
144144
end.
@@ -148,14 +148,14 @@ migrate_pubsub_subscriptions(_Config) ->
148148
%%% ==================================================================
149149

150150
migrate_pubsub_affiliations(_Config) ->
151-
Nidx = create_migration_node(),
151+
_ = create_migration_node(),
152152
{ok, _} = ?RPC_MIGRATE(<<"pubsub_affiliations">>),
153-
SqlData = #{table => <<"pubsub_affiliations">>, where => <<"nidx=", (list_to_binary(integer_to_list(Nidx)))/binary>>},
154-
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
155-
{selected, [{Nidx}]} ->
153+
SqlData = #{table => <<"pubsub_affiliations">>, where => <<"luser='", ?USERNAME/binary, "'">>},
154+
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"luser">>}) of
155+
{selected, [{?USERNAME}]} ->
156156
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
157157
_ = clear_tables(),
158-
ct:comment("Migration of 'pubsub_affiliations' is successful for Nidx: ~p", [Nidx]);
158+
ct:comment("Migration of 'pubsub_affiliations' is successful for 'pubsub_affiliations': ~p", [?USERNAME]);
159159
Any ->
160160
ct:fail("Unexpected result of 'pubsub_affiliations' migration ~p~n", [Any])
161161
end.
@@ -168,12 +168,12 @@ migrate_pubsub_items(_Config) ->
168168
Nidx = create_migration_node(),
169169
ok = mongoose_helper:successful_rpc(mod_pubsub_db_mnesia, transaction, [#{name => add_item, args => [Nidx, ?JID, ?BASE_ITEM(Nidx)]}]),
170170
{ok, _} = ?RPC_MIGRATE(<<"pubsub_items">>),
171-
SqlData = #{table => <<"pubsub_items">>, where => <<"nidx=", (list_to_binary(integer_to_list(Nidx)))/binary>>},
172-
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
173-
{selected, [{Nidx}]} ->
171+
SqlData = #{table => <<"pubsub_items">>, where => <<"created_luser='", ?USERNAME/binary, "'">>},
172+
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"created_luser">>}) of
173+
{selected, [{?USERNAME}]} ->
174174
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
175175
_ = clear_tables(),
176-
ct:comment("Migration of 'pubsub_items' is successful for Nidx: ~p", [Nidx]);
176+
ct:comment("Migration of 'pubsub_items' is successful for 'created_luser': ~p", [?USERNAME]);
177177
Any ->
178178
ct:fail("Unexpected result of 'pubsub_items' migration ~p~n", [Any])
179179
end.
@@ -216,7 +216,7 @@ migrate_vcard_search(_Config) ->
216216
%%% Test case for migrate vcard
217217
%%% ==================================================================
218218

219-
migrate_vcard(Config) ->
219+
migrate_vcard(_Config) ->
220220
ok = set_vcard(),
221221
{ok, _} = ?RPC_MIGRATE(<<"vcard">>),
222222
SqlData = #{table => <<"vcard">>, where => <<"username='", ?USERNAME/binary, "'">>},
@@ -233,7 +233,7 @@ migrate_vcard(Config) ->
233233
%%% Test case for migrate event_pusher_push_subscription
234234
%%% ==================================================================
235235

236-
migrate_event_pusher_push_subscription(Config) ->
236+
migrate_event_pusher_push_subscription(_Config) ->
237237
PubsubNode = <<"migration_pub_sub_node">>,
238238
ok = mongoose_helper:successful_rpc(mod_event_pusher_push_mnesia, enable, [?JID, ?JID, PubsubNode, [{<<"name">>, <<"value">>}]]),
239239
{ok, _} = ?RPC_MIGRATE(<<"event_pusher_push_subscription">>),
@@ -251,63 +251,63 @@ migrate_event_pusher_push_subscription(Config) ->
251251
%%% Test case for migrate rosterusers @TODO
252252
%%% ==================================================================
253253

254-
migrate_rosterusers(Config) ->
254+
migrate_rosterusers(_Config) ->
255255
?RPC_MIGRATE(<<"rosterusers">>),
256256
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
257257

258258
%%% ==================================================================
259259
%%% Test case for migrate roster_version @TODO
260260
%%% ==================================================================
261261

262-
migrate_roster_version(Config) ->
262+
migrate_roster_version(_Config) ->
263263
?RPC_MIGRATE(<<"roster_version">>),
264264
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
265265

266266
%%% ==================================================================
267267
%%% Test case for migrate rostergroups @TODO
268268
%%% ==================================================================
269269

270-
migrate_rostergroups(Config) ->
270+
migrate_rostergroups(_Config) ->
271271
?RPC_MIGRATE(<<"rostergroups">>),
272272
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
273273

274274
%%% ==================================================================
275275
%%% Test case for migrate last @TODO
276276
%%% ==================================================================
277277

278-
migrate_last(Config) ->
278+
migrate_last(_Config) ->
279279
?RPC_MIGRATE(<<"last">>),
280280
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
281281

282282
%%% ==================================================================
283283
%%% Test case for migrate private_storage @TODO
284284
%%% ==================================================================
285285

286-
migrate_private_storage(Config) ->
286+
migrate_private_storage(_Config) ->
287287
?RPC_MIGRATE(<<"private_storage">>),
288288
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
289289

290290
%%% ==================================================================
291291
%%% Test case for migrate offline_message @TODO
292292
%%% ==================================================================
293293

294-
migrate_offline_message(Config) ->
294+
migrate_offline_message(_Config) ->
295295
?RPC_MIGRATE(<<"offline_message">>),
296296
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
297297

298298
%%% ==================================================================
299299
%%% Test case for migrate muc_light_rooms @TODO
300300
%%% ==================================================================
301301

302-
migrate_muc_light_rooms(Config) ->
302+
migrate_muc_light_rooms(_Config) ->
303303
?RPC_MIGRATE(<<"muc_light_rooms">>),
304304
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
305305

306306
%%% ==================================================================
307307
%%% Test case for migrate all @TODO
308308
%%% ==================================================================
309309

310-
migrate_all(Config) ->
310+
migrate_all(_Config) ->
311311
?RPC_MIGRATE(<<"all">>),
312312
ct:comment("TEST CASE ~p", [?FUNCTION_NAME]).
313313

@@ -331,7 +331,7 @@ slow_rpc(M, F, A) ->
331331

332332
sql_to_rdbms(#{act := Act, column := Column, table := Table, where := Where}) ->
333333
SelectQuery = <<Act/binary, " ", Column/binary, " FROM ", Table/binary, " WHERE ", Where/binary>>,
334-
SelectResult = sql_query(SelectQuery).
334+
sql_query(SelectQuery).
335335

336336
clear_tables() ->
337337
Tables = [pubsub_state, pubsub_item, pubsub_node,
@@ -346,4 +346,4 @@ create_migration_node() ->
346346
Nidx.
347347

348348
set_vcard() ->
349-
mongoose_helper:successful_rpc(mod_vcard_mnesia, set_vcard, [?USERNAME, <<"localhost">>, ?BASE_VCARD, ?BASE_SEARCH_VCARD]).
349+
mongoose_helper:successful_rpc(mod_vcard_mnesia, set_vcard, [?USERNAME, <<"localhost">>, ?BASE_VCARD, ?BASE_SEARCH_VCARD]).

src/admin_extra/service_admin_extra_migration.erl

+13-8
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ migrate_pubsub_nodes([H|_]) ->
184184
{Host, ID} = H#pubsub_node.nodeid,
185185
Owners = jiffy:encode([jid:to_binary(O) || O <- H#pubsub_node.owners]),
186186
Ops = jiffy:encode({H#pubsub_node.options}),
187-
Cols = ["nidx", "p_key", "name", "type", "owners", "options"],
188-
Vals = [H#pubsub_node.id, Host, ID, H#pubsub_node.type, Owners, Ops],
187+
Cols = ["p_key", "name", "type", "owners", "options"], % "nidx"
188+
Vals = [Host, ID, H#pubsub_node.type, Owners, Ops], % H#pubsub_node.id
189189
Q = ["INSERT INTO pubsub_nodes ", expand_sql_vals(Cols, Vals), ";"],
190190
case mongoose_rdbms:sql_query(?MYNAME, Q) of
191191
{error, Reason} ->
@@ -271,15 +271,15 @@ migrate_pubsub_items([]) ->
271271
migrate_pubsub_items([H|_]) ->
272272
{IID, NodeIdx} = H#pubsub_item.itemid,
273273
{CTime, CJID} = H#pubsub_item.creation,
274-
[Payload] = H#pubsub_item.payload,
274+
Payload = H#pubsub_item.payload,
275275
CT = to_unixtime(CTime),
276276
JidBIn = jid:to_binary(CJID),
277277
JID = jid:from_binary(JidBIn),
278-
XMLB = <<"<item>", (exml:to_binary(Payload))/binary, "</item>">>,
278+
XMLB = exml:to_binary(#xmlel{name = <<"item">>, children = Payload}),
279279
Cols = ["nidx", "itemid", "created_luser", "created_lserver", "created_at",
280280
"modified_luser", "modified_lserver", "modified_lresource", "modified_at", "payload"],
281281
Vals = [NodeIdx, IID, JID#jid.luser, JID#jid.lserver, CT, JID#jid.lserver,
282-
JID#jid.lserver, JID#jid.lresource, CT, XMLB],
282+
JID#jid.lserver, JID#jid.lresource, CT, {XMLB, escape_binary}],
283283
Q = ["INSERT INTO pubsub_items ", expand_sql_vals(Cols, Vals), ";"],
284284
case mongoose_rdbms:sql_query(?MYNAME, Q) of
285285
{error, Reason} ->
@@ -558,10 +558,12 @@ migrate_muc_light_rooms([H|_]) ->
558558
{selected, [{null}]} ->
559559
1;
560560
{selected, [{N}]} when is_integer(N) ->
561-
N + 1
561+
N + 1;
562+
{selected, [{N}]} when is_binary(N) ->
563+
list_to_integer(binary_to_list(N)) + 1
562564
end,
563-
Cols = ["id", "luser", "lserver", "version"],
564-
Vals = [ID, LUser, LServer, mod_muc_light_db_mnesia:get_room_version(H)],
565+
Cols = ["luser", "lserver", "version"], % ---"id"
566+
Vals = [LUser, LServer, mod_muc_light_db_mnesia:get_room_version(H)], % --- ID
565567
Q = ["INSERT INTO muc_light_rooms ", expand_sql_vals(Cols, Vals), ";"],
566568
case mongoose_rdbms:sql_query(?MYNAME, Q) of
567569
{error, Reason} ->
@@ -689,6 +691,9 @@ join([H|T], Sep) ->
689691
%% -------------------------------------------------------------------
690692
-spec escape(Data :: any()) -> Res :: binary() | list() | any().
691693

694+
escape({Data, Escape}) ->
695+
mongoose_rdbms_odbc:Escape(Data);
696+
692697
escape(Data) when is_integer(Data) ->
693698
integer_to_binary(Data);
694699

0 commit comments

Comments
 (0)