Class: Brillo::Adapter::Postgres

Inherits:
Base
  • Object
show all
Defined in:
lib/brillo/adapter/postgres.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#dump_structure_and_migrations, #footer, #header, #initialize

Methods included from Logger

#logger, logger, logger=

Constructor Details

This class inherits a constructor from Brillo::Adapter::Base

Instance Method Details

#load_commandObject



4
5
6
7
8
9
10
# File 'lib/brillo/adapter/postgres.rb', line 4

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

#recreate_dbObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brillo/adapter/postgres.rb', line 22

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

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



14
15
16
17
18
19
20
# File 'lib/brillo/adapter/postgres.rb', line 14

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