• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

processone / ejabberd / 511

pending completion
511

push

github

Badlop
Set version to 23.04

13516 of 40931 relevant lines covered (33.02%)

672.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

1.69
/src/mod_admin_update_sql.erl
1
%%%-------------------------------------------------------------------
2
%%% File    : mod_admin_update_sql.erl
3
%%% Author  : Alexey Shchepin <alexey@process-one.net>
4
%%% Purpose : Convert SQL DB to the new format
5
%%% Created :  9 Aug 2017 by Alexey Shchepin <alexey@process-one.net>
6
%%%
7
%%%
8
%%% ejabberd, Copyright (C) 2002-2023   ProcessOne
9
%%%
10
%%% This program is free software; you can redistribute it and/or
11
%%% modify it under the terms of the GNU General Public License as
12
%%% published by the Free Software Foundation; either version 2 of the
13
%%% License, or (at your option) any later version.
14
%%%
15
%%% This program is distributed in the hope that it will be useful,
16
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
17
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
%%% General Public License for more details.
19
%%%
20
%%% You should have received a copy of the GNU General Public License along
21
%%% with this program; if not, write to the Free Software Foundation, Inc.,
22
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
%%%
24
%%%-------------------------------------------------------------------
25

26
-module(mod_admin_update_sql).
27
-author('alexey@process-one.net').
28

29
-behaviour(gen_mod).
30

31
-export([start/2, stop/1, reload/3, mod_options/1,
32
         get_commands_spec/0, depends/2, mod_doc/0]).
33

34
% Commands API
35
-export([update_sql/0]).
36

37
% For testing
38
-export([update_sql/1]).
39

40
-include("logger.hrl").
41
-include("ejabberd_commands.hrl").
42
-include_lib("xmpp/include/xmpp.hrl").
43
-include("ejabberd_sql_pt.hrl").
44
-include("translate.hrl").
45

46
%%%
47
%%% gen_mod
48
%%%
49

50
start(_Host, _Opts) ->
51
    ejabberd_commands:register_commands(?MODULE, get_commands_spec()).
3✔
52

53
stop(_Host) ->
54
    ejabberd_commands:unregister_commands(get_commands_spec()).
3✔
55

56
reload(_Host, _NewOpts, _OldOpts) ->
57
    ok.
×
58

59
depends(_Host, _Opts) ->
60
    [].
3✔
61

62
%%%
63
%%% Register commands
64
%%%
65

66
get_commands_spec() ->
67
    [#ejabberd_commands{name = update_sql, tags = [sql],
6✔
68
                        desc = "Convert MS SQL, MySQL or PostgreSQL DB to the new format",
69
                        note = "improved in 23.04",
70
                        module = ?MODULE, function = update_sql,
71
                        args = [],
72
                        args_example = [],
73
                        args_desc = [],
74
                        result = {res, rescode},
75
                        result_example = ok,
76
                        result_desc = "Status code: 0 on success, 1 otherwise"}
77
    ].
78

79
update_sql() ->
80
    lists:foreach(
×
81
      fun(Host) ->
82
              case ejabberd_sql_sup:is_started(Host) of
×
83
                  false ->
84
                      ok;
×
85
                  true ->
86
                      update_sql(Host)
×
87
              end
88
      end, ejabberd_option:hosts()).
89

90
-record(state, {host :: binary(),
91
                dbtype :: mysql | pgsql | sqlite | mssql | odbc,
92
                escape}).
93

94
update_sql(Host) ->
95
    LHost = jid:nameprep(Host),
×
96
    DBType = ejabberd_option:sql_type(LHost),
×
97
    IsSupported =
×
98
        case DBType of
99
            mssql -> true;
×
100
            mysql -> true;
×
101
            pgsql -> true;
×
102
            _ -> false
×
103
        end,
104
    if
×
105
        not IsSupported ->
106
            io:format("Converting ~p DB is not supported~n", [DBType]),
