From b5d4ed519dec1836246190e5af84b4a8c4543dd4 Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 8 Feb 2021 02:19:07 +0100 Subject: [PATCH] Add MySQL and alembic --- mysql_creation.sql | 11 ++++++++++ psql_creation.sql | 6 ++---- shell.nix | 51 ++++++++++++++++++++++++++++++++++------------ 3 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 mysql_creation.sql diff --git a/mysql_creation.sql b/mysql_creation.sql new file mode 100644 index 0000000..4f31025 --- /dev/null +++ b/mysql_creation.sql @@ -0,0 +1,11 @@ +CREATE DATABASE odyfo CHARACTER SET utf8mb4; + +CREATE DATABASE test_odyfo CHARACTER SET utf8mb4; + +USE odyfo; + +source databases/production_db.sql; + +USE test; + +source databases/test_db.sql; diff --git a/psql_creation.sql b/psql_creation.sql index d9b2a1d..b3ccca0 100644 --- a/psql_creation.sql +++ b/psql_creation.sql @@ -1,5 +1,3 @@ -CREATE ROLE odyfo WITH LOGIN PASSWORD 'rajjout'; +CREATE DATABASE odyfo; -CREATE DATABASE odyfo WITH OWNER odyfo; - -CREATE DATABASE test_odyfo WITH OWNER odyfo; +CREATE DATABASE test_odyfo; diff --git a/shell.nix b/shell.nix index 40614cd..8eefc0e 100644 --- a/shell.nix +++ b/shell.nix @@ -3,34 +3,59 @@ with pkgs; let - sql_file = "psql_creation.sql"; - data_dir = "$(pwd)/.pgdata"; - origin = "odyfo.db"; + psql_file = "psql_creation.sql"; + psql_dir = "$(pwd)/.pgdata"; + mysql_file = "databases/mysql_creation.sql"; + mysql_dir = "$(pwd)/.mysql"; + socket = "${mysql_dir}/mysql.sock"; + origin = "mysql:///odyfo"; + destination = "postgresql:///odyfo"; in mkShell { - buildInputs = [ postgresql pgloader ]; + buildInputs = [ + postgresql + mysql57 + pgloader + python38Packages.alembic + python38Packages.pymysql + ]; shellHook = '' trap "kill 0" EXIT - export PGDATA="${data_dir}" - export PGHOST="${data_dir}" + export PGDATA="${psql_dir}" + export PGHOST="${psql_dir}" - if [ ! -d ${data_dir} ]; then + # Initialize PostgreSQL + if [ ! -d ${psql_dir} ]; then initdb --auth-local=trust --no-locale --encoding=UTF8 fi if ! pg_ctl status; then - pg_ctl start -o "--unix_socket_directories=${data_dir} --listen_addresses='''" + pg_ctl start -o "--unix_socket_directories=${psql_dir} --listen_addresses='''" fi - if [ ! -d ${data_dir} ]; then - psql -d postgres -f ${sql_file} + if [ ! -d ${psql_dir} ]; then + psql -d postgres -f ${psql_file} + fi + + # Initialize MySQL + if [ ! -d ${mysql_dir} ]; then + mysqld --datadir="${mysql_dir}" --socket="${socket}" --initialize-insecure + fi + + mysqld --datadir="${mysql_dir}" --socket="${socket}" --skip-networking & + sleep 5 + + if [ ! -d ${mysql_dir} ]; then + mysql --socket="${socket}" -u root < ${mysql_file} fi alias psql='psql -d postgres' - alias nuke='rm -rf ${data_dir}' + alias mysql='mysql --socket="${socket}" -u root' + alias nuke='rm -rf ${psql_dir} ${mysql_dir}' - pgloader ${origin} postgresql:///odyfo - pg_dump odyfo > migrated_db.sql + alembic upgrade head + pgloader ${origin} ${destination} + pg_dump odyfo > databases/migrated_db.sql ''; }