Top Level Namespace

Defined Under Namespace

Modules: Mina

Instance Method Summary collapse

Instance Method Details

#backup_tables(table_names) ⇒ Object



49
50
51
52
53
# File 'lib/mina/db_sync/tasks.rb', line 49

def backup_tables(table_names)
  db_credentials
  command %{pg_dump --format=c --no-owner #{tweak_tables(table_names)} $DATABASE > #{db_file}}
  command "gzip -f #{db_file}"
end

#db_archiveObject



61
62
63
64
# File 'lib/mina/db_sync/tasks.rb', line 61

def db_archive
  return 'dump.db.gz' if fetch(:local_run)
  "#{fetch(:shared_path)}/dump.db.gz"
end

#db_credentials(local: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mina/db_sync/tasks.rb', line 35

def db_credentials(local: false)
  if fetch(:local_run)
    db = 'config/database.yml'
    db_type = 'development'
  else
    db = fetch(:shared_path) + '/config/database.yml'
    db_type = 'production'
  end

  %w(username password database).each do |val|
    command %{#{val.upcase}=$(ruby -ryaml -e 'puts YAML.load_file("#{db}")["#{db_type}"]["#{val}"]')}
  end
end

#db_fileObject



66
67
68
69
# File 'lib/mina/db_sync/tasks.rb', line 66

def db_file
  return 'dump.db' if fetch(:local_run)
  "#{fetch(:shared_path)}/dump.db"
end

#restore_tablesObject



55
56
57
58
59
# File 'lib/mina/db_sync/tasks.rb', line 55

def restore_tables
  db_credentials
  command %{gunzip -f #{db_archive}}
  command %{pg_restore --no-owner --username=$USERNAME --clean --dbname=$DATABASE #{db_file}}
end

#tweak_tables(table_names) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/mina/db_sync/tasks.rb', line 71

def tweak_tables(table_names)
  if table_names.to_a.empty?
    STDOUT.write "Tables to get: "
    tables = STDIN.gets.strip
  else
    tables = table_names.to_a.join(' ')
  end

  tables.split(' ').map { |table| "--table=#{table}" }.join(' ')
end