Skip to content

Commit 4c542f5

Browse files
committed
Bump version
1 parent 4db932a commit 4c542f5

File tree

8 files changed

+55
-26
lines changed

8 files changed

+55
-26
lines changed

Rakefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require "bundler/gem_tasks"
2-
require "rspec/core/rake_task"
1+
require 'bundler/gem_tasks'
2+
require 'rspec/core/rake_task'
33

44
RSpec::Core::RakeTask.new(:spec)
55

6-
task :default => :spec
6+
task default: :spec

bin/console

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

3-
require "bundler/setup"
4-
require "utf8mb4rails"
3+
require 'bundler/setup'
4+
require 'utf8mb4rails'
55

66
# You can add fixtures and/or initialization code here to make experimenting
77
# with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "utf8mb4rails"
1010
# require "pry"
1111
# Pry.start
1212

13-
require "irb"
13+
require 'irb'
1414
IRB.start

lib/utf8mb4rails.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ def usage
1414
puts 'You can also specify the COLLATION (utf8mb4_unicode_520_ci)'
1515
end
1616

17+
# Top Module
1718
module Utf8mb4rails
1819
extend Rake::DSL
1920

2021
namespace :db do
2122
desc 'migrates a table[/column] (TABLE, COLUMN env vars) to utf8mb encoding'
2223
task utf8mb4: :environment do
2324
require 'departure'
24-
Departure.configure do |_config|; end
25+
Departure.configure do |_config|
26+
end
2527

2628
table = ENV['TABLE']
2729
unless table

lib/utf8mb4rails/migrator/column_info.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Migrator
55
# received from the database
66
class ColumnInfo
77
TEXT_TYPES = %w(CHAR VARCHAR TINYTEXT TEXT MEDIUMTEXT LONGTEXT).freeze
8-
attr_accessor :info
8+
attr_reader :info
99

1010
# Receives a hash with (:type, :default, :max_length, :charset)
1111
def initialize(info)
@@ -19,14 +19,18 @@ def utf8mb4?
1919

2020
# @return String : the sql part of the new type of the column definition
2121
def new_type_for_sql
22-
return "#{info[:type]}(#{info[:max_length]})" if info[:type] =~ /CHAR/
23-
info[:type]
22+
info_type = info[:type]
23+
return "#{info_type}(#{info[:max_length]})" if info_type =~ /CHAR/
24+
25+
info_type
2426
end
2527

2628
# @return String : the sql part of the default value of the column definition
2729
def default_value_for_sql
28-
return nil unless info[:default]
29-
"default '#{info[:default]}'"
30+
info_default = info[:default]
31+
return nil unless info_default
32+
33+
"default '#{info_default}'"
3034
end
3135

3236
# @return Bool : True if the column is of a text type

