aboutsummaryrefslogtreecommitdiffstats
path: root/test/mysqlerl_test_lib.erl
blob: 79a4ee5fba1571984f261c5ead6da17582ba4317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%%% @author Brian Cully <bjc@kublai.com>
%%% @copyright (C) 2012, Brian Cully
%%% @doc
%%%
%%% @end
%%% Created :  9 Feb 2012 by Brian Cully <bjc@kublai.com>

-module(mysqlerl_test_lib).

-compile(export_all).

-include_lib("common_test/include/ct.hrl").

mysql_cmd(undefined, undefined) ->
    "mysql";
mysql_cmd(User, undefined) ->
    io_lib:format("mysql -u'~s'", [User]);
mysql_cmd(undefined, Pass) ->
    io_lib:format("mysql -p'~s'", [Pass]);
mysql_cmd(User, Pass) ->
    io_lib:format("mysql -u'~s' -p'~s'", [User, Pass]).

create_db(_Config) ->
    DBInfo = ct:get_config(db_info),
    User   = ?config(username, DBInfo),
    Pass   = ?config(password, DBInfo),
    Name   = ?config(name, DBInfo),
    create_db(User, Pass, Name).


create_db(User, Pass, Name) ->
    drop_db(User, Pass, Name),
    SQL = io_lib:format("CREATE DATABASE ~s", [Name]),
    CMD = mysql_cmd(User, Pass),
    os:cmd(io_lib:format("echo '~s' | ~s", [SQL, CMD])),
    ok.

drop_db(_Config) ->
    DBInfo = ct:get_config(db_info),
    User   = ?config(username, DBInfo),
    Pass   = ?config(password, DBInfo),
    Name   = ?config(name, DBInfo),
    drop_db(User, Pass, Name).

drop_db(User, Pass, Name) ->
    SQL = io_lib:format("DROP DATABASE IF EXISTS ~s", [Name]),
    CMD = mysql_cmd(User, Pass),
    os:cmd(io_lib:format("echo '~s' | ~s", [SQL, CMD])),
    ok.

create_table(Config) ->
    DBInfo  = ct:get_config(db_info),
    User    = ?config(username, DBInfo),
    Pass    = ?config(password, DBInfo),
    Name    = ?config(name, DBInfo),
    DataDir = ?config(data_dir, Config),
    create_table(User, Pass, Name, DataDir).

create_table(User, Pass, Name, DataDir) ->
    CMD = mysql_cmd(User, Pass),
    os:cmd(io_lib:format("~s ~s < ~s/table-data.sql", [CMD, Name, DataDir])),
    ok.