postgres_migration/shell.nix

62 lines
1.6 KiB
Nix

{ pkgs ? import <nixpkgs> { } }:
with pkgs;
let
psql_file = "psql_creation.sql";
psql_dir = "$(pwd)/.pgdata";
mysql_file = "mysql_creation.sql";
mysql_dir = "$(pwd)/.mysql";
socket = "${mysql_dir}/mysql.sock";
prod_origin = "mysql://root@unix:${socket}:/odyfo";
prod_destination = "postgresql://localhost/odyfo";
test_origin = "mysql://root@unix:${socket}:/test_odyfo";
test_destination = "postgresql://localhost/test_odyfo";
in mkShell {
buildInputs = [
postgresql
mysql57
pgloader
python38Packages.alembic
python38Packages.pymysql
];
shellHook = ''
trap "kill 0" EXIT
export PGDATA="${psql_dir}"
export PGHOST="${psql_dir}"
# 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=${psql_dir} --listen_addresses='''"
fi
psql -d postgres -f ${psql_file}
# 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
mysql --socket="${socket}" -u root < ${mysql_file}
alias psql='psql -d postgres'
alias mysql='mysql --socket="${socket}" -u root'
alias nuke='rm -rf ${psql_dir} ${mysql_dir}'
alembic upgrade head
pgloader ${prod_origin} ${prod_destination}
pgloader ${test_origin} ${test_destination}
pg_dump odyfo > databases/psql_prod_db.sql
pg_dump test_odyfo > databases/psql_test_db.sql
'';
}