-
Notifications
You must be signed in to change notification settings - Fork 19
/
postgres.rb
38 lines (36 loc) · 1.45 KB
/
postgres.rb
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
module Brillo
module Adapter
class Postgres < Base
def load_command
host = config["host"] ? "--host #{config["host"]}" : ""
password = config["password"] ? "PGPASSWORD=#{config["password"]} " : ""
search_path = config["schema_search_path"] ? "PGOPTIONS=--search_path=#{config["schema_search_path"]} " : ""
inline_options = password + search_path
"#{inline_options}psql #{host} -U #{config.fetch("username")} #{config.fetch("database")}"
end
# pgdump without schema does not set sequences, so we have to do it ourselves, or the first insert
# into a scrubbed table will fail on duplicate primary key
def table_footer(klass)
table_name = klass.table_name
<<-SQL
SELECT setval(pg_get_serial_sequence('#{table_name}', 'id'), coalesce(MAX(id),0) + 1, false)
FROM #{table_name};
SQL
end
def recreate_db
logger.info "Dropping all connections to #{config[:database]}"
ActiveRecord::Base.connection.execute(
<<-SQL
-- Disconnect all others from the database we are about to drop.
-- Without this, the drop will fail and so the load will abort.
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '#{config[:database]}'
AND pid <> pg_backend_pid();
SQL
)
super
end
end
end
end