×
107
            error;
×
108
        true ->
109
            Escape =
×
110
                case DBType of
111
                    mssql -> fun ejabberd_sql:standard_escape/1;
×
112
                    sqlite -> fun ejabberd_sql:standard_escape/1;
×
113
                    _ -> fun ejabberd_sql:escape/1
×
114
                end,
115
            State = #state{host = LHost,
×
116
                           dbtype = DBType,
117
                           escape = Escape},
118
            update_tables(State),
×
119
            check_config()
×
120
    end.
121

122
check_config() ->
123
    case ejabberd_sql:use_new_schema() of
×
124
        true -> ok;
×
125
        false ->
126
            ejabberd_config:set_option(new_sql_schema, true),
×
127
            io:format('~nNOTE: you must add "new_sql_schema: true" to ejabberd.yml before next restart~n~n', [])
×
128
    end.
129

130
update_tables(State) ->
131
    case add_sh_column(State, "users") of
×
132
        true ->
133
            drop_pkey(State, "users"),
×
134
            add_pkey(State, "users", ["server_host", "username"]),
×
135
            drop_sh_default(State, "users");
×
136
        false ->
137
            ok
×
138
    end,
139

140
    case add_sh_column(State, "last") of
×
141
        true ->
142
            drop_pkey(State, "last"),
×
143
            add_pkey(State, "last", ["server_host", "username"]),
×
144
            drop_sh_default(State, "last");
×
145
        false ->
146
            ok
×
147
    end,
148

149
    case add_sh_column(State, "rosterusers") of
×
150
        true ->
151
            drop_index(State, "rosterusers", "i_rosteru_user_jid"),
×
152
            drop_index(State, "rosterusers", "i_rosteru_username"),
×
153
            drop_index(State, "rosterusers", "i_rosteru_jid"),
×
154
            create_unique_index(State, "rosterusers", "i_rosteru_sh_user_jid", ["server_host", "username", "jid"]),
×
155
            create_index(State, "rosterusers", "i_rosteru_sh_jid", ["server_host", "jid"]),
×
156
            drop_sh_default(State, "rosterusers");
×
157
        false ->
158
            ok
×
159
    end,
160

161
    case add_sh_column(State, "rostergroups") of
×
162
        true ->
163
            drop_index(State, "rostergroups", "pk_rosterg_user_jid"),
×
164
            create_index(State, "rostergroups", "i_rosterg_sh_user_jid", ["server_host", "username", "jid"]),
×
165
            drop_sh_default(State, "rostergroups");
×
166
        false ->
167
            ok
×
168
    end,
169

170
    case add_sh_column(State, "sr_group") of
×
171
        true ->
172
            drop_index(State, "sr_group", "i_sr_group_name"),
×
173
            create_unique_index(State, "sr_group", "i_sr_group_sh_name", ["server_host", "name"]),
×
174
            drop_sh_default(State, "sr_group");
×
175
        false ->
176
            ok
×
177
    end,
178

179
    case add_sh_column(State, "sr_user") of
×
180
        true ->
181
            drop_index(State, "sr_user", "i_sr_user_jid_grp"),
×
182
            drop_index(State, "sr_user", "i_sr_user_jid"),
×
183
            drop_index(State, "sr_user", "i_sr_user_grp"),
×
184
            create_unique_index(State, "sr_user", "i_sr_user_sh_jid_grp", ["server_host", "jid", "grp"]),
×
185
            create_index(State, "sr_user", "i_sr_user_sh_grp", ["server_host", "grp"]),
×
186
            drop_sh_default(State, "sr_user");
×
187
        false ->
188
            ok
×
189
    end,
190

191
    case add_sh_column(State, "spool") of
×
192
        true ->
193
            drop_index(State, "spool", "i_despool"),
×
194
            create_index(State, "spool", "i_spool_sh_username", ["server_host", "username"]),
×
195
            drop_sh_default(State, "spool");
×
196
        false ->
