|
| 1 | +%%%------------------------------------------------------------------- |
| 2 | +%%% Author : Pawel Chmielowski <pawel@process-one.net> |
| 3 | +%%% Created : 20 Jul 2026 by Pawel Chmielowski <pawel@process-one.net>p |
| 4 | +%%% |
| 5 | +%%% |
| 6 | +%%% ejabberd, Copyright (C) 2002-2026 ProcessOne |
| 7 | +%%% |
| 8 | +%%% This program is free software; you can redistribute it and/or |
| 9 | +%%% modify it under the terms of the GNU General Public License as |
| 10 | +%%% published by the Free Software Foundation; either version 2 of the |
| 11 | +%%% License, or (at your option) any later version. |
| 12 | +%%% |
| 13 | +%%% This program is distributed in the hope that it will be useful, |
| 14 | +%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | +%%% General Public License for more details. |
| 17 | +%%% |
| 18 | +%%% You should have received a copy of the GNU General Public License along |
| 19 | +%%% with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | +%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | +%%% |
| 22 | +%%%------------------------------------------------------------------- |
| 23 | + |
| 24 | +-module(sqlschemaupdater_tests). |
| 25 | + |
| 26 | +-compile(export_all). |
| 27 | + |
| 28 | +-include("suite.hrl"). |
| 29 | +-include("ejabberd_sql_pt.hrl"). |
| 30 | + |
| 31 | +%%%================================== |
| 32 | + |
| 33 | + |
| 34 | +single_cases() -> |
| 35 | + {sqlschemaupdater_single, |
| 36 | + [sequence], |
| 37 | + [single_test(add_column), |
| 38 | + single_test(change_column)]}. |
| 39 | + |
| 40 | + |
| 41 | +%% Interactions |
| 42 | + |
| 43 | + |
| 44 | +apply_schemas(Config, Schemas) -> |
| 45 | + Server = ?config(server, Config), |
| 46 | + ejabberd_sql:sql_query(Server, <<"drop table if exists schemaupdate;">>), |
| 47 | + ejabberd_sql:sql_query(Server, <<"delete from schema_version where module='schemaupdate';">>), |
| 48 | + lists:foreach( |
| 49 | + fun(#sql_schema{} = Schema) -> |
| 50 | + ?match(ok, ejabberd_sql_schema:update_schema(Server, schemaupdate, [Schema])); |
| 51 | + (ToExec) when is_binary(ToExec) -> |
| 52 | + ejabberd_sql:sql_query(Server, ToExec); |
| 53 | + ([ToExec, Result]) when is_binary(ToExec) -> |
| 54 | + ?match({selected, _, Result}, ejabberd_sql:sql_query(Server, ToExec)) |
| 55 | + end, |
| 56 | + Schemas). |
| 57 | + |
| 58 | + |
| 59 | +add_column(Config) -> |
| 60 | + Schemas = [#sql_schema{ |
| 61 | + version = 1, |
| 62 | + tables = [#sql_table{ |
| 63 | + name = <<"schemaupdate">>, |
| 64 | + columns = [#sql_column{name = <<"first">>, type = integer}] |
| 65 | + }] |
| 66 | + }, |
| 67 | + <<"insert into schemaupdate (first) values (1)">>, |
| 68 | + #sql_schema{ |
| 69 | + version = 2, |
| 70 | + tables = [#sql_table{ |
| 71 | + name = <<"schemaupdate">>, |
| 72 | + columns = [#sql_column{name = <<"first">>, type = integer}#sql_column{name = <<"second">>, type = integer}] |
| 73 | + }], |
| 74 | + update = [{add_column, <<"schemaupdate">>, <<"second">>}] |
| 75 | + }, |
| 76 | + <<"insert into schemaupdate (first, second) values (1, 2)">>, |
| 77 | + [<<"select first, second from schemaupdate order by first, second">>, [[<<"1">>, <<"0">>], [<<"1">>, <<"2">>]]]], |
| 78 | + apply_schemas(Config, Schemas). |
| 79 | + |
| 80 | + |
| 81 | +change_column(Config) -> |
| 82 | + Schemas = [#sql_schema{ |
| 83 | + version = 1, |
| 84 | + tables = [#sql_table{ |
| 85 | + name = <<"schemaupdate">>, |
| 86 | + columns = [#sql_column{name = <<"first">>, type = boolean}] |
| 87 | + }] |
| 88 | + }, |
| 89 | + <<"insert into schemaupdate (first) values (false)">>, |
| 90 | + #sql_schema{ |
| 91 | + version = 2, |
| 92 | + tables = [#sql_table{ |
| 93 | + name = <<"schemaupdate">>, |
| 94 | + columns = [#sql_column{name = <<"first">>, type = integer}] |
| 95 | + }], |
| 96 | + update = [{change_column_type, <<"schemaupdate">>, <<"first">>}] |
| 97 | + }, |
| 98 | + [<<"select first from schemaupdate">>, [[<<"0">>]]], |
| 99 | + <<"update schemaupdate set first=2">>, |
| 100 | + [<<"select first from schemaupdate">>, [[<<"2">>]]]], |
| 101 | + apply_schemas(Config, Schemas). |
| 102 | + |
| 103 | + |
| 104 | +single_test(T) -> |
| 105 | + list_to_atom("sqlschemaupdater_" ++ atom_to_list(T)). |
0 commit comments