Skip to content

Commit 2a719c1

Browse files
committed
Add testing conditionals for uuid
1 parent b5c0054 commit 2a719c1

4 files changed

Lines changed: 69 additions & 26 deletions

File tree

src/sql_bridge_test.erl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,33 @@ gen_setup(Adapter, ReplacementType, Host, Port) ->
6666
application:set_env(sql_bridge, replacement_token_style, ReplacementType),
6767
application:set_env(sql_bridge, stringify_binaries, true),
6868
sql_bridge:start(),
69+
case Adapter of
70+
sql_bridge_epgsql -> set_feature(uuid, true);
71+
sql_bridge_mysql_otp -> check_mysql_feature(uuid)
72+
end,
6973
?DB:q("delete from fruit").
7074

75+
check_mysql_feature(uuid) ->
76+
Vsn = ?DB:fffr("select @@version"),
77+
io:format("MySQL Version: ~p", [Vsn]),
78+
Enabled = case re:run(Vsn, "mariadb", [caseless, {capture, none}]) of
79+
match ->
80+
io:format("UUID Support Enabled~n"),
81+
true;
82+
nomatch ->
83+
io:format("UUID Tests Disabled~n"),
84+
false
85+
end,
86+
set_feature(uuid, Enabled).
87+
88+
set_feature(Feature, TF) ->
89+
persistent_term:put({sql_bridge_feature, Feature}, TF).
90+
91+
get_feature(Feature) ->
92+
persistent_term:get({sql_bridge_feature, Feature}).
93+
94+
get_feature_uuid() ->
95+
get_feature(uuid).
7196

7297
epgsql_cleanup(_) ->
7398
application:stop(epgsql),
@@ -206,6 +231,7 @@ trans_status(StartTime, Tag, Msg, Args) ->
206231
Msg2 = "(~p) Trans Update [Tag = ~p] (~pms Elapsed): " ++ Msg ++ "\n",
207232
logger:notice(Msg2, Args2).
208233

234+
209235
main_tests(_) ->
210236
[
211237
?_assertEqual([], ?DB:q("select * from fruit")),
@@ -278,9 +304,11 @@ main_tests(_) ->
278304
?_assert(test_key_variety(other_auto, fun is_nonzero_integer/1)),
279305
?_assert(test_key_variety(other_int, fun is_nonzero_integer/1)),
280306
?_assert(test_key_variety(other_string, fun is_string/1)),
281-
?_assert(test_key_variety(other_uuid, fun is_uuid/1))
307+
?_assert(maybe_test(fun get_feature_uuid/0, fun() -> test_key_variety(other_uuid, fun is_uuid/1) end))
282308
].
283309

310+
311+
284312
is_nonzero_integer(X) ->
285313
is_integer(X) andalso X=/=0.
286314

@@ -327,6 +355,12 @@ test_null() ->
327355
ID = ?DB:pl(other, [{otherid, 0}, {my_decimal, 123.5}]),
328356
?DB:field(other, my_date, ID).
329357

358+
maybe_test(CondFun, TestFun) ->
359+
case CondFun() of
360+
true -> TestFun();
361+
false -> true
362+
end.
363+
330364
test_key_variety(Table, TypeFun) ->
331365
ID = ?DB:save(Table, [{some_text, "random_text"}]),
332366
true=TypeFun(ID),

test/db_config.sh

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,37 @@ PGCMD="/usr/bin/psql -q"
44
MYSQLCMD="/usr/bin/mysql --verbose"
55

66
if [[ -z $GITHUB_ACTION ]]; then
7-
## We're not in a github action, so we're going to use the ubuntu
8-
## out-of-the-box configuration to connect to postgres and mysql
9-
## with sudo.
10-
## This could be reworked to install a local docker image, then remove it
11-
## after the fact, but for now, this is the method we're going to use
12-
PGCMD="sudo -u postgres $PGCMD"
13-
MYSQLCMD="sudo $MYSQLCMD"
7+
## We're not in a github action, so we're going to use the ubuntu
8+
## out-of-the-box configuration to connect to postgres and mysql
9+
## with sudo.
10+
## This could be reworked to install a local docker image, then remove it
11+
## after the fact, but for now, this is the method we're going to use
12+
PGCMD="sudo -u postgres $PGCMD"
13+
MYSQLCMD="sudo $MYSQLCMD"
1414
else
15-
MYSQLCMD="$MYSQLCMD -uroot"
15+
MYSQLCMD="$MYSQLCMD -uroot"
1616
fi
1717

18-
1918
echo "[*] Initializing the PostreSQL database and user"
20-
$PGCMD < test/postgres/pg-test-1.sql
19+
$PGCMD <test/postgres/pg-test-1.sql
2120
echo "[*] Populating the PostreSQL database"
22-
$PGCMD sql_bridge_test < test/postgres/pg-test-2.sql
21+
$PGCMD sql_bridge_test <test/postgres/pg-test-2.sql
2322
echo "[*] Creating and populating the MySQL database"
24-
$MYSQLCMD < test/mysql/create-database.sql
23+
$MYSQLCMD <test/mysql/create-database.sql
24+
25+
echo "[*] Checking if MySQL is actually MariaDB"
26+
MARIA=$($MYSQLCMD -B sql_bridge_test -e "select version()" | grep -i mariadb)
27+
if [[ -n $MARIA ]]; then
28+
echo "[*] MySQL is MariaDB ($MARIA). Adding UUID Field Types"
29+
$MYSQLCMD <test/mysql/uuid.sql
30+
else
31+
echo "[ ] MySQL is NOT MariaDB. Skipping UUID Type Fields"
32+
fi
2533

2634
if [[ -z $GITHUB_ACTION ]]; then
27-
## we only initialize the user if we're not running this from a github action.
28-
## This is because the workflows file in .github/ is manually creating the
29-
## users
30-
echo "[*] Initializing the MySQL test user"
31-
$MYSQLCMD < test/mysql/init-user.sql
35+
## we only initialize the user if we're not running this from a github action.
36+
## This is because the workflows file in .github/ is manually creating the
37+
## users
38+
echo "[*] Initializing the MySQL test user"
39+
$MYSQLCMD <test/mysql/init-user.sql
3240
fi

test/mysql/create-database.sql

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ create table other (
2323
si smallint,
2424
bi bigint,
2525
vc varchar(20),
26-
c char(20),
27-
u uuid
26+
c char(20)
2827
);
2928

3029
-- testing IDs that aren't tablename + 'id'
@@ -46,9 +45,3 @@ create table other_string (
4645
id varchar(25) primary key,
4746
some_text text
4847
);
49-
50-
drop table if exists other_uuid;
51-
create table other_uuid (
52-
id uuid primary key,
53-
some_text text
54-
);

test/mysql/uuid.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use sql_bridge_test;
2+
alter table other add u uuid;
3+
4+
drop table if exists other_uuid;
5+
create table other_uuid (
6+
id uuid primary key,
7+
some_text text
8+
);

0 commit comments

Comments
 (0)