Add MySQL and alembic

This commit is contained in:
coolneng 2021-02-08 02:19:07 +01:00
parent ad63122878
commit b5d4ed519d
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
3 changed files with 51 additions and 17 deletions

11
mysql_creation.sql Normal file
View File

@ -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;

View File

@ -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;

View File

@ -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
'';
}