Class: MysqlAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/adapters/mysql_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MysqlAdapter

Returns a new instance of MysqlAdapter.



2
3
4
# File 'lib/adapters/mysql_adapter.rb', line 2

def initialize(config)
  @config = config
end

Instance Method Details

#copy(new_db_config) ⇒ Object



16
17
18
19
20
# File 'lib/adapters/mysql_adapter.rb', line 16

def copy(new_db_config)
  ensure_db_exists
  ensure_db_exists(new_db_config['database'])
  Kernel.` "mysqldump -h #{@config['host']} -u #{@config['username']} #{'-p' + @config['password'] if @config['password']} #{@config['database']} | mysql -h #{new_db_config['host']} -u #{new_db_config['username']} #{'-p' + new_db_config['password'] if new_db_config['password']} #{new_db_config['database']}"
end

#ensure_db_exists(name = @config['database']) ⇒ Object



22
23
24
# File 'lib/adapters/mysql_adapter.rb', line 22

def ensure_db_exists(name = @config['database'])
  `echo "CREATE DATABASE IF NOT EXISTS #{name}" | mysql -h #{@config['host']} -u #{@config['username']} #{'-p' + @config['password'] if @config['password']}`
end

#exportObject



6
7
8
9
# File 'lib/adapters/mysql_adapter.rb', line 6

def export
  ensure_db_exists
  Kernel.` "mysqldump -h #{@config['host']} -u #{@config['username']} #{'-p' + @config['password'] if @config['password']} #{@config['database']}"
end

#import(sql) ⇒ Object



11
12
13
14
# File 'lib/adapters/mysql_adapter.rb', line 11

def import(sql)
  ensure_db_exists
  Kernel.` "echo \"#{sql}\" | mysql -h #{@config['host']} -u #{@config['username']} #{'-p' + @config['password'] if @config['password']} #{@config['database']}"
end