Class: ActiveRecord::Tasks::MySQLDatabaseTasks

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ MySQLDatabaseTasks

Returns a new instance of MySQLDatabaseTasks.



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

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#charsetObject



34
35
36
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 34

def charset
  connection.charset
end

#collationObject



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

def collation
  connection.collation
end

#createObject



12
13
14
15
16
17
18
19
20
21
22
# File '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 error.message.include?("database exists")
    raise DatabaseAlreadyExists
  else
    raise
  end
end

#dropObject



24
25
26
27
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 24

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

#purgeObject



29
30
31
32
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 29

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

#structure_dump(filename, extra_flags) ⇒ Object



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

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



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

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