Class: GTA::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/gta/db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gta_config_path = nil, local_env = nil) ⇒ DB

Returns a new instance of DB.



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

def initialize(gta_config_path=nil, local_env=nil)
  @gta_config_path = gta_config_path
  @local_env = local_env || 'development'
end

Instance Attribute Details

#gta_config_pathObject (readonly)

Returns the value of attribute gta_config_path.



3
4
5
# File 'lib/gta/db.rb', line 3

def gta_config_path
  @gta_config_path
end

#local_envObject (readonly)

Returns the value of attribute local_env.



3
4
5
# File 'lib/gta/db.rb', line 3

def local_env
  @local_env
end

Instance Method Details

#db(stage_name) ⇒ Object



52
53
54
# File 'lib/gta/db.rb', line 52

def db(stage_name)
  HerokuDB.new(manager.app_name, stage_name)
end

#fetch(stage_name = nil) ⇒ Object



18
19
20
21
# File 'lib/gta/db.rb', line 18

def fetch(stage_name=nil)
  stage = stage_or_final(stage_name)
  db(stage.name).fetch
end

#load(stage_name = nil) ⇒ Object



23
24
25
26
27
# File 'lib/gta/db.rb', line 23

def load(stage_name=nil)
  stage = stage_or_final(stage_name)
  heroku_db = db(stage.name)
  local_db.load(heroku_db.file_name)
end

#local_dbObject



14
15
16
# File 'lib/gta/db.rb', line 14

def local_db
  @local_db ||= LocalDB.new(local_env, manager.database_config_path)
end

#managerObject



10
11
12
# File 'lib/gta/db.rb', line 10

def manager
  @manager ||= Manager.new(gta_config_path)
end

#pull(stage_name = nil) ⇒ Object



29
30
31
32
33
# File 'lib/gta/db.rb', line 29

def pull(stage_name=nil)
  stage = stage_or_final(stage_name)
  fetch(stage.name)
  self.load(stage.name)
end

#restore(destination_name, source_name = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gta/db.rb', line 35

def restore(destination_name, source_name=nil)
  source = stage_or_final(source_name)
  destination = manager.stage!(destination_name)

  raise "cannot restore #{destination.name}" unless destination.restorable?

  source_db = db(source.name)
  source_db.backup

  destination_db = db(destination.name)
  destination_db.restore_from(source_db.url)
end

#stage_or_final(stage_name) ⇒ Object



48
49
50
# File 'lib/gta/db.rb', line 48

def stage_or_final(stage_name)
  manager.stage(stage_name) || manager.final_stage
end