197
            ok
×
198
    end,
199

200
    case add_sh_column(State, "archive") of
×
201
        true ->
202
            drop_index(State, "archive", "i_username"),
×
203
            drop_index(State, "archive", "i_username_timestamp"),
×
204
            drop_index(State, "archive", "i_timestamp"),
×
205
            drop_index(State, "archive", "i_peer"),
×
206
            drop_index(State, "archive", "i_bare_peer"),
×
207
            drop_index(State, "archive", "i_username_peer"),
×
208
            drop_index(State, "archive", "i_username_bare_peer"),
×
209
            create_index(State, "archive", "i_archive_sh_username_timestamp", ["server_host", "username", "timestamp"]),
×
210
            create_index(State, "archive", "i_archive_sh_timestamp", ["server_host", "timestamp"]),
×
211
            create_index(State, "archive", "i_archive_sh_username_peer", ["server_host", "username", "peer"]),
×
212
            create_index(State, "archive", "i_archive_sh_username_bare_peer", ["server_host", "username", "bare_peer"]),
×
213
            drop_sh_default(State, "archive");
×
214
        false ->
215
            ok
×
216
    end,
217

218
    case add_sh_column(State, "archive_prefs") of
×
219
        true ->
220
            drop_pkey(State, "archive_prefs"),
×
221
            add_pkey(State, "archive_prefs", ["server_host", "username"]),
×
222
            drop_sh_default(State, "archive_prefs");
×
223
        false ->
224
            ok
×
225
    end,
226

227
    case add_sh_column(State, "vcard") of
×
228
        true ->
229
            drop_pkey(State, "vcard"),
×
230
            add_pkey(State, "vcard", ["server_host", "username"]),
×
231
            drop_sh_default(State, "vcard");
×
232
        false ->
233
            ok
×
234
    end,
235

236
    case add_sh_column(State, "vcard_search") of
×
237
        true ->
238
            drop_pkey(State, "vcard_search"),
×
239
            drop_index(State, "vcard_search", "i_vcard_search_lfn"),
×
240
            drop_index(State, "vcard_search", "i_vcard_search_lfamily"),
×
241
            drop_index(State, "vcard_search", "i_vcard_search_lgiven"),
×
242
            drop_index(State, "vcard_search", "i_vcard_search_lmiddle"),
×
243
            drop_index(State, "vcard_search", "i_vcard_search_lnickname"),
×
244
            drop_index(State, "vcard_search", "i_vcard_search_lbday"),
×
245
            drop_index(State, "vcard_search", "i_vcard_search_lctry"),
×
246
            drop_index(State, "vcard_search", "i_vcard_search_llocality"),
×
247
            drop_index(State, "vcard_search", "i_vcard_search_lemail"),
×
248
            drop_index(State, "vcard_search", "i_vcard_search_lorgname"),
×
249
            drop_index(State, "vcard_search", "i_vcard_search_lorgunit"),
×
250
            add_pkey(State, "vcard_search", ["server_host", "lusername"]),
×
251
            create_index(State, "vcard_search", "i_vcard_search_sh_lfn",       ["server_host", "lfn"]),
×
252
            create_index(State, "vcard_search", "i_vcard_search_sh_lfamily",   ["server_host", "lfamily"]),
×
253
            create_index(State, "vcard_search", "i_vcard_search_sh_lgiven",    ["server_host", "lgiven"]),
×
254
            create_index(State, "vcard_search", "i_vcard_search_sh_lmiddle",   ["server_host", "lmiddle"]),
×
255
            create_index(State, "vcard_search", "i_vcard_search_sh_lnickname", ["server_host", "lnickname"]),
×
256
            create_index(State, "vcard_search", "i_vcard_search_sh_lbday",     ["server_host", "lbday"]),
×
257
            create_index(State, "vcard_search", "i_vcard_search_sh_lctry",     ["server_host", "lctry"]),
