Class: Dump::DatabaseConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/db_time_machine/dump.rb

Direct Known Subclasses

Mongodb, Mysql2, Postgresql

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabaseConfig

Returns a new instance of DatabaseConfig.



6
7
8
9
10
11
12
13
14
# File 'lib/db_time_machine/dump.rb', line 6

def initialize
  self.cmd        = nil
  self.tables     = DbTimeMachine.configuration.tables
  self.tmp_folder = DbTimeMachine.configuration.tmp_folder
  DatabaseConfig.find_config do |app, d, u, p, h, a, p1|
    self.db, self.user, self.password, self.host, self.port = d, u, p, h, p1
  end
  self.file       = File.join(tmp_folder, "#{db}_#{Time.now.to_i}.sql")
end

Instance Attribute Details

#cmdObject

Returns the value of attribute cmd.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def cmd
  @cmd
end

#dbObject

Returns the value of attribute db.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def db
  @db
end

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def file
  @file
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def host
  @host
end

#passwordObject

Returns the value of attribute password.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def password
  @password
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def port
  @port
end

#tablesObject

Returns the value of attribute tables.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def tables
  @tables
end

#tmp_folderObject

Returns the value of attribute tmp_folder.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def tmp_folder
  @tmp_folder
end

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/db_time_machine/dump.rb', line 4

def user
  @user
end

Class Method Details

.find_adapterObject



37
38
39
40
# File 'lib/db_time_machine/dump.rb', line 37

def self.find_adapter
  return 'mongodb' if File.exists?("#{Rails.root}/config/mongoid.yml")
  ActiveRecord::Base.connection_config[:adapter]
end

.find_config {|Rails.application.class.parent_name.underscore, ActiveRecord::Base.connection_config[:database], ActiveRecord::Base.connection_config[:username], ActiveRecord::Base.connection_config[:password], ActiveRecord::Base.connection_config[:host], ActiveRecord::Base.connection_config[:port]| ... } ⇒ Object

Yields:

  • (Rails.application.class.parent_name.underscore, ActiveRecord::Base.connection_config[:database], ActiveRecord::Base.connection_config[:username], ActiveRecord::Base.connection_config[:password], ActiveRecord::Base.connection_config[:host], ActiveRecord::Base.connection_config[:port])


28
29
30
31
32
33
34
35
# File 'lib/db_time_machine/dump.rb', line 28

def self.find_config
  yield Rails.application.class.parent_name.underscore,
    ActiveRecord::Base.connection_config[:database],
    ActiveRecord::Base.connection_config[:username],
    ActiveRecord::Base.connection_config[:password],
    ActiveRecord::Base.connection_config[:host],
    ActiveRecord::Base.connection_config[:port]
end

Instance Method Details

#start_dumpObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/db_time_machine/dump.rb', line 16

def start_dump
  puts "exporting the latest dump to #{file}"
  inprocess = File.join(tmp_folder, "dumping_data")
  `touch #{inprocess};#{cmd};rm #{inprocess}`
  while File.exist?('#{inprocess}')
    puts "database dump is in progress."
    puts "sleeping"
    sleep(0.5)
  end
  puts "database dump is complete."
end