Class: Ardb::Adapter::Postgresql

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

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#==, #connect_db, #connect_hash, #database, #dump_ruby_schema, #dump_schema, #escape_like_pattern, #initialize, #load_ruby_schema, #load_schema, #migrate_db, #migrations_path, #ruby_schema_path, #schema_format, #sql_schema_path

Constructor Details

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

Instance Method Details

#create_dbObject



17
18
19
20
21
# File 'lib/ardb/adapter/postgresql.rb', line 17

def create_db
  ActiveRecord::Base.establish_connection(self.public_connect_hash)
  ActiveRecord::Base.connection.create_database(self.database, self.connect_hash)
  ActiveRecord::Base.establish_connection(self.connect_hash)
end

#drop_dbObject



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

def drop_db
  begin
    ActiveRecord::Base.establish_connection(self.public_connect_hash)
    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



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

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



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

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 => env_var_hash).tap(&:run)
  raise 'Error dumping database' unless cmd.success?
end

#foreign_key_add_sqlObject



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

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



56
57
58
59
# File 'lib/ardb/adapter/postgresql.rb', line 56

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

#load_sql_schemaObject



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

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

#public_connect_hashObject

the ‘postgres’ db is a “public” (doesn’t typically require auth/grants to connect to) db that typically exists for all postgres installations; the adapter uses it to create/drop other databases



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

def public_connect_hash
  @public_connect_hash ||= self.connect_hash.merge({
    'database'           => 'postgres',
    'schema_search_path' => 'public'
  })
end