×
258
            create_index(State, "vcard_search", "i_vcard_search_sh_llocality", ["server_host", "llocality"]),
×
259
            create_index(State, "vcard_search", "i_vcard_search_sh_lemail",    ["server_host", "lemail"]),
×
260
            create_index(State, "vcard_search", "i_vcard_search_sh_lorgname",  ["server_host", "lorgname"]),
×
261
            create_index(State, "vcard_search", "i_vcard_search_sh_lorgunit",  ["server_host", "lorgunit"]),
×
262
            drop_sh_default(State, "vcard_search");
×
263
        false ->
264
            ok
×
265
    end,
266

267
    case add_sh_column(State, "privacy_default_list") of
×
268
        true ->
269
            drop_pkey(State, "privacy_default_list"),
×
270
            add_pkey(State, "privacy_default_list", ["server_host", "username"]),
×
271
            drop_sh_default(State, "privacy_default_list");
×
272
        false ->
273
            ok
×
274
    end,
275

276
    case add_sh_column(State, "privacy_list") of
×
277
        true ->
278
            drop_index(State, "privacy_list", "i_privacy_list_username"),
×
279
            drop_index(State, "privacy_list", "i_privacy_list_username_name"),
×
280
            create_unique_index(State, "privacy_list", "i_privacy_list_sh_username_name", ["server_host", "username", "name"]),
×
281
            drop_sh_default(State, "privacy_list");
×
282
        false ->
283
            ok
×
284
    end,
285

286
    case add_sh_column(State, "private_storage") of
×
287
        true ->
288
            drop_index(State, "private_storage", "i_private_storage_username"),
×
289
            drop_index(State, "private_storage", "i_private_storage_username_namespace"),
×
290
            add_pkey(State, "private_storage", ["server_host", "username", "namespace"]),
×
291
            drop_sh_default(State, "private_storage");
×
292
        false ->
293
            ok
×
294
    end,
295

296
    case add_sh_column(State, "roster_version") of
×
297
        true ->
298
            drop_pkey(State, "roster_version"),
×
299
            add_pkey(State, "roster_version", ["server_host", "username"]),
×
300
            drop_sh_default(State, "roster_version");
×
301
        false ->
302
            ok
×
303
    end,
304

305
    case add_sh_column(State, "muc_room") of
×
306
        true ->
307
            drop_sh_default(State, "muc_room");
×
308
        false ->
309
            ok
×
310
    end,
311

312
    case add_sh_column(State, "muc_registered") of
×
313
        true ->
314
            drop_sh_default(State, "muc_registered");
×
315
        false ->
316
            ok
×
317
    end,
318

319
    case add_sh_column(State, "muc_online_room") of
×
320
        true ->
321
            drop_sh_default(State, "muc_online_room");
×
322
        false ->
323
            ok
×
324
    end,
325

326
    case add_sh_column(State, "muc_online_users") of
×
327
        true ->
328
            drop_sh_default(State, "muc_online_users");
×
329
        false ->
330
            ok
×
331
    end,
332

333
    case add_sh_column(State, "motd") of
×
334
        true ->
335
            drop_pkey(State, "motd"),
×
336
            add_pkey(State, "motd", ["server_host", "username"]),
×
337
            drop_sh_default(State, "motd");
×
338
        false ->
339
            ok
×
340
    end,
341

342
    case add_sh_column(State, "sm") of
×
343
        true ->
344
            drop_index(State, "sm", "i_sm_sid"),
×
345
            drop_index(State, "sm", "i_sm_username"),
×
346
            add_pkey(State, "sm", ["usec", "pid"]),
×
347
            create_index(State, "sm", "i_sm_sh_username", ["server_host", "username"]),
×
348
            drop_sh_default(State, "sm");
×
349
        false ->
350
            ok
×
351
    end,
352

353
    case add_sh_column(State, "push_session") of
×
354
        true ->
355
            drop_index(State, "push_session", "i_push_usn"),
