Skip to content

Commit

Permalink
Mnesia migration: Fix logic of CT and CTL for correct MSSQL migration
Browse files Browse the repository at this point in the history
  • Loading branch information
vkatsuba committed Dec 25, 2020
1 parent edaddc0 commit 2b675d3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
56 changes: 28 additions & 28 deletions big_tests/tests/mnesia_migration_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ end_per_testcase(CaseName, Config) ->
%%% ==================================================================

migrate_pubsub_nodes(_Config) ->
Nidx = create_migration_node(),
_ = create_migration_node(),
{ok, _} = ?RPC_MIGRATE(<<"pubsub_nodes">>),
SqlData = #{table => <<"pubsub_nodes">>, where => <<"name='", ?NODE_NAME/binary, "'">>},
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
{selected, [{Nidx}]} ->
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"name">>}) of
{selected, [{?NODE_NAME}]} ->
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
_ = clear_tables(),
ct:comment("Migration of 'pubsub_nodes' is successful for Nidx: ~p", [Nidx]);
ct:comment("Migration of 'pubsub_nodes' is successful for 'name': ~p", [?NODE_NAME]);
Any ->
ct:fail("Unexpected result of 'pubsub_nodes' migration ~p~n", [Any])
end.
Expand All @@ -133,12 +133,12 @@ migrate_pubsub_subscriptions(_Config) ->
Nidx = create_migration_node(),
ok = mongoose_helper:successful_rpc(mod_pubsub_db_mnesia, transaction, [#{name => add_subscription, args => [Nidx, ?JID, 'subscribed', <<"0000-0000-0000000">>, []]}]),
{ok, _} = ?RPC_MIGRATE(<<"pubsub_subscriptions">>),
SqlData = #{table => <<"pubsub_subscriptions">>, where => <<"nidx=", (list_to_binary(integer_to_list(Nidx)))/binary>>},
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
{selected, [{Nidx}]} ->
SqlData = #{table => <<"pubsub_subscriptions">>, where => <<"luser='", ?USERNAME/binary, "'">>},
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"luser">>}) of
{selected, [{?USERNAME}]} ->
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
_ = clear_tables(),
ct:comment("Migration of 'pubsub_subscriptions' is successful for Nidx: ~p", [Nidx]);
ct:comment("Migration of 'pubsub_subscriptions' is successful for 'luser': ~p", [?USERNAME]);
Any ->
ct:fail("Unexpected result of 'pubsub_subscriptions' migration ~p~n", [Any])
end.
Expand All @@ -148,14 +148,14 @@ migrate_pubsub_subscriptions(_Config) ->
%%% ==================================================================

