Class: ActiveRecord::Tasks::MySQLDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/tasks/mysql_database_tasks.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_CHARSET =
ENV['CHARSET']   || 'utf8'
DEFAULT_COLLATION =
ENV['COLLATION'] || 'utf8_unicode_ci'
ACCESS_DENIED_ERROR =
1045

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ MySQLDatabaseTasks

Returns a new instance of MySQLDatabaseTasks.



10
11
12
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 10

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#charsetObject



50
51
52
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 50

def charset
  connection.charset
end

#collationObject



54
55
56
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 54

def collation
  connection.collation
end

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 14

def create
  establish_connection configuration_without_database
  connection.create_database configuration['database'], creation_options
  establish_connection configuration
rescue ActiveRecord::StatementInvalid => error
  if /database exists/ === error.message
    raise DatabaseAlreadyExists
  else
    raise
  end
rescue error_class => error
  if error.respond_to?(:errno) && error.errno == ACCESS_DENIED_ERROR
    $stdout.print error.error
    establish_connection root_configuration_without_database
    connection.create_database configuration['database'], creation_options
    if configuration['username'] != 'root'
      connection.execute grant_statement.gsub(/\s+/, ' ').strip
    end
    establish_connection configuration
  else
    $stderr.puts error.inspect
    $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}"
    $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding']
  end
end

#dropObject



40
41
42
43
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 40

def drop
  establish_connection configuration
  connection.drop_database configuration['database']
end

#purgeObject



45
46
47
48
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 45

def purge
  establish_connection configuration
  connection.recreate_database configuration['database'], creation_options
end

#structure_dump(filename) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 58

def structure_dump(filename)
  args = prepare_command_options('mysqldump')
  args.concat(["--result-file", "#{filename}"])
  args.concat(["--no-data"])
  args.concat(["#{configuration['database']}"])
  unless Kernel.system(*args)
    $stderr.puts "Could not dump the database structure. "\
                 "Make sure `mysqldump` is in your PATH and check the command output for warnings."
  end
end

#structure_load(filename) ⇒ Object



69
70
71
72
73
74
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 69

def structure_load(filename)
  args = prepare_command_options('mysql')
  args.concat(['--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
  args.concat(["--database", "#{configuration['database']}"])
  Kernel.system(*args)
end