×
356
            drop_index(State, "push_session", "i_push_ut"),
×
357
            create_unique_index(State, "push_session", "i_push_session_susn", ["server_host", "username", "service", "node"]),
×
358
            create_index(State, "push_session", "i_push_session_sh_username_timestamp", ["server_host", "username", "timestamp"]),
×
359
            drop_sh_default(State, "push_session");
×
360
        false ->
361
            ok
×
362
    end,
363

364
    case add_sh_column(State, "mix_pam") of
×
365
        true ->
366
            drop_index(State, "mix_pam", "i_mix_pam"),
×
367
            drop_index(State, "mix_pam", "i_mix_pam_u"),
×
368
            drop_index(State, "mix_pam", "i_mix_pam_us"),
×
369
            create_unique_index(State, "mix_pam", "i_mix_pam", ["username", "server_host", "channel", "service"]),
×
370
            drop_sh_default(State, "mix_pam");
×
371
        false ->
372
            ok
×
373
    end,
374

375
    case add_sh_column(State, "mqtt_pub") of
×
376
        true ->
377
            drop_index(State, "mqtt_pub", "i_mqtt_topic"),
×
378
            create_unique_index(State, "mqtt_pub", "i_mqtt_topic_server", ["topic", "server_host"]),
×
379
            drop_sh_default(State, "mqtt_pub");
×
380
        false ->
381
            ok
×
382
    end,
383

384
    ok.
×
385

