Class: Dataset::Database::Mysql

Inherits:
Base
  • Object
show all
Defined in:
lib/dataset/database/mysql.rb

Overview

The interface to a mySQL database, this will capture by creating a dump file and restore by loading one of the same.

Instance Method Summary collapse

Methods inherited from Base

#clear, #record_heirarchy, #record_meta

Constructor Details

#initialize(database_spec, storage_path) ⇒ Mysql

Returns a new instance of Mysql.



8
9
10
11
12
13
14
# File 'lib/dataset/database/mysql.rb', line 8

def initialize(database_spec, storage_path)
  @database = database_spec[:database]
  @username = database_spec[:username]
  @password = database_spec[:password]
  @storage_path = storage_path
  FileUtils.mkdir_p(@storage_path)
end

Instance Method Details

#capture(datasets) ⇒ Object



16
17
18
19
# File 'lib/dataset/database/mysql.rb', line 16

def capture(datasets)
  return if datasets.nil? || datasets.empty?
  `mysqldump -u #{@username} --password=#{@password} --compact --extended-insert --no-create-db --add-drop-table --quick --quote-names #{@database} > #{storage_path(datasets)}`
end

#restore(datasets) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/dataset/database/mysql.rb', line 21

def restore(datasets)
  store = storage_path(datasets)
  if File.file?(store)
    `mysql -u #{@username} --password=#{@password} --database=#{@database} < #{store}`
    true
  end
end

#storage_path(datasets) ⇒ Object



29
30
31
# File 'lib/dataset/database/mysql.rb', line 29

def storage_path(datasets)
  "#{@storage_path}/#{datasets.collect {|c| c.__id__}.join('_')}.sql"
end