postgres_migration/shell.nix

62 lines
1.6 KiB
Nix
Raw Normal View History

2021-02-05 20:14:43 +01:00
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
let
2021-02-08 02:19:07 +01:00
psql_file = "psql_creation.sql";
psql_dir = "$(pwd)/.pgdata";
2021-02-08 02:53:21 +01:00
mysql_file = "mysql_creation.sql";
2021-02-08 02:19:07 +01:00
mysql_dir = "$(pwd)/.mysql";
socket = "${mysql_dir}/mysql.sock";
2021-02-08 09:42:06 +01:00
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";
2021-02-05 20:14:43 +01:00
in mkShell {
2021-02-08 02:19:07 +01:00
buildInputs = [
postgresql
mysql57
pgloader
python38Packages.alembic
python38Packages.pymysql
];
2021-02-05 20:14:43 +01:00
shellHook = ''
trap "kill 0" EXIT
2021-02-08 02:19:07 +01:00
export PGDATA="${psql_dir}"
export PGHOST="${psql_dir}"
2021-02-05 20:14:43 +01:00
2021-02-08 02:19:07 +01:00
# Initialize PostgreSQL
if [ ! -d ${psql_dir} ]; then
2021-02-05 20:14:43 +01:00
initdb --auth-local=trust --no-locale --encoding=UTF8
fi
2021-02-05 21:16:35 +01:00
if ! pg_ctl status; then
2021-02-08 02:19:07 +01:00
pg_ctl start -o "--unix_socket_directories=${psql_dir} --listen_addresses='''"
2021-02-05 21:16:35 +01:00
fi
2021-02-05 20:14:43 +01:00
2021-02-08 02:53:21 +01:00
psql -d postgres -f ${psql_file}
2021-02-08 02:19:07 +01:00
# 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
2021-02-08 02:53:21 +01:00
mysql --socket="${socket}" -u root < ${mysql_file}
2021-02-05 20:14:43 +01:00
alias psql='psql -d postgres'
2021-02-08 02:19:07 +01:00
alias mysql='mysql --socket="${socket}" -u root'
alias nuke='rm -rf ${psql_dir} ${mysql_dir}'
2021-02-05 21:16:35 +01:00
2021-02-08 02:19:07 +01:00
alembic upgrade head
2021-02-08 09:42:06 +01:00
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
2021-02-05 20:14:43 +01:00
'';
}