123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Tasks::MySQLDatabaseTasks

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/tasks/mysql_database_tasks.rb

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(db_config) ⇒ MySQLDatabaseTasks

[ GitHub ]

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

def initialize(db_config)
  @db_config = db_config
  @configuration_hash = db_config.configuration_hash
end

Class Attribute Details

.using_database_configurations?Boolean (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 6

def self.using_database_configurations?
  true
end

Instance Attribute Details

#configuration_hash (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 69

attr_reader :db_config, :configuration_hash

#db_config (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 69

attr_reader :db_config, :configuration_hash

Instance Method Details

#charset

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 32

def charset
  connection.charset
end

#collation

[ GitHub ]

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

def collation
  connection.collation
end

#configuration_hash_without_database (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 79

def configuration_hash_without_database
  configuration_hash.merge(database: nil)
end

#connection (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 71

def connection
  ActiveRecord::Base.lease_connection
end

#create

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 15

def create
  establish_connection(configuration_hash_without_database)
  connection.create_database(db_config.database, creation_options)
  establish_connection
end

#creation_options (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 83

def creation_options
  Hash.new.tap do |options|
    options[:charset]     = configuration_hash[:encoding]   if configuration_hash.include?(:encoding)
    options[:collation]   = configuration_hash[:collation]  if configuration_hash.include?(:collation)
  end
end

#drop

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 21

def drop
  establish_connection
  connection.drop_database(db_config.database)
end

#establish_connection(config = db_config) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 75

def establish_connection(config = db_config)
  ActiveRecord::Base.establish_connection(config)
end

#prepare_command_options (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 90

def prepare_command_options
  args = {
    host:      "--host",
    port:      "--port",
    socket:    "--socket",
    username:  "--user",
    password:  "--password",
    encoding:  "--default-character-set",
    sslca:     "--ssl-ca",
    sslcert:   "--ssl-cert",
    sslcapath: "--ssl-capath",
    sslcipher: "--ssl-cipher",
    sslkey:    "--ssl-key",
    ssl_mode:  "--ssl-mode"
  }.filter_map { |opt, arg| "#{arg}=#{configuration_hash[opt]}" if configuration_hash[opt] }

  args
end

#purge

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 26

def purge
  establish_connection(configuration_hash_without_database)
  connection.recreate_database(db_config.database, creation_options)
  establish_connection
end

#run_cmd(cmd, args, action) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 109

def run_cmd(cmd, args, action)
  fail run_cmd_error(cmd, args, action) unless Kernel.system(cmd, *args)
end

#run_cmd_error(cmd, args, action) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 113

def run_cmd_error(cmd, args, action)
  msg = +"failed to execute: `#{cmd}`\n"
  msg << "Please check the output above for any errors and make sure that `#{cmd}` is installed in your PATH and has proper permissions.\n\n"
  msg
end

#structure_dump(filename, extra_flags)

[ GitHub ]

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

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?
    ignore_tables = connection.data_sources.select { |table| ignore_tables.any? { |pattern| pattern === table } }
    args += ignore_tables.map { |table| "--ignore-table=#{db_config.database}.#{table}" }
  end

  args.concat([db_config.database.to_s])
  args.unshift(*extra_flags) if extra_flags

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

#structure_load(filename, extra_flags)

[ GitHub ]

  
# File 'activerecord/lib/active_record/tasks/mysql_database_tasks.rb', line 59

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", db_config.database.to_s])
  args.unshift(*extra_flags) if extra_flags

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