386
check_sh_column(#state{dbtype = mysql} = State, Table) ->
387
    DB = ejabberd_option:sql_database(State#state.host),
×
388
    sql_query(
×
389
      State#state.host,
390
      ["SELECT 1 FROM information_schema.columns ",
391
       "WHERE table_name = '", Table, "' AND column_name = 'server_host' ",
392
       "AND table_schema = '", (State#state.escape)(DB), "' ",
393
       "GROUP BY table_name, column_name;"], false);
394
check_sh_column(State, Table) ->
395
    DB = ejabberd_option:sql_database(State#state.host),
×
396
    sql_query(
×
397
      State#state.host,
398
      ["SELECT 1 FROM information_schema.columns ",
399
       "WHERE table_name = '", Table, "' AND column_name = 'server_host' ",
400
       "AND table_catalog = '", (State#state.escape)(DB), "' ",
401
       "GROUP BY table_name, column_name;"], false).
402

403
add_sh_column(State, Table) ->
404
    case check_sh_column(State, Table) of
×
405
        true -> false;
×
406
        false ->
407
            do_add_sh_column(State, Table),
×
408
            true
×
409
    end.
410

411
do_add_sh_column(#state{dbtype = pgsql} = State, Table) ->
412
    sql_query(
×
413
      State#state.host,
414
      ["ALTER TABLE ", Table, " ADD COLUMN server_host text NOT NULL DEFAULT '",
415
       (State#state.escape)(State#state.host),
416
       "';"]);
417
do_add_sh_column(#state{dbtype = mssql} = State, Table) ->
418
    sql_query(
×
419
      State#state.host,
420
      ["ALTER TABLE [", Table, "] ADD [server_host] varchar (250) NOT NULL ",
421
       "CONSTRAINT [server_host_default] DEFAULT '",
422
       (State#state.escape)(State#state.host),
423
       "';"]);
424
do_add_sh_column(#state{dbtype = mysql} = State, Table) ->
425
    sql_query(
×
426
      State#state.host,
427
      ["ALTER TABLE ", Table, " ADD COLUMN server_host varchar(191) NOT NULL DEFAULT '",
428
       (State#state.escape)(State#state.host),
429
       "';"]).
430

431
drop_pkey(#state{dbtype = pgsql} = State, Table) ->
432
    sql_query(
×
433
      State#state.host,
434
      ["ALTER TABLE ", Table, " DROP CONSTRAINT ", Table, "_pkey;"]);
435
drop_pkey(#state{dbtype = mssql} = State, Table) ->
436
    sql_query(
×
437
      State#state.host,
438
      ["ALTER TABLE [", Table, "] DROP CONSTRAINT [", Table, "_PRIMARY];"]);
439
drop_pkey(#state{dbtype = mysql} = State, Table) ->
440
    sql_query(
×
441
      State#state.host,
442
      ["ALTER TABLE ", Table, " DROP PRIMARY KEY;"]).
443

444
add_pkey(#state{dbtype = pgsql} = State, Table, Cols) ->
445
    SCols = string:join(Cols, ", "),
×
446
    sql_query(
×
447
      State#state.host,
448
      ["ALTER TABLE ", Table, " ADD PRIMARY KEY (", SCols, ");"]);
449
add_pkey(#state{dbtype = mssql} = State, Table, Cols) ->
450
    SCols = string:join(Cols, "], ["),
×
451
    sql_query(
×
452
      State#state.host,
453
      ["ALTER TABLE [", Table, "] ADD CONSTRAINT [", Table, "_PRIMARY] PRIMARY KEY CLUSTERED ([", SCols, "]) ",
454
       "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ",
455
       "ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY];"]);
456
add_pkey(#state{dbtype = mysql} = State, Table, Cols) ->
457
    Cols2 = [C ++ mysql_keylen(Table, C) || C <- Cols],
×
458
    SCols = string:join(Cols2, ", "),
×
459
    sql_query(
×
460
      State#state.host,
461
      ["ALTER TABLE ", Table, " ADD PRIMARY KEY (", SCols, ");"]).
462

463
drop_sh_default(#state{dbtype = pgsql} = State, Table) ->
464
    sql_query(
×
465
      State#state.host,
466
      ["ALTER TABLE ", Table, " ALTER COLUMN server_host DROP DEFAULT;"]);
467
drop_sh_default(#state{dbtype = mssql} = State, Table) ->
468
    sql_query(
×
469
      State#state.host,
470
      ["ALTER TABLE [", Table, "] DROP CONSTRAINT [server_host_default];"]);
471
drop_sh_default(#state{dbtype = mysql} = State, Table) ->
472
    sql_query(
×
473
      State#state.host,
474
      ["ALTER TABLE ", Table, " ALTER COLUMN server_host DROP DEFAULT;"]).
475

476
check_index(#state{dbtype = pgsql} = State, Table, Index) ->
477
    sql_query(
×
478
      State#state.host,
479
      ["SELECT 1 FROM pg_indexes WHERE tablename = '", Table,
480
       "' AND indexname = '", Index, "';"], false);
481
check_index(#state{dbtype = mssql} = State, Table, Index) ->
482
    sql_query(
×
483
      State#state.host,
484
      ["SELECT 1 FROM sys.tables t ",
485
       "INNER JOIN sys.indexes i ON i.object_id = t.object_id ",
486
       "WHERE i.index_id > 0 ",
487
       "AND i.name = '", Index, "' ",
488
       "AND t.name = '", Table, "';"], false);
489
check_index(#state{dbtype = mysql} = State, Table, Index) ->
490
    DB = ejabberd_option:sql_database(State#state.host),
×
491
    sql_query(
×
492
      State#state.host,
493
      ["SELECT 1 FROM information_schema.statistics ",
494
       "WHERE table_name = '", Table, "' AND index_name = '", Index, "' ",
495
       "AND table_schema = '", (State#state.escape)(DB), "' ",
496
       "GROUP BY table_name, index_name;"], false).
497

498
drop_index(State, Table, Index) ->
499
    OldIndex = old_index_name(State#state.dbtype, Index),
×
500
    case check_index(State, Table, OldIndex) of
×
501
        true -> do_drop_index(State, Table, OldIndex);
×
502
        false -> ok
×
503
    end.
504

505
do_drop_index(#state{dbtype = pgsql} = State, _Table, Index) ->
506
    sql_query(
×
507
      State#state.host,
508
      ["DROP INDEX ", Index, ";"]);
509
do_drop_index(#state{dbtype = mssql} = State, Table, Index) ->
510
    sql_query(
×
511
      State#state.host,
512
      ["DROP INDEX [", Index, "] ON [", Table, "];"]);
513
do_drop_index(#state{dbtype = mysql} = State, Table, Index) ->
514
    sql_query(
×
515
      State#state.host,
516
      ["ALTER TABLE ", Table, " DROP INDEX ", Index, ";"]).
517

518
create_unique_index(#state{dbtype = pgsql} = State, Table, Index, Cols) ->
519
    SCols = string:join(Cols, ", "),
×
520
    sql_query(
×
521
      State#state.host,
522
      ["CREATE UNIQUE INDEX ", Index, " ON ", Table, " USING btree (",
523
       SCols, ");"]);
524
create_unique_index(#state{dbtype = mssql} = State, Table, "i_privacy_list_sh_username_name" = Index, Cols) ->
525
    create_index(State, Table, Index, Cols);
×
526
create_unique_index(#state{dbtype = mssql} = State, Table, Index, Cols) ->
527
    SCols = string:join(Cols, ", "),
×
528
    sql_query(
×
529
      State#state.host,
530
      ["CREATE UNIQUE ", mssql_clustered(Index), "INDEX [", new_index_name(State#state.dbtype, Index), "] ",
531
       "ON [", Table, "] (", SCols, ") ",
532
       "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON);"]);
533
create_unique_index(#state{dbtype = mysql} = State, Table, Index, Cols) ->
534
    Cols2 = [C ++ mysql_keylen(Index, C) || C <- Cols],
×
535
    SCols = string:join(Cols2, ", "),
×
536
    sql_query(
×
537
      State#state.host,
538
      ["CREATE UNIQUE INDEX ", Index, " ON ", Table, "(",
539
       SCols, ");"]).
540

541
create_index(#state{dbtype = pgsql} = State, Table, Index, Cols) ->
542
    NewIndex = new_index_name(State#state.dbtype, Index),
×
543
    SCols = string:join(Cols, ", "),
×
544
    sql_query(
×
545
      State#state.host,
546
      ["CREATE INDEX ", NewIndex, " ON ", Table, " USING btree (",
547
       SCols, ");"]);
548
create_index(#state{dbtype = mssql} = State, Table, Index, Cols) ->
549
    NewIndex = new_index_name(State#state.dbtype, Index),
×
550
    SCols = string:join(Cols, ", "),
×
551
    sql_query(
×
552
      State#state.host,
553
      ["CREATE INDEX [", NewIndex, "] ON [", Table, "] (", SCols, ") ",
554
       "WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON);"]);
555
create_index(#state{dbtype = mysql} = State, Table, Index, Cols) ->
556
    NewIndex = new_index_name(State#state.dbtype, Index),
×
557
    Cols2 = [C ++ mysql_keylen(NewIndex, C) || C <- Cols],
×
558
    SCols = string:join(Cols2, ", "),
×
559
    sql_query(
×
560
      State#state.host,
561
      ["CREATE INDEX ", NewIndex, " ON ", Table, "(",
562
       SCols, ");"]).
563

564
old_index_name(mssql, "i_bare_peer") -> "archive_bare_peer";
×
565
old_index_name(mssql, "i_peer") -> "archive_peer";
×
566
old_index_name(mssql, "i_timestamp") -> "archive_timestamp";
×
567
old_index_name(mssql, "i_username") -> "archive_username";
×
568
old_index_name(mssql, "i_username_bare_peer") -> "archive_username_bare_peer";
×
569
old_index_name(mssql, "i_username_peer") -> "archive_username_peer";
×
570
old_index_name(mssql, "i_username_timestamp") -> "archive_username_timestamp";
×
571
old_index_name(mssql, "i_push_usn") -> "i_push_usn";
×
572
old_index_name(mssql, "i_push_ut") -> "i_push_ut";
×
573
old_index_name(mssql, "pk_rosterg_user_jid") -> "rostergroups_username_jid";
×
574
old_index_name(mssql, "i_rosteru_jid") -> "rosterusers_jid";
×
575
old_index_name(mssql, "i_rosteru_username") -> "rosterusers_username";
×
576
old_index_name(mssql, "i_rosteru_user_jid") -> "rosterusers_username_jid";
×
577
old_index_name(mssql, "i_despool") -> "spool_username";
×
578
old_index_name(mssql, "i_sr_user_jid_grp") -> "sr_user_jid_group";
×
579
old_index_name(mssql, Index) -> string:substr(Index, 3);
×
580
old_index_name(_Type, Index) -> Index.
×
581

582
new_index_name(mssql, "i_rosterg_sh_user_jid") -> "rostergroups_sh_username_jid";
×
583
new_index_name(mssql, "i_rosteru_sh_jid") -> "rosterusers_sh_jid";
×
584
new_index_name(mssql, "i_rosteru_sh_user_jid") -> "rosterusers_sh_username_jid";
×
585
new_index_name(mssql, "i_sr_user_sh_jid_grp") -> "sr_user_sh_jid_group";
×
586
new_index_name(mssql, Index) -> string:substr(Index, 3);
×
587
new_index_name(_Type, Index) -> Index.
×
588

589
mssql_clustered("i_mix_pam") -> "";
×
590
mssql_clustered("i_push_session_susn") -> "";
×
591
mssql_clustered(_) -> "CLUSTERED ".
×
592

593
mysql_keylen(_, "bare_peer") -> "(191)";
×
594
mysql_keylen(_, "channel") -> "(191)";
×
595
mysql_keylen(_, "domain") -> "(75)";
×
596
mysql_keylen(_, "jid") -> "(75)";
×
597
mysql_keylen(_, "name") -> "(75)";
×
598
mysql_keylen(_, "node") -> "(75)";
×
599
mysql_keylen(_, "peer") -> "(191)";
×
600
mysql_keylen(_, "pid") -> "(75)";
×
601
mysql_keylen(_, "server_host") -> "(191)";
×
602
mysql_keylen(_, "service") -> "(191)";
×
603
mysql_keylen(_, "topic") -> "(191)";
×
604
mysql_keylen("i_privacy_list_sh_username_name", "username") -> "(75)";
×
605
mysql_keylen("i_rosterg_sh_user_jid", "username") -> "(75)";
×
606
mysql_keylen("i_rosteru_sh_user_jid", "username") -> "(75)";
×
607
mysql_keylen(_, "username") -> "(191)";
×
608
mysql_keylen(_, _) -> "".
×
609

610
sql_query(Host, Query) ->
611
    sql_query(Host, Query, true).
×
612

613
sql_query(Host, Query, Log) ->
614
    case Log of
×
615
        true -> io:format("executing \"~ts\" on ~ts~n", [Query, Host]);
×
616
        false -> ok
×
617
    end,
618
    case ejabberd_sql:sql_query(Host, Query) of
×
619
        {selected, _Cols, []} ->
620
            false;
×
621
        {selected, _Cols, [_Rows]} ->
622
            true;
×
623
        {error, Error} ->
624
            io:format("error: ~p~n", [Error]),
×
625
            false;
×
626
        _ ->
627
            ok
×
628
    end.
629

630
mod_options(_) -> [].
3✔
631

632
mod_doc() ->
633
    #{desc =>
×
634
          ?T("This module can be used to update existing SQL database "
635
             "from the default to the new schema. Check the section "
636
             "http://../database/#default-and-new-schemas[Default and New Schemas] for details. "
637
             "Please note that only MS SQL, MySQL, and PostgreSQL are supported. "
638
             "When the module is loaded use _`update_sql`_ API.")}.
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc