Class: ActiveRecord::Tasks::MySQLDatabaseTasks

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

Overview

:nodoc:

Constant Summary collapse

ER_DB_CREATE_EXISTS =
1007

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



36
37
38
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 36

def charset
  connection.charset
end

#collationObject



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

def collation
  connection.collation
end

#createObject



14
15
16
17
18
19
20
21
22
23
24
# 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 connection.error_number(error.cause) == ER_DB_CREATE_EXISTS
    raise DatabaseAlreadyExists
  else
    raise
  end
end

#dropObject



26
27
28
29
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 26

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

#purgeObject



31
32
33
34
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 31

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

#structure_dump(filename, extra_flags) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 44

def structure_dump(filename, extra_flags)
  args = prepare_command_options
  args.concat(["--result-file", "#{filename}"])
  args.concat(["--no-data"])
  args.concat(["--routines"])
  args.concat(["--skip-comments"])

  ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
  if ignore_tables.any?
    args += ignore_tables.map { |table| "--ignore-table=#{configuration['database']}.#{table}" }
  end

  args.concat(["#{configuration['database']}"])
  args.unshift(*extra_flags) if extra_flags

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

#structure_load(filename, extra_flags) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 62

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

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