-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdata-replication
More file actions
executable file
·46 lines (36 loc) · 1.32 KB
/
data-replication
File metadata and controls
executable file
·46 lines (36 loc) · 1.32 KB
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
#!/usr/bin/env bash
# Data replication script - creates read-only database role and keeps container running
set -e
echo "Starting data replication setup..."
if [ -z "$READ_ONLY_DB_PASSWORD" ]; then
echo "Error: READ_ONLY_DB_PASSWORD environment variable is not set"
exit 1
fi
echo "Creating read-only database role..."
bundle exec rails runner "
begin
ActiveRecord::Base.connection.execute(\"
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_catalog.pg_roles WHERE rolname = 'grafana_read_only') THEN
CREATE ROLE grafana_read_only WITH LOGIN PASSWORD '#{ENV['READ_ONLY_DB_PASSWORD']}';
ELSE
ALTER ROLE grafana_read_only WITH PASSWORD '#{ENV['READ_ONLY_DB_PASSWORD']}';
END IF;
END
\$\$;
\")
ActiveRecord::Base.connection.execute(\"
GRANT CONNECT ON DATABASE #{ActiveRecord::Base.connection.current_database} TO grafana_read_only;
GRANT USAGE ON SCHEMA public TO grafana_read_only;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO grafana_read_only;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO grafana_read_only;
\")
puts 'Read-only role created/updated successfully'
rescue => e
puts \"Error creating read-only role: \#{e.message}\"
exit 1
end
"
echo "Data replication setup completed. Keeping container running..."
exec tail -f /dev/null