lib/utf8mb4rails/migrator/db_inspector.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module Utf8mb4rails
22
module Migrator
3+
# DB introspection (only for columns)
34
class DBInspector
45
# Initializes AR
6+
# :reek:UtilityFunction
57
def initialize
68
ActiveRecord::Base.establish_connection(
79
ActiveRecord::Base.connection_config.merge(adapter: 'percona')
@@ -10,6 +12,7 @@ def initialize
1012

1113
##
1214
# @returns [Array<String>] An array with column names
15+
# :reek:UtilityFunction
1316
def columns(table)
1417
ActiveRecord::Base.connection.columns(table).map(&:name)
1518
end
@@ -20,6 +23,7 @@ def columns(table)
2023
# @param [String] column
2124
#
2225
# @returns [Hash] { type: String, default: String or nil, charset: String or nil }
26+
# :reek:FeatureEnvy
2327
def column_info(table, column)
2428
sql = "SELECT DATA_TYPE, COLUMN_DEFAULT, CHARACTER_SET_NAME, CHARACTER_MAXIMUM_LENGTH
2529
FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '#{table}'
@@ -36,6 +40,7 @@ def column_info(table, column)
3640

3741
private
3842

43+
# :reek:UtilityFunction
3944
def database_name
4045
ActiveRecord::Base.connection.current_database
4146
end

lib/utf8mb4rails/migrator/runner.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
require 'English'
2+
13
module Utf8mb4rails
24
module Migrator
5+
# This class will perform the actual migration, will run sql queries that
6+
# alters tables
37
class Runner
48
NEW_COLLATION = ENV.fetch('COLLATION', 'utf8mb4_unicode_520_ci').freeze
59

6-
attr_accessor :inspector
10+
attr_reader :inspector
711

812
def initialize
913
@inspector = DBInspector.new
1014
end
1115

16+
# :reek:FeatureEnvy
1217
def migrate_column!(table, column)
1318
column_info = inspector.column_info(table, column)
1419
if column_info.utf8mb4?
@@ -22,22 +27,31 @@ def migrate_column!(table, column)
2227
ActiveRecord::Base.connection.execute(sql)
2328
end
2429

25-
def migrate_table_schema!(table)
26-
sql = "ALTER TABLE `#{table}` CONVERT TO CHARACTER SET utf8mb4 COLLATE #{NEW_COLLATION}"
27-
ActiveRecord::Base.connection.execute(sql)
28-
end
29-
3030
def migrate_table!(table)
31+
sql = []
3132
inspector.columns(table).each do |column|
32-
puts " migrating #{table}:#{column}"
33-
migrate_column!(table, column)
33+
column_info = inspector.column_info(table, column)
34+
next if column_info.utf8mb4? || !column_info.text_column? || skipped_column?(column)
35+
sql << "DROP COLUMN \`#{column}`"
36+
sql << "ADD COLUMN \`#{column}`\ #{column_info.new_type_for_sql} \
37+
CHARACTER SET utf8mb4 COLLATE #{NEW_COLLATION} #{column_info.default_value_for_sql}"
3438
end
35-
migrate_table_schema!(table)
39+
sql << "CONVERT TO CHARACTER SET utf8mb4 COLLATE #{NEW_COLLATION}"
40+
ActiveRecord::Base.connection.execute("ALTER TABLE `#{table}` #{sql.join(' , ')};")
3641
rescue
3742
puts "Problems accesing the table #{table}"
38-
puts $!
43+
puts $ERROR_INFO
3944
exit 1
4045
end
46+
47+
private
48+
49+
# :reek:UtilityFunction
50+
def skipped_column?(column_name)
51+
return false unless ENV.key? 'SKIP_COLUMNS'
52+
skipped_columns = ENV['SKIP_COLUMNS'].join(',')
53+
skipped_columns.include(column_name)
54+
end
4155
end
4256
end
4357
end

lib/utf8mb4rails/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Utf8mb4rails
2-
VERSION = '0.1.1'
2+
VERSION = '0.2.0'.freeze
33
end

utf8mb4rails.gemspec

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding: utf-8
2+
23
lib = File.expand_path('../lib', __FILE__)
34
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
45
require 'utf8mb4rails/version'
@@ -10,16 +11,19 @@ Gem::Specification.new do |spec|
1011
spec.email = ['[email protected]']
1112

1213
spec.summary = 'Adds a task to migrate mysql utf8 tables to utf8mb4 (emojis)'
13-
spec.description = 'Poetry is so XIX century, nowadays we express ourselves using emojis. This gem adds a task to migrate mysql utf8 tables to utf8mb4 in rails projects.'
14+
spec.description = 'Poetry is so XIX century, nowadays we express ourselves using emojis.
15+
This gem adds a task to migrate mysql utf8 tables to utf8mb4 in rails projects.'
1416
spec.homepage = 'https://github.com/magec/utf8mb4rails'
1517
spec.license = 'MIT'
1618

17-
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
20+
f.match(%r{^(test|spec|features)/})
21+
end
1822
spec.bindir = 'exe'
1923
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
2024
spec.require_paths = ['lib']
2125

22-
spec.add_runtime_dependency 'departure', '~> 4.0', '>= 4.0.1'
26+
spec.add_runtime_dependency 'departure', '~> 6.0'
2327
spec.add_runtime_dependency 'mysql2', '~> 0.4.0'
2428

2529
spec.add_development_dependency 'bundler', '~> 1.12'

0 commit comments

Comments
 (0)