Class: Immigrate::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/immigrate/database.rb

Instance Method Summary collapse

Instance Method Details

#connectionObject



38
39
40
# File 'lib/immigrate/database.rb', line 38

def connection
  ActiveRecord::Base.connection
end

#create_fdw_extensionObject



3
4
5
# File 'lib/immigrate/database.rb', line 3

def create_fdw_extension
  enable_extension :postgres_fdw
end

#create_server_connection(server) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/immigrate/database.rb', line 11

def create_server_connection server
  server_config = database_configuration[Rails.env][server.to_s]
  execute <<-SQL
    CREATE SERVER #{server}
    FOREIGN DATA WRAPPER postgres_fdw
    OPTIONS (host '#{server_config['host']}',
             port '#{server_config['port']}',
             dbname '#{server_config['dbname']}')
  SQL
end

#create_user_mapping(server) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/immigrate/database.rb', line 22

def create_user_mapping server
  server_config = database_configuration[Rails.env][server.to_s]
  execute <<-SQL
    CREATE USER MAPPING FOR #{current_user}
    SERVER #{server}
    OPTIONS (user '#{server_config['user']}',
             password '#{server_config['password']}')
  SQL
end

#current_userObject



32
33
34
# File 'lib/immigrate/database.rb', line 32

def current_user
  execute("SELECT CURRENT_USER").first['current_user']
end

#database_configurationObject



42
43
44
45
# File 'lib/immigrate/database.rb', line 42

def database_configuration
  yaml = Pathname.new('config/immigrate.yml')
  YAML.load(ERB.new(yaml.read).result)
end

#drop_fdw_extensionObject



7
8
9
# File 'lib/immigrate/database.rb', line 7

def drop_fdw_extension
  disable_extension :postgres_fdw
end