Skip to content

Commit

Permalink
Adding mysql support to ci
Browse files Browse the repository at this point in the history
  • Loading branch information
kno3comma14 committed Aug 21, 2023
1 parent bf9efe1 commit 6b3cd7e
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 17 deletions.
9 changes: 7 additions & 2 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ version: "3.3"

services:
mysql:
profiles: ["all", "mysql"]
image: percona:5.7
environment:
- MYSQL_ROOT_PASSWORD
ports:
- "3306:3306"
command:
[--character-set-server=utf8mb4, --collation-server=utf8mb4_unicode_ci]
volumes:
- ./sql-scripts:/sql # I don't know too much about volumes(May I share this?). Ask or research about this
# - db-data:/var/lib/postgresql/data

postgres:
postgresql:
profiles: ["all", "postgresql"]
build:
context: .
dockerfile: postgres.dockerfile
dockerfile: postgresql.dockerfile
# environment:
# POSTGRES_DB: framework
volumes:
Expand Down
3 changes: 0 additions & 3 deletions docker/postgres.dockerfile

This file was deleted.

3 changes: 3 additions & 0 deletions docker/postgresql.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM postgres:11.5-alpine

# COPY sql-scripts/postgresql/init.sql /docker-entrypoint-initdb.d
15 changes: 15 additions & 0 deletions docker/sql-scripts/mysql/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Drop the database if it exists
DROP DATABASE IF EXISTS framework;

-- Create the database
CREATE DATABASE framework;

-- Grant privileges to a MySQL user
GRANT ALL PRIVILEGES ON framework.* TO 'username'@'localhost';

-- Create UUID function
CREATE FUNCTION uuid()
RETURNS CHAR(36)
BEGIN
RETURN LOWER(REPLACE(UUID(), '-', ''));
END;
34 changes: 34 additions & 0 deletions docker/sql-scripts/mysql/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Create UUID function
DELIMITER //
CREATE FUNCTION uuid()
RETURNS CHAR(36)
BEGIN
RETURN LOWER(REPLACE(UUID(), '-', ''));
END;
//
DELIMITER ;

-- Drop the table if it exists
DROP TABLE IF EXISTS users;

-- Create the table
CREATE TABLE users
(
id CHAR(36) DEFAULT uuid() PRIMARY KEY,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_login DATETIME,
is_active BOOLEAN,
email VARCHAR(254) NOT NULL,
role VARCHAR(254),
username TEXT NOT NULL,
password TEXT,
salt TEXT,
fullname TEXT
);

-- Insert data into the table
INSERT INTO users (id, created_at, email, role, password, username, is_active)
VALUES
('fd5e0d70-506a-45cc-84d5-b12b5e3e99d2', '2021-03-30 12:34:10', 'admin@frankie.sw', 'admin', '$2a$11$ivfRMKD7dHMfqCWBiEQcaOknsJgDnK9zoSP/cXAVNQVYHc.M9SZJK', 'admin', 1),
('31c2c58f-28cb-4013-8765-9240626a18a2', '2021-03-30 12:34:10', 'frankie@frankie.sw', 'user', '$2a$11$ivf2RMKD7dHMfqCWBiEQcaOknsJgDnK9zoSP/cXAVNQVYHc.M9SZJK', 'frankie', 1),
('8d05b2e1-6463-478a-ba30-35768738af29', '2021-03-30 12:34:10', 'impostor@frankie.sw', 'interviewer', '$2a$11$ivfRMKD7dHMfqCWBiEQcaOknsJgDnK9zoSP/cXAVNQVYHc.M9SZJK', 'impostor', 0);
File renamed without changes.
File renamed without changes.
73 changes: 61 additions & 12 deletions script/auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ _ACTION="tests"
# flags
_YES_FLAG=1

# default dbms
_DBMS=postgresql

# compose variables: file and service
_COMPOSE_FILE=./docker/docker-compose.yml
_COMPOSE_SERVICE=postgres
_COMPOSE_SERVICE_LIST=("postgresql" "mysql")
_COMPOSE_SERVICE=$_DBMS
_COMPOSE_PROFILE=$_DBMS

