Class: RailsDbDumpRestore::Database

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

Instance Method Summary collapse

Instance Method Details

#dumpObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails_db_dump_restore/database.rb', line 3

def dump
  system "mkdir -p $(dirname '#{dumpfile}')"
  path = "#{Rails.root}/#{dumpfile}"
  case ActiveRecord::Base.connection_config[:adapter]
  when "postgresql"
    args = "--clean --no-owner --no-privileges"

    run """
      PGPASSWORD='#{password}'
      pg_dump #{args}
      --host='#{host}'
      --username='#{username}'
      --dbname='#{database}'
      --file='#{path}'
    """
  when "mysql2"
    run """
      MYSQL_PWD='#{password}'
      mysqldump
      --host='#{host}'
      --user='#{username}'
      '#{database}' > '#{path}'
    """
  end

  puts "#{Rails.env.to_s} database dumped to #{dumpfile}"
end

#restoreObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rails_db_dump_restore/database.rb', line 31

def restore
  path = "#{Rails.root}/#{dumpfile}"
  case ActiveRecord::Base.connection_config[:adapter]
  when "postgresql"
    run """
      PGPASSWORD=#{password}
      psql
      --host=#{host}
      --username=#{username}
      --dbname=#{database}
      --file=#{path}
    """
  when "mysql2"
    run """
      MYSQL_PWD=#{password}
      mysql
      --host=#{host}
      --user=#{username}
      #{database} < #{path}
    """
  end

  puts "#{Rails.env.to_s} database replaced with contents of #{dumpfile}"
end