-
Notifications
You must be signed in to change notification settings - Fork 3
/
default.nix
145 lines (118 loc) · 4.35 KB
/
default.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{ pkgs ? import <nixpkgs> {} }:
let
python = pkgs.python3;
in pkgs.mkShell {
buildInputs = [
pkgs.just
pkgs.poetry
pkgs.postgresql # Add PostgreSQL Database
];
shellHook = ''
# SETUP POSTGRES SERVER
echo "Loading environment variables from .env"
if [ -f .env ]; then
export $(cat .env | xargs)
else
echo ".env file not found! Exiting."
exit 1
fi
# Define PGDATA and LOGFILE based on environment variables
export PGDATA=$(pwd)/pgdata
LOGFILE=$(pwd)/pgdata/postgresql.log
echo "Initializing PostgreSQL data directory at $PGDATA"
echo "PGDATA: $PGDATA"
if [ ! -d "$PGDATA" ]; then
initdb -D "$PGDATA" | tee -a $LOGFILE
fi
echo "Stopping any existing PostgreSQL server..."
pg_ctl -D "$PGDATA" stop || echo "No existing server to stop."
echo "Starting PostgreSQL server..."
pg_ctl -D "$PGDATA" -l $LOGFILE start
# Wait for PostgreSQL to start
sleep 2
set -x
# Set up the test database, role, and tables
echo "Setting up the test database..."
echo "POSTGRES_DB=$POSTGRES_DB"
echo "POSTGRES_USER=$POSTGRES_USER"
echo "POSTGRES_PASSWORD=$POSTGRES_PASSWORD"
echo "POSTGRES_HOST=$POSTGRES_HOST"
echo "POSTGRES_PORT=$POSTGRES_PORT"
psql -U dialogues -d postgres -c "ALTER USER $POSTGRES_USER WITH SUPERUSER;"
# Check if the database exists, create if not
psql -U "$POSTGRES_USER" -d postgres -tc "SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB'" | grep -q 1 || \
psql -U "$POSTGRES_USER" -d postgres -c "CREATE DATABASE $POSTGRES_DB;"
psql -d $POSTGRES_DB <<SQL | tee -a $LOGFILE
-- Create the conversation table
CREATE TABLE IF NOT EXISTS conversation (
message_id VARCHAR PRIMARY KEY,
conv_id VARCHAR NOT NULL,
userid VARCHAR NOT NULL,
timestamp TIMESTAMP NOT NULL,
name VARCHAR,
role VARCHAR NOT NULL,
message TEXT NOT NULL
);
-- Create the utterance_token_info table
CREATE TABLE IF NOT EXISTS utterance_token_info (
message_id VARCHAR PRIMARY KEY,
conv_id VARCHAR NOT NULL,
userid VARCHAR NOT NULL,
name VARCHAR,
role VARCHAR NOT NULL,
timestamp TIMESTAMP NOT NULL,
ents JSONB
);
-- Create the utterance_text_info table
CREATE TABLE IF NOT EXISTS utterance_text_info (
message_id VARCHAR PRIMARY KEY,
conv_id VARCHAR NOT NULL,
userid VARCHAR NOT NULL,
name VARCHAR,
role VARCHAR NOT NULL,
timestamp TIMESTAMP NOT NULL,
moderator JSONB,
mod_label VARCHAR,
tern_sent JSONB,
tern_label VARCHAR,
emo_27 JSONB,
emo_27_label VARCHAR
);
CREATE TABLE IF NOT EXISTS groups (
group_id TEXT PRIMARY KEY,
group_name TEXT NOT NULL UNIQUE
);
CREATE TABLE IF NOT EXISTS users (
user_id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
last_seen_online TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS user_groups (
user_id TEXT,
group_id TEXT,
FOREIGN KEY (user_id) REFERENCES users (user_id),
FOREIGN KEY (group_id) REFERENCES groups (group_id),
PRIMARY KEY (user_id, group_id)
);
CREATE INDEX IF NOT EXISTS idx_user_groups_user_id ON user_groups (user_id);
CREATE INDEX IF NOT EXISTS idx_user_groups_group_id ON user_groups (group_id);
CREATE ROLE $POSTGRES_USER WITH LOGIN PASSWORD '$POSTGRES_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE $POSTGRES_DB TO $POSTGRES_USER;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $POSTGRES_USER;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $POSTGRES_USER;
GRANT ALL PRIVILEGES ON SCHEMA public TO $POSTGRES_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO $POSTGRES_USER;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO $POSTGRES_USER;
-- If you're using PostgreSQL 14 or later, you can also add these:
GRANT pg_read_all_data TO $POSTGRES_USER;
GRANT pg_write_all_data TO $POSTGRES_USER;
SQL
echo "PostgreSQL setup complete. Logs can be found at $LOGFILE"
# FINISH POSTGRES SERVER
# BUILD TOPOS PROJECT
echo "Running just build"
just build
echo "Starting the topos server..."
topos run
'';
}