Class: ActiveRecord::Tasks::MySQLDatabaseTasks
- Inherits:
-
Object
- Object
- ActiveRecord::Tasks::MySQLDatabaseTasks
- Defined in:
- lib/active_record/tasks/mysql_database_tasks.rb
Overview
:nodoc:
Constant Summary collapse
- ACCESS_DENIED_ERROR =
1045
Instance Method Summary collapse
- #charset ⇒ Object
- #collation ⇒ Object
- #create ⇒ Object
- #drop ⇒ Object
-
#initialize(configuration) ⇒ MySQLDatabaseTasks
constructor
A new instance of MySQLDatabaseTasks.
- #purge ⇒ Object
- #structure_dump(filename) ⇒ Object
- #structure_load(filename) ⇒ Object
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
#charset ⇒ Object
48 49 50 |
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 48 def charset connection.charset end |
#collation ⇒ Object
52 53 54 |
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 52 def collation connection.collation end |
#create ⇒ Object
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 'lib/active_record/tasks/mysql_database_tasks.rb', line 12 def create establish_connection configuration_without_database connection.create_database configuration['database'], establish_connection configuration rescue ActiveRecord::StatementInvalid => error if /database exists/ === error. raise DatabaseAlreadyExists else raise end rescue error_class => error if error.respond_to?(:errno) && error.errno == ACCESS_DENIED_ERROR $stdout.print error. establish_connection root_configuration_without_database connection.create_database configuration['database'], 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}, #{.inspect}" $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding'] end end |
#drop ⇒ Object
38 39 40 41 |
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 38 def drop establish_connection configuration connection.drop_database configuration['database'] end |
#purge ⇒ Object
43 44 45 46 |
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 43 def purge establish_connection configuration connection.recreate_database configuration['database'], end |
#structure_dump(filename) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/active_record/tasks/mysql_database_tasks.rb', line 56 def structure_dump(filename) args = 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 'lib/active_record/tasks/mysql_database_tasks.rb', line 66 def structure_load(filename) args = 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 |