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

ACCESS_DENIED_ERROR =
1045

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ MySQLDatabaseTasks

Returns a new instance of MySQLDatabaseTasks.


8
9
10
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 8

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#charsetObject


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

def charset
  connection.charset
end

#collationObject


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

def collation
  connection.collation
end

#createObject


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

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.message
    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


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

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

#purgeObject


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

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

#structure_dump(filename) ⇒ Object


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

def structure_dump(filename)
  args = prepare_command_options
  args.concat(["--result-file", "#{filename}"])
  args.concat(["--no-data"])
  args.concat(["--routines"])
  args.concat(["#{configuration['database']}"])

  run_cmd('mysqldump', args, 'dumping')
end

#structure_load(filename) ⇒ Object


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

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

  run_cmd('mysql', args, 'loading')
end