-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.nix
111 lines (100 loc) · 2.8 KB
/
commands.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
{ pkgs
, extraDDLs ? [ ]
, pgUser ? "tx_indexer"
, pgPort ? "5555"
, pgDir ? ".pg"
, postgresql ? pkgs.postgresql_16
, extraPostgresConf ? ""
, schemaDumpIncludePlutus ? false
}:
let
postgresConf =
pkgs.writeText "postgresql.conf"
''
# Add Custom Settings
log_min_messages = warning
log_min_error_statement = error
log_min_duration_statement = 100 # ms
log_connections = on
log_disconnections = on
log_duration = on
#log_line_prefix = '[] '
log_timezone = 'UTC'
log_statement = 'all'
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
logging_collector = on
log_min_error_statement = error
${extraPostgresConf}
'';
ddls = [
./lib-migrations/00000000000001_plutus/up.sql
./lib-migrations/00000000000002_sync_progress/up.sql
] ++ extraDDLs;
init-db = pkgs.writeShellApplication {
name = "init-db";
runtimeInputs = [ postgresql ];
runtimeEnv = {
LC_CTYPE = "en_US.UTF-8";
LC_ALL = "en_US.UTF-8";
LANG = "en_US.UTF-8";
};
text = "init-empty-db; " + pkgs.lib.concatMapStringsSep "\n" (ddl: "pg < ${ddl}") ddls;
};
init-empty-db = pkgs.writeShellApplication {
name = "init-empty-db";
runtimeInputs = [ postgresql ];
runtimeEnv = {
LC_CTYPE = "en_US.UTF-8";
LC_ALL = "en_US.UTF-8";
LANG = "en_US.UTF-8";
};
text = ''
export PGDATA="$PWD/${pgDir}"
export PGHOST="$PWD/${pgDir}"
[ ! -d "$PGDATA" ] && pg_ctl initdb -o "-U ${pgUser}" && cat "${postgresConf}" >> "$PGDATA/postgresql.conf"
start-db
echo "SELECT 'CREATE DATABASE ${pgUser}' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${pgUser}')\gexec" \
| psql -p "${pgPort}" -U "${pgUser}" postgres
'';
};
start-db = pkgs.writeShellApplication {
name = "start-db";
runtimeInputs = [ postgresql ];
text = ''
export PGDATA="$PWD/${pgDir}";
pg_ctl -o "-p ${pgPort} -k $PGDATA" start
'';
};
stop-db = pkgs.writeShellApplication {
name = "stop-db";
runtimeInputs = [ postgresql ];
text = ''
export PGDATA="$PWD/${pgDir}";
pg_ctl stop && exit
'';
};
pg = pkgs.writeShellApplication {
name = "pg";
runtimeInputs = [ postgresql ];
text = ''
export PGHOST="$PWD/${pgDir}";
psql -p ${pgPort} -U ${pgUser}
'';
};
dump-schema = pkgs.writeShellApplication {
name = "dump-schema";
runtimeInputs = [ postgresql ];
text = ''
pg_dump -sx \
-U ${pgUser} \
-h 127.0.0.1 \
-p ${pgPort} \
${pkgs.lib.optionalString (! schemaDumpIncludePlutus) "-N plutus" }
'';
};
in
{
inherit postgresConf;
devShellTools = [ init-db start-db stop-db pg init-empty-db dump-schema ];
}