1: -module(mod_offline_mnesia_SUITE).
    2: -include_lib("eunit/include/eunit.hrl").
    3: -include_lib("common_test/include/ct.hrl").
    4: 
    5: -compile([export_all, nowarn_export_all]).
    6: 
    7: -define(OFFLINE_MSG_3_5_FIELDS, [us, timestamp, expire, from, to, packet]).
    8: 
    9: all() ->
   10:     [mnesia_offline_table_can_be_upgraded_from_3_5_to_next].
   11: 
   12: init_per_suite(C) ->
   13:     application:ensure_all_started(jid),
   14:     ok = mnesia:create_schema([node()]),
   15:     ok = mnesia:start(),
   16:     C.
   17: 
   18: end_per_suite(_C) ->
   19:     mnesia:stop(),
   20:     mnesia:delete_schema([node()]).
   21: 
   22: mnesia_offline_table_can_be_upgraded_from_3_5_to_next(_C) ->
   23:     %% GIVEN offline_msg table existing with fields as in MongooseIM 3.5.0
   24:     {atomic, ok} = mnesia:create_table(offline_msg,
   25:                                        [{disc_only_copies, [node()]},
   26:                                         {type, bag},
   27:                                         {attributes, ?OFFLINE_MSG_3_5_FIELDS}]),
   28:     Key = {<<"user">>, <<"localhost">>},
   29:     Record = {offline_msg, Key, os:timestamp(), undefined,
   30:               <<"user@localhost">>, <<"user2@localhost">>, <<"packet">>},
   31:     %% AND there is a record in the table
   32:     mnesia:dirty_write(Record),
   33:     [Before] = mnesia:dirty_read(offline_msg, Key),
   34:     %% WHEN new mod_offline_mnesia starts
   35:     ok = mod_offline_mnesia:init(<<"localhost">>, []),
   36:     %% THEN the field is added to table schema
   37:     ?assert(lists:member(permanent_fields, mnesia:table_info(offline_msg, attributes))),
   38:     %% AND empty the value of the new field is an empty list
   39:     [After] = mnesia:dirty_read(offline_msg, Key),
   40:     ?assertEqual(erlang:append_element(Before, []), After).
   41: 
   42: