25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Don't attempt to create SQL tables when running tests

Instead, only clear existing tables.
The commit assumes that the SQL schemas will be loaded
manually before running the test suite, see test/README
for the examples.
This commit is contained in:
Evgeny Khramtsov 2019-10-28 12:09:14 +03:00
parent a202818037
commit e2b79ea0b6
2 changed files with 11 additions and 9 deletions

View File

@ -14,6 +14,7 @@ $ psql template1
template1=# CREATE USER ejabberd_test WITH PASSWORD 'ejabberd_test';
template1=# CREATE DATABASE ejabberd_test;
template1=# GRANT ALL PRIVILEGES ON DATABASE ejabberd_test TO ejabberd_test;
$ psql ejabberd_test -f sql/pg.sql
-------------------
MySQL
@ -22,3 +23,4 @@ $ mysql
mysql> CREATE USER 'ejabberd_test'@'localhost' IDENTIFIED BY 'ejabberd_test';
mysql> CREATE DATABASE ejabberd_test;
mysql> GRANT ALL ON ejabberd_test.* TO 'ejabberd_test'@'localhost';
$ mysql ejabberd_test < sql/mysql.sql

View File

@ -99,7 +99,7 @@ do_init_per_group(mysql, Config) ->
case catch ejabberd_sql:sql_query(?MYSQL_VHOST, [<<"select 1;">>]) of
{selected, _, _} ->
mod_muc:shutdown_rooms(?MYSQL_VHOST),
create_sql_tables(mysql, ?config(base_dir, Config)),
clear_sql_tables(mysql, ?config(base_dir, Config)),
set_opt(server, ?MYSQL_VHOST, Config);
Err ->
{skip, {mysql_not_available, Err}}
@ -108,7 +108,7 @@ do_init_per_group(pgsql, Config) ->
case catch ejabberd_sql:sql_query(?PGSQL_VHOST, [<<"select 1;">>]) of
{selected, _, _} ->
mod_muc:shutdown_rooms(?PGSQL_VHOST),
create_sql_tables(pgsql, ?config(base_dir, Config)),
clear_sql_tables(pgsql, ?config(base_dir, Config)),
set_opt(server, ?PGSQL_VHOST, Config);
Err ->
{skip, {pgsql_not_available, Err}}
@ -989,9 +989,9 @@ bookmark_conference() ->
%%%===================================================================
%%% SQL stuff
%%%===================================================================
create_sql_tables(sqlite, _BaseDir) ->
clear_sql_tables(sqlite, _BaseDir) ->
ok;
create_sql_tables(Type, BaseDir) ->
clear_sql_tables(Type, BaseDir) ->
{VHost, File} = case Type of
mysql ->
Path = case ejabberd_sql:use_new_schema() of
@ -1012,13 +1012,13 @@ create_sql_tables(Type, BaseDir) ->
end,
SQLFile = filename:join([BaseDir, "sql", File]),
CreationQueries = read_sql_queries(SQLFile),
DropTableQueries = drop_table_queries(CreationQueries),
ClearTableQueries = clear_table_queries(CreationQueries),
case ejabberd_sql:sql_transaction(
VHost, DropTableQueries ++ CreationQueries) of
VHost, ClearTableQueries) of
{atomic, ok} ->
ok;
Err ->
ct:fail({failed_to_create_sql_tables, Type, Err})
ct:fail({failed_to_clear_sql_tables, Type, Err})
end.
read_sql_queries(File) ->
@ -1029,12 +1029,12 @@ read_sql_queries(File) ->
ct:fail({open_file_failed, File, Err})
end.
drop_table_queries(Queries) ->
clear_table_queries(Queries) ->
lists:foldl(
fun(Query, Acc) ->
case split(str:to_lower(Query)) of
[<<"create">>, <<"table">>, Table|_] ->
[<<"DROP TABLE IF EXISTS ", Table/binary, ";">>|Acc];
[<<"DELETE FROM ", Table/binary, ";">>|Acc];
_ ->
Acc
end