postgres_migration/shell.nix

34 lines
717 B
Nix
Raw Normal View History

2021-02-05 20:14:43 +01:00
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
let
2021-02-05 21:16:35 +01:00
sql_file = "psql_creation.sql";
data_dir = "$(pwd)/.pgdata";
origin = "odyfo.db";
2021-02-05 20:14:43 +01:00
in mkShell {
2021-02-05 21:16:35 +01:00
buildInputs = [ postgresql pgloader ];
2021-02-05 20:14:43 +01:00
shellHook = ''
trap "kill 0" EXIT
2021-02-05 21:16:35 +01:00
export PGDATA="${data_dir}"
export PGHOST="${data_dir}"
2021-02-05 20:14:43 +01:00
2021-02-05 21:16:35 +01:00
if [ ! -d ${data_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
pg_ctl start -o "--unix_socket_directories=${data_dir} --listen_addresses='''"
fi
2021-02-05 20:14:43 +01:00
2021-02-05 21:16:35 +01:00
psql -d postgres -f ${sql_file}
2021-02-05 20:14:43 +01:00
alias psql='psql -d postgres'
2021-02-05 21:16:35 +01:00
alias nuke='rm -rf ${data_dir}'
pgloader ${origin} postgresql:///odyfo
pg_dump odyfo > migrated_db.sql
2021-02-05 20:14:43 +01:00
'';
}