# default edn config file
_CONFIG=./config/test/config.edn

# sql script
_DB_USER=postgres
_DB_NAME=framework
_DB_SCRIPT=/sql/test.sql
_DB_SCRIPT=/sql/postgresql/test.sql

usage ()
{
Expand All @@ -37,13 +42,13 @@ usage ()
[ AUTO HELPER SCRIPT ]
usage: ${0} [-yxve] [-C {edn_config}] {ACTION}
usage: ${0} [-yxve] [-C {edn_config}] [-D [postgres|mysql]] {ACTION}
ACTIONS:
help show this message and exit
opts show default options
docker setup postgres docker instance
docker setup selected database docker instance
setup execute defined sql scripts
tests run the local tests
all execute docker, setup and tests: shortcut
Expand All @@ -60,6 +65,9 @@ usage ()
-C EDN_CONFIG set the edn configuration file
default: ./config/test/test.edn
-D DBMS set the dbms to use. Options: postgresql, mysql, all
default: postgres
EOF
printf $_RESET
}
Expand Down Expand Up @@ -100,6 +108,7 @@ CONFIG = ${_CONFIG}
DB_USER = ${_DB_USER}
DB_NAME = ${_DB_NAME}
DB_SCRIPT = ${_DB_SCRIPT}
DBMS = ${_DBMS}
\n"
}

Expand Down Expand Up @@ -159,6 +168,9 @@ parse_opts ()

# set edn config file
-C) shift 1; _CONFIG=${1}; shift 1 ;;

# set DBMS
-D) shift 1; _DBMS=${2}; shift 1 ;;
esac
done

Expand Down Expand Up @@ -218,27 +230,64 @@ handle_info ()
exit 0
}

_compose_exec ()
{
local _cmd=$1

docker-compose -f ${_COMPOSE_FILE} exec ${_COMPOSE_SERVICE} bash -c "$_cmd"
}

_compose_up ()
{
# up: create and instantiate postgre service
docker-compose -f ${_COMPOSE_FILE} up -d
COMPOSE_PROFILES=${_COMPOSE_PROFILE} docker-compose -f ${_COMPOSE_FILE} up -d
}

_select_init_command ()
{
local _cs=$1
case ${_cs} in
"postgresql") return "psql -U $_DB_USER -f /sql/postgresql/init.sql" ;;
"mysql") return "mysql -u $_DB_USER -p < /sql/mysql/init.sql" ;;
esac
}

_select_test_command ()
{
local _cs=$1
case ${_cs} in
"postgresql") return "psql -U $_DB_USER -f /sql/postgresql/test.sql" ;;
"mysql") return "mysql -u $_DB_USER -p < /sql/mysql/test.sql" ;;
esac
}

_compose_exec ()
{
local _cmd=$1
local _compose_service=$2

docker-compose -f $_compose_service exec $_compose_service bash -c "$_cmd"
}

_db_init_script ()
{
_compose_exec "psql -U $_DB_USER -f /sql/init.sql"
if [ "${_COMPOSE_SERVICE}" == "all" ]; then
for _cs in ${_COMPOSE_SERVICE_LIST}; do
_command=$_select_init_command ${_cs}
_compose_exec $_command $_cs
done
else
_command=$_select_init_command ${_COMPOSE_SERVICE}
_compose_exec $_command ${_COMPOSE_SERVICE}
fi
}

_db_test_script ()
{
_compose_exec "psql -U $_DB_USER -d $_DB_NAME -f /sql/test.sql"
if [ "${_COMPOSE_SERVICE}" == "all" ]; then
for _cs in ${_COMPOSE_SERVICE_LIST}; do
_command=$_select_test_command ${_cs}
_compose_exec $_command $_cs
done
else
_command=$_select_test_command ${_COMPOSE_SERVICE}
_compose_exec $_command ${_COMPOSE_SERVICE}
fi
}

_setup ()
Expand Down

0 comments on commit 6b3cd7e

Please sign in to comment.