Class: ActiveRecord::Tasks::MySQLDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 10

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#charsetObject



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

def charset
  connection.charset
end

#collationObject



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

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
# File 'activerecord/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 "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



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

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

#purgeObject



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

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

#structure_dump(filename) ⇒ Object



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

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



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

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