Class: GTA::LocalDB

Inherits:
Object
  • Object
show all
Includes:
Sh
Defined in:
lib/gta/local_db.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sh

#commander, #sh, #sh!

Constructor Details

#initialize(env, database_config_path = nil) ⇒ LocalDB

Returns a new instance of LocalDB.



7
8
9
10
# File 'lib/gta/local_db.rb', line 7

def initialize(env, database_config_path=nil)
  @env = env
  @database_config_path = database_config_path || self.class.default_database_config_path
end

Instance Attribute Details

#database_config_pathObject (readonly)

Returns the value of attribute database_config_path.



5
6
7
# File 'lib/gta/local_db.rb', line 5

def database_config_path
  @database_config_path
end

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/gta/local_db.rb', line 5

def env
  @env
end

Class Method Details

.default_database_config_pathObject



40
41
42
# File 'lib/gta/local_db.rb', line 40

def self.default_database_config_path
  "#{Dir.pwd}/config/database.yml"
end

.local_database_envObject



44
45
46
# File 'lib/gta/local_db.rb', line 44

def self.local_database_env
  ENV['RAILS_ENV'] || ENV['GTA_LOCAL_ENV']
end

Instance Method Details

#_usernameObject



28
29
30
# File 'lib/gta/local_db.rb', line 28

def _username
  config['username'] || config['user']
end

#configObject



16
17
18
# File 'lib/gta/local_db.rb', line 16

def config
  @config ||= parsed_config[env]
end

#databaseObject



36
37
38
# File 'lib/gta/local_db.rb', line 36

def database
  config['database'] ? " -d #{config['database']}" : ''
end

#file_contentsObject



20
21
22
# File 'lib/gta/local_db.rb', line 20

def file_contents
  File.read(database_config_path)
end

#load(backup_path) ⇒ Object



12
13
14
# File 'lib/gta/local_db.rb', line 12

def load(backup_path)
  sh("pg_restore --verbose --clean --no-acl --no-owner -h localhost#{username}#{database} #{backup_path}")
end

#parsed_configObject



24
25
26
# File 'lib/gta/local_db.rb', line 24

def parsed_config
  database_config_path.match(/yml$/) ? YAML.load(file_contents) : JSON.parse(file_contents)
end

#usernameObject



32
33
34
# File 'lib/gta/local_db.rb', line 32

def username
  _username ? " -U #{_username}" : ''
end