Class: Utf8mb4rails::Migrator::Runner
- Inherits:
-
Object
- Object
- Utf8mb4rails::Migrator::Runner
- Defined in:
- lib/utf8mb4rails/migrator/runner.rb
Constant Summary collapse
- NEW_COLLATION =
ENV.fetch('COLLATION', 'utf8mb4_unicode_520_ci').freeze
Instance Attribute Summary collapse
-
#inspector ⇒ Object
Returns the value of attribute inspector.
Instance Method Summary collapse
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
- #migrate_column!(table, column) ⇒ Object
- #migrate_table!(table) ⇒ Object
- #migrate_table_schema!(table) ⇒ Object
Constructor Details
#initialize ⇒ Runner
Returns a new instance of Runner.
8 9 10 |
# File 'lib/utf8mb4rails/migrator/runner.rb', line 8 def initialize @inspector = DBInspector.new end |
Instance Attribute Details
#inspector ⇒ Object
Returns the value of attribute inspector.
6 7 8 |
# File 'lib/utf8mb4rails/migrator/runner.rb', line 6 def inspector @inspector end |
Instance Method Details
#migrate_column!(table, column) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/utf8mb4rails/migrator/runner.rb', line 12 def migrate_column!(table, column) column_info = inspector.column_info(table, column) if column_info.utf8mb4? puts " Skipping column #{column} (already in utf8mb4)'" return end return unless column_info.text_column? sql = "ALTER TABLE `#{table}` DROP COLUMN \`#{column}`\ , ADD COLUMN \`#{column}`\ #{column_info.new_type_for_sql} CHARACTER SET utf8mb4 COLLATE #{NEW_COLLATION} #{column_info.default_value_for_sql}" ActiveRecord::Base.connection.execute(sql) end |
#migrate_table!(table) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/utf8mb4rails/migrator/runner.rb', line 30 def migrate_table!(table) inspector.columns(table).each do |column| puts " migrating #{table}:#{column}" migrate_column!(table, column) end migrate_table_schema!(table) rescue puts "Problems accesing the table #{table}" puts $! exit 1 end |
#migrate_table_schema!(table) ⇒ Object
25 26 27 28 |
# File 'lib/utf8mb4rails/migrator/runner.rb', line 25 def migrate_table_schema!(table) sql = "ALTER TABLE `#{table}` CONVERT TO CHARACTER SET utf8mb4 COLLATE #{NEW_COLLATION}" ActiveRecord::Base.connection.execute(sql) end |