postgres_migration/shell.nix

58 lines
1.3 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";
origin = "mysql:///odyfo";
destination = "postgresql:///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
pgloader ${origin} ${destination}
pg_dump odyfo > databases/migrated_db.sql
2021-02-05 20:14:43 +01:00
'';
}