migrate_pubsub_affiliations(_Config) ->
Nidx = create_migration_node(),
_ = create_migration_node(),
{ok, _} = ?RPC_MIGRATE(<<"pubsub_affiliations">>),
SqlData = #{table => <<"pubsub_affiliations">>, where => <<"nidx=", (list_to_binary(integer_to_list(Nidx)))/binary>>},
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
{selected, [{Nidx}]} ->
SqlData = #{table => <<"pubsub_affiliations">>, where => <<"luser='", ?USERNAME/binary, "'">>},
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"luser">>}) of
{selected, [{?USERNAME}]} ->
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
_ = clear_tables(),
ct:comment("Migration of 'pubsub_affiliations' is successful for Nidx: ~p", [Nidx]);
ct:comment("Migration of 'pubsub_affiliations' is successful for 'pubsub_affiliations': ~p", [?USERNAME]);
Any ->
ct:fail("Unexpected result of 'pubsub_affiliations' migration ~p~n", [Any])
end.
Expand All @@ -168,12 +168,12 @@ migrate_pubsub_items(_Config) ->
Nidx = create_migration_node(),
ok = mongoose_helper:successful_rpc(mod_pubsub_db_mnesia, transaction, [#{name => add_item, args => [Nidx, ?JID, ?BASE_ITEM(Nidx)]}]),
{ok, _} = ?RPC_MIGRATE(<<"pubsub_items">>),
SqlData = #{table => <<"pubsub_items">>, where => <<"nidx=", (list_to_binary(integer_to_list(Nidx)))/binary>>},
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"nidx">>}) of
{selected, [{Nidx}]} ->
SqlData = #{table => <<"pubsub_items">>, where => <<"created_luser='", ?USERNAME/binary, "'">>},
case sql_to_rdbms(SqlData#{act => <<"SELECT">>, column => <<"created_luser">>}) of
{selected, [{?USERNAME}]} ->
{updated, 1} = sql_to_rdbms(SqlData#{act => <<"DELETE">>, column => <<"">>}),
_ = clear_tables(),
ct:comment("Migration of 'pubsub_items' is successful for Nidx: ~p", [Nidx]);
ct:comment("Migration of 'pubsub_items' is successful for 'created_luser': ~p", [?USERNAME]);
Any ->
ct:fail("Unexpected result of 'pubsub_items' migration ~p~n", [Any])
end.
Expand Down Expand Up @@ -216,7 +216,7 @@ migrate_vcard_search(_Config) ->
%%% Test case for migrate vcard
%%% ==================================================================

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

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

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

%%% ==================================================================
%%% Test case for migrate roster_version @TODO
%%% ==================================================================

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

%%% ==================================================================
%%% Test case for migrate rostergroups @TODO
%%% ==================================================================

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

%%% ==================================================================
%%% Test case for migrate last @TODO
%%% ==================================================================

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

%%% ==================================================================
%%% Test case for migrate private_storage @TODO
%%% ==================================================================

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

%%% ==================================================================
%%% Test case for migrate offline_message @TODO
%%% ==================================================================

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

%%% ==================================================================
%%% Test case for migrate muc_light_rooms @TODO
%%% ==================================================================

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

%%% ==================================================================
%%% Test case for migrate all @TODO
%%% ==================================================================

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

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

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

clear_tables() ->
Tables = [pubsub_state, pubsub_item, pubsub_node,
Expand Down
21 changes: 13 additions & 8 deletions src/admin_extra/service_admin_extra_migration.erl
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ migrate_pubsub_nodes([H|_]) ->
{Host, ID} = H#pubsub_node.nodeid,
Owners = jiffy:encode([jid:to_binary(O) || O <- H#pubsub_node.owners]),
Ops = jiffy:encode({H#pubsub_node.options}),
Cols = ["nidx", "p_key", "name", "type", "owners", "options"],
Vals = [H#pubsub_node.id, Host, ID, H#pubsub_node.type, Owners, Ops],
Cols = ["p_key", "name", "type", "owners", "options"], % "nidx"
Vals = [Host, ID, H#pubsub_node.type, Owners, Ops], % H#pubsub_node.id
Q = ["INSERT INTO pubsub_nodes ", expand_sql_vals(Cols, Vals), ";"],
case mongoose_rdbms:sql_query(?MYNAME, Q) of
{error, Reason} ->
Expand Down Expand Up @@ -271,15 +271,15 @@ migrate_pubsub_items([]) ->
migrate_pubsub_items([H|_]) ->
{IID, NodeIdx} = H#pubsub_item.itemid,
{CTime, CJID} = H#pubsub_item.creation,
[Payload] = H#pubsub_item.payload,
Payload = H#pubsub_item.payload,
CT = to_unixtime(CTime),
JidBIn = jid:to_binary(CJID),
JID = jid:from_binary(JidBIn),
XMLB = <<"<item>", (exml:to_binary(Payload))/binary, "</item>">>,
XMLB = exml:to_binary(#xmlel{name = <<"item">>, children = Payload}),
Cols = ["nidx", "itemid", "created_luser", "created_lserver", "created_at",
"modified_luser", "modified_lserver", "modified_lresource", "modified_at", "payload"],
Vals = [NodeIdx, IID, JID#jid.luser, JID#jid.lserver, CT, JID#jid.lserver,
JID#jid.lserver, JID#jid.lresource, CT, XMLB],
JID#jid.lserver, JID#jid.lresource, CT, {XMLB, escape_binary}],
Q = ["INSERT INTO pubsub_items ", expand_sql_vals(Cols, Vals), ";"],
case mongoose_rdbms:sql_query(?MYNAME, Q) of
{error, Reason} ->
Expand Down Expand Up @@ -558,10 +558,12 @@ migrate_muc_light_rooms([H|_]) ->
{selected, [{null}]} ->
1;
{selected, [{N}]} when is_integer(N) ->
N + 1
N + 1;
{selected, [{N}]} when is_binary(N) ->
list_to_integer(binary_to_list(N)) + 1
end,
Cols = ["id", "luser", "lserver", "version"],
Vals = [ID, LUser, LServer, mod_muc_light_db_mnesia:get_room_version(H)],
Cols = ["luser", "lserver", "version"], % ---"id"
Vals = [LUser, LServer, mod_muc_light_db_mnesia:get_room_version(H)], % --- ID
Q = ["INSERT INTO muc_light_rooms ", expand_sql_vals(Cols, Vals), ";"],
case mongoose_rdbms:sql_query(?MYNAME, Q) of
{error, Reason} ->
Expand Down Expand Up @@ -689,6 +691,9 @@ join([H|T], Sep) ->
%% -------------------------------------------------------------------
-spec escape(Data :: any()) -> Res :: binary() | list() | any().

escape({Data, Escape}) ->
mongoose_rdbms_odbc:Escape(Data);

escape(Data) when is_integer(Data) ->
integer_to_binary(Data);

Expand Down

0 comments on commit 2b675d3

Please sign in to comment.