Class: Ardb::Adapter::Postgresql

Inherits:
Base
  • Object
show all
Defined in:
lib/ardb/adapter/postgresql.rb

Instance Attribute Summary

Attributes inherited from Base

#config_settings, #database, #ruby_schema_path, #schema_format, #sql_schema_path

Instance Method Summary collapse

Methods inherited from Base

#==, #dump_ruby_schema, #dump_schema, #initialize, #load_ruby_schema, #load_schema

Constructor Details

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

Instance Method Details

#create_dbObject



15
16
17
18
19
# File 'lib/ardb/adapter/postgresql.rb', line 15

def create_db
  ActiveRecord::Base.establish_connection(self.public_schema_settings)
  ActiveRecord::Base.connection.create_database(self.database, self.config_settings)
  ActiveRecord::Base.establish_connection(self.config_settings)
end

#drop_dbObject



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

def drop_db
  begin
    ActiveRecord::Base.establish_connection(self.public_schema_settings)
    ActiveRecord::Base.connection.tap do |conn|
      conn.execute "UPDATE pg_catalog.pg_database"\
                   " SET datallowconn=false WHERE datname='#{self.database}'"
      # this SELECT actually runs a command: it terminates all the connections
      # http://www.postgresql.org/docs/9.2/static/functions-admin.html#FUNCTIONS-ADMIN-SIGNAL-TABLE
      conn.execute "SELECT pg_terminate_backend(pid)"\
                   " FROM pg_stat_activity WHERE datname='#{self.database}'"
      conn.execute "DROP DATABASE IF EXISTS #{self.database}"
    end
  rescue PG::Error => e
    raise e unless e.message =~ /does not exist/
  end
end

#drop_tablesObject



38
39
40
41
42
43
44
45
# File 'lib/ardb/adapter/postgresql.rb', line 38

def drop_tables
  ActiveRecord::Base.connection.tap do |conn|
    tables = conn.execute "SELECT table_name"\
                          " FROM information_schema.tables"\
                          " WHERE table_schema = 'public';"
    tables.each{ |row| conn.execute "DROP TABLE #{row['table_name']} CASCADE" }
  end
end

#dump_sql_schemaObject



66
67
68
69
70
71
# File 'lib/ardb/adapter/postgresql.rb', line 66

def dump_sql_schema
  require 'scmd'
  cmd_str = "pg_dump -i -s -x -O -f \"#{self.sql_schema_path}\" #{self.database}"
  cmd = Scmd.new(cmd_str, env_var_hash).tap(&:run)
  raise 'Error dumping database' unless cmd.success?
end

#foreign_key_add_sqlObject



47
48
49
50
51
52
# File 'lib/ardb/adapter/postgresql.rb', line 47

def foreign_key_add_sql
  "ALTER TABLE :from_table"\
  " ADD CONSTRAINT :name"\
  " FOREIGN KEY (:from_column)"\
  " REFERENCES :to_table (:to_column)"
end

#foreign_key_drop_sqlObject



54
55
56
57
# File 'lib/ardb/adapter/postgresql.rb', line 54

def foreign_key_drop_sql
  "ALTER TABLE :from_table"\
  " DROP CONSTRAINT :name"
end

#load_sql_schemaObject



59
60
61
62
63
64
# File 'lib/ardb/adapter/postgresql.rb', line 59

def load_sql_schema
  require 'scmd'
  cmd_str = "psql -f \"#{self.sql_schema_path}\" #{self.database}"
  cmd = Scmd.new(cmd_str, env_var_hash).tap(&:run)
  raise 'Error loading database' unless cmd.success?
end

#public_schema_settingsObject



8
9
10
11
12
13
# File 'lib/ardb/adapter/postgresql.rb', line 8

def public_schema_settings
  self.config_settings.merge({
    'database'           => 'postgres',
    'schema_search_path' => 'public'
  })
end