Class: Ardb::Adapter::Postgresql
- Inherits:
-
Base
- Object
- Base
- Ardb::Adapter::Postgresql
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, #migrate_db_backward, #migrate_db_down, #migrate_db_forward, #migrate_db_up, #migrations_path, #ruby_schema_path, #schema_format, #sql_schema_path
Instance Method Details
#create_db ⇒ Object
15
16
17
18
19
|
# File 'lib/ardb/adapter/postgresql.rb', line 15
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_db ⇒ Object
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_connect_hash)
ActiveRecord::Base.connection.tap do |conn|
conn.execute "UPDATE pg_catalog.pg_database"\
" SET datallowconn=false WHERE datname='#{self.database}'"
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_tables ⇒ Object
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_schema ⇒ Object
54
55
56
57
58
59
|
# File 'lib/ardb/adapter/postgresql.rb', line 54
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
|
#load_sql_schema ⇒ Object
47
48
49
50
51
52
|
# File 'lib/ardb/adapter/postgresql.rb', line 47
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_hash ⇒ Object
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
8
9
10
11
12
13
|
# File 'lib/ardb/adapter/postgresql.rb', line 8
def public_connect_hash
@public_connect_hash ||= self.connect_hash.merge({
"database" => "postgres",
"schema_search_path" => "public"
})
end
|