-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrisekit_db_init.sh
163 lines (143 loc) · 5.49 KB
/
risekit_db_init.sh
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
set -e
db_name="postgres"
s3_server_name="s3_risekit_files_production"
foreign_db_server_name="risekit_production_db_replica"
echo "Setting up extensions"
psql -U postgres -d ${db_name} -p 5433 -c "CREATE EXTENSION IF NOT EXISTS pg_lakehouse;"
psql -U postgres -d ${db_name} -p 5433 -c "CREATE EXTENSION IF NOT EXISTS postgres_fdw;"
psql -U postgres -d ${db_name} -p 5433 -c "CREATE EXTENSION IF NOT EXISTS postgis;"
psql -U postgres -d ${db_name} -p 5433 -c "CREATE EXTENSION IF NOT EXISTS postgis_topology;"
psql -U postgres -d ${db_name} -p 5433 -c "CREATE EXTENSION IF NOT EXISTS pg_cron;"
echo "Setting up pg_lakehouse access to S3"
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
DO
\$\$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_foreign_data_wrapper
WHERE fdwname = 's3_wrapper'
) THEN
CREATE FOREIGN DATA WRAPPER s3_wrapper
HANDLER s3_fdw_handler
VALIDATOR s3_fdw_validator;
END IF;
END
\$\$;
SQL
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
CREATE SERVER IF NOT EXISTS ${s3_server_name}
FOREIGN DATA WRAPPER s3_wrapper
OPTIONS (region '${AWS_REGION}');
SQL
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
CREATE USER MAPPING IF NOT EXISTS FOR postgres
SERVER ${s3_server_name}
OPTIONS (
access_key_id '${AWS_ACCESS_KEY_ID}',
secret_access_key '${AWS_SECRET_ACCESS_KEY}'
);
SQL
echo "Setting up foreign S3 tables"
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
DROP FOREIGN TABLE IF EXISTS recommendations_dictionaries;
CREATE FOREIGN TABLE IF NOT EXISTS recommendations_dictionaries (
Json_record TEXT
)
SERVER ${s3_server_name}
OPTIONS (path 's3://rise-kit-files-production/recommendations/parquet/', extension 'parquet');
SQL
echo "Setting up foreign database tables"
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
CREATE SERVER IF NOT EXISTS ${foreign_db_server_name}
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '${APPLICATION_DB_HOSTNAME}', port '${APPLICATION_DB_PORT}', dbname '${APPLICATION_DB_NAME}');
SQL
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
CREATE USER MAPPING IF NOT EXISTS FOR postgres
SERVER ${foreign_db_server_name}
OPTIONS (user '${APPLICATION_DB_USERNAME}', password '${APPLICATION_DB_PASSWORD}');
SQL
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
IMPORT FOREIGN SCHEMA public
LIMIT TO (
ahoy_events,
ahoy_visits,
resumes,
shared_resources,
genders,
ethnicities,
military_statuses,
groups,
teams,
criminal_record_options,
resource_links,
candidate_profiles,
organizations
)
FROM SERVER ${foreign_db_server_name} INTO public
SQL
# We need to create a foreign table for the users table because
# the users table includes encryted columns that we don't need
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
CREATE FOREIGN TABLE IF NOT EXISTS users (
id bigint NOT NULL,
email character varying DEFAULT ''::character varying,
reset_password_sent_at timestamp without time zone,
remember_created_at timestamp without time zone,
sign_in_count integer DEFAULT 0 NOT NULL,
current_sign_in_at timestamp without time zone,
last_sign_in_at timestamp without time zone,
current_sign_in_ip character varying,
last_sign_in_ip character varying,
confirmed_at timestamp without time zone,
confirmation_sent_at timestamp without time zone,
unconfirmed_email character varying,
unlock_token character varying,
locked_at timestamp without time zone,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL,
first_name character varying,
last_name character varying,
mobile_phone character varying,
phone_verified boolean DEFAULT false,
phone_code_expire_at timestamp without time zone,
risekit_username character varying,
account_type_id bigint DEFAULT 0,
organization_id bigint,
middle_name character varying,
agree_terms_privacy_policy boolean DEFAULT false,
admin boolean DEFAULT false,
one_time_code_expire_at timestamp without time zone,
one_time_code_verified boolean DEFAULT false,
merge_data jsonb DEFAULT '{}'::jsonb,
referrer_data jsonb DEFAULT '{}'::jsonb,
success_plan jsonb DEFAULT '{}'::jsonb,
referrer_id bigint,
ukg_data jsonb DEFAULT '{}'::jsonb,
discarded_at timestamp without time zone,
slug character varying
)
SERVER ${foreign_db_server_name}
OPTIONS (table_name 'users');
SQL
echo "Setting up views"
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
CREATE MATERIALIZED VIEW recommendations_view AS
SELECT "json_record"::jsonb AS data
FROM recommendations_dictionaries;
SQL
# at minute 0 of every hour
once_every_hour="0 * * * *"
# NOTE: overwrites the existing cron job with the same name
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
SELECT cron.schedule('refresh_recommendations_view',
'${once_every_hour}',
'REFRESH MATERIALIZED VIEW recommendations_view');
SQL
# needed to that pg_cron switches to a domain socket connection when connecting
# to the local database; see https://github.com/citusdata/pg_cron/issues/48#issuecomment-386207327
psql -U postgres -d ${db_name} -p 5433 --set ON_ERROR_STOP=on <<-SQL
UPDATE cron.job SET nodename = '';
SQL