utf8mb4rails 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +88 -80
- data/Rakefile +3 -3
- data/bin/console +3 -3
- data/lib/utf8mb4rails.rb +3 -1
- data/lib/utf8mb4rails/migrator/column_info.rb +9 -5
- data/lib/utf8mb4rails/migrator/db_inspector.rb +5 -0
- data/lib/utf8mb4rails/migrator/runner.rb +24 -10
- data/lib/utf8mb4rails/version.rb +1 -1
- data/utf8mb4rails.gemspec +8 -4
- metadata +11 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 56b87fc42083d61b0f0c48437897aef8e01241f672d0b7a2c238952d89a57807
|
4
|
+
data.tar.gz: b927e865f13370247f95ac65bcbb92a95860357b6632740397fed8972721b122
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1919067c8d0592d0b40447c1be880245eca1310c34760749a0a9dfd5bb9ea23360ee985be44fc78d51b9506226fdac897ae50a3910b986f06b63342e6b25f9b9
|
7
|
+
data.tar.gz: c65d92bd56b807ffc7d83e3c557a4ceaccddc27c1500d70b7410401416c6742ad8e85955aca9912789ccbae7794f0d0159e40aee06c6462b32b5f39e034f097b
|
data/Gemfile.lock
CHANGED
@@ -1,98 +1,103 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
utf8mb4rails (0.
|
5
|
-
departure (~>
|
4
|
+
utf8mb4rails (0.2.0)
|
5
|
+
departure (~> 6.1)
|
6
6
|
mysql2 (~> 0.4.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
|
12
|
-
actionpack (=
|
13
|
-
|
14
|
-
|
11
|
+
actioncable (5.1.7)
|
12
|
+
actionpack (= 5.1.7)
|
13
|
+
nio4r (~> 2.0)
|
14
|
+
websocket-driver (~> 0.6.1)
|
15
|
+
actionmailer (5.1.7)
|
16
|
+
actionpack (= 5.1.7)
|
17
|
+
actionview (= 5.1.7)
|
18
|
+
activejob (= 5.1.7)
|
15
19
|
mail (~> 2.5, >= 2.5.4)
|
16
|
-
rails-dom-testing (~>
|
17
|
-
actionpack (
|
18
|
-
actionview (=
|
19
|
-
activesupport (=
|
20
|
-
rack (~>
|
21
|
-
rack-test (
|
22
|
-
rails-dom-testing (~>
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
actionpack (5.1.7)
|
22
|
+
actionview (= 5.1.7)
|
23
|
+
activesupport (= 5.1.7)
|
24
|
+
rack (~> 2.0)
|
25
|
+
rack-test (>= 0.6.3)
|
26
|
+
rails-dom-testing (~> 2.0)
|
23
27
|
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
24
|
-
actionview (
|
25
|
-
activesupport (=
|
28
|
+
actionview (5.1.7)
|
29
|
+
activesupport (= 5.1.7)
|
26
30
|
builder (~> 3.1)
|
27
|
-
|
28
|
-
rails-dom-testing (~>
|
31
|
+
erubi (~> 1.4)
|
32
|
+
rails-dom-testing (~> 2.0)
|
29
33
|
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
30
|
-
activejob (
|
31
|
-
activesupport (=
|
32
|
-
globalid (>= 0.3.
|
33
|
-
activemodel (
|
34
|
-
activesupport (=
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
i18n (
|
34
|
+
activejob (5.1.7)
|
35
|
+
activesupport (= 5.1.7)
|
36
|
+
globalid (>= 0.3.6)
|
37
|
+
activemodel (5.1.7)
|
38
|
+
activesupport (= 5.1.7)
|
39
|
+
activerecord (5.1.7)
|
40
|
+
activemodel (= 5.1.7)
|
41
|
+
activesupport (= 5.1.7)
|
42
|
+
arel (~> 8.0)
|
43
|
+
activesupport (5.1.7)
|
44
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
45
|
+
i18n (>= 0.7, < 2)
|
42
46
|
minitest (~> 5.1)
|
43
|
-
thread_safe (~> 0.3, >= 0.3.4)
|
44
47
|
tzinfo (~> 1.1)
|
45
|
-
arel (
|
46
|
-
builder (3.2.
|
47
|
-
concurrent-ruby (1.
|
48
|
-
|
48
|
+
arel (8.0.0)
|
49
|
+
builder (3.2.4)
|
50
|
+
concurrent-ruby (1.1.6)
|
51
|
+
crass (1.0.6)
|
52
|
+
departure (6.1.0)
|
49
53
|
mysql2 (~> 0.4.0)
|
50
|
-
rails (~>
|
54
|
+
rails (~> 5.1.0)
|
51
55
|
diff-lcs (1.3)
|
52
|
-
|
53
|
-
globalid (0.4.
|
56
|
+
erubi (1.9.0)
|
57
|
+
globalid (0.4.2)
|
54
58
|
activesupport (>= 4.2.0)
|
55
|
-
i18n (
|
56
|
-
|
59
|
+
i18n (1.8.2)
|
60
|
+
concurrent-ruby (~> 1.0)
|
61
|
+
loofah (2.5.0)
|
62
|
+
crass (~> 1.0.2)
|
57
63
|
nokogiri (>= 1.5.9)
|
58
|
-
mail (2.
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
nokogiri (1.
|
67
|
-
mini_portile2 (~> 2.
|
68
|
-
rack (
|
69
|
-
rack-test (
|
70
|
-
rack (>= 1.0)
|
71
|
-
rails (
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
activesupport (= 4.2.9)
|
64
|
+
mail (2.7.1)
|
65
|
+
mini_mime (>= 0.1.1)
|
66
|
+
method_source (1.0.0)
|
67
|
+
mini_mime (1.0.2)
|
68
|
+
mini_portile2 (2.4.0)
|
69
|
+
minitest (5.14.0)
|
70
|
+
mysql2 (0.4.10)
|
71
|
+
nio4r (2.5.2)
|
72
|
+
nokogiri (1.10.9)
|
73
|
+
mini_portile2 (~> 2.4.0)
|
74
|
+
rack (2.2.2)
|
75
|
+
rack-test (1.1.0)
|
76
|
+
rack (>= 1.0, < 3)
|
77
|
+
rails (5.1.7)
|
78
|
+
actioncable (= 5.1.7)
|
79
|
+
actionmailer (= 5.1.7)
|
80
|
+
actionpack (= 5.1.7)
|
81
|
+
actionview (= 5.1.7)
|
82
|
+
activejob (= 5.1.7)
|
83
|
+
activemodel (= 5.1.7)
|
84
|
+
activerecord (= 5.1.7)
|
85
|
+
activesupport (= 5.1.7)
|
86
|
+
bundler (>= 1.3.0)
|
87
|
+
railties (= 5.1.7)
|
88
|
+
sprockets-rails (>= 2.0.0)
|
89
|
+
rails-dom-testing (2.0.3)
|
90
|
+
activesupport (>= 4.2.0)
|
91
|
+
nokogiri (>= 1.6)
|
92
|
+
rails-html-sanitizer (1.3.0)
|
93
|
+
loofah (~> 2.3)
|
94
|
+
railties (5.1.7)
|
95
|
+
actionpack (= 5.1.7)
|
96
|
+
activesupport (= 5.1.7)
|
97
|
+
method_source
|
93
98
|
rake (>= 0.8.7)
|
94
99
|
thor (>= 0.18.1, < 2.0)
|
95
|
-
rake (
|
100
|
+
rake (13.0.1)
|
96
101
|
rspec (3.6.0)
|
97
102
|
rspec-core (~> 3.6.0)
|
98
103
|
rspec-expectations (~> 3.6.0)
|
@@ -106,26 +111,29 @@ GEM
|
|
106
111
|
diff-lcs (>= 1.2.0, < 2.0)
|
107
112
|
rspec-support (~> 3.6.0)
|
108
113
|
rspec-support (3.6.0)
|
109
|
-
sprockets (
|
114
|
+
sprockets (4.0.0)
|
110
115
|
concurrent-ruby (~> 1.0)
|
111
116
|
rack (> 1, < 3)
|
112
117
|
sprockets-rails (3.2.1)
|
113
118
|
actionpack (>= 4.0)
|
114
119
|
activesupport (>= 4.0)
|
115
120
|
sprockets (>= 3.0.0)
|
116
|
-
thor (0.
|
121
|
+
thor (1.0.1)
|
117
122
|
thread_safe (0.3.6)
|
118
|
-
tzinfo (1.2.
|
123
|
+
tzinfo (1.2.7)
|
119
124
|
thread_safe (~> 0.1)
|
125
|
+
websocket-driver (0.6.5)
|
126
|
+
websocket-extensions (>= 0.1.0)
|
127
|
+
websocket-extensions (0.1.4)
|
120
128
|
|
121
129
|
PLATFORMS
|
122
130
|
ruby
|
123
131
|
|
124
132
|
DEPENDENCIES
|
125
133
|
bundler (~> 1.12)
|
126
|
-
rake (
|
134
|
+
rake (>= 12.3.3)
|
127
135
|
rspec (~> 3.0)
|
128
136
|
utf8mb4rails!
|
129
137
|
|
130
138
|
BUNDLED WITH
|
131
|
-
1.
|
139
|
+
1.17.1
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'utf8mb4rails'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "utf8mb4rails"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start
|
data/lib/utf8mb4rails.rb
CHANGED
@@ -14,6 +14,7 @@ def usage
|
|
14
14
|
puts 'You can also specify the COLLATION (utf8mb4_unicode_520_ci)'
|
15
15
|
end
|
16
16
|
|
17
|
+
# Top Module
|
17
18
|
module Utf8mb4rails
|
18
19
|
extend Rake::DSL
|
19
20
|
|
@@ -21,7 +22,8 @@ module Utf8mb4rails
|
|
21
22
|
desc 'migrates a table[/column] (TABLE, COLUMN env vars) to utf8mb encoding'
|
22
23
|
task utf8mb4: :environment do
|
23
24
|
require 'departure'
|
24
|
-
Departure.configure do |_config
|
25
|
+
Departure.configure do |_config|
|
26
|
+
end
|
25
27
|
|
26
28
|
table = ENV['TABLE']
|
27
29
|
unless table
|
@@ -5,7 +5,7 @@ module Utf8mb4rails
|
|
5
5
|
# received from the database
|
6
6
|
class ColumnInfo
|
7
7
|
TEXT_TYPES = %w(CHAR VARCHAR TINYTEXT TEXT MEDIUMTEXT LONGTEXT).freeze
|
8
|
-
|
8
|
+
attr_reader :info
|
9
9
|
|
10
10
|
# Receives a hash with (:type, :default, :max_length, :charset)
|
11
11
|
def initialize(info)
|
@@ -19,14 +19,18 @@ module Utf8mb4rails
|
|
19
19
|
|
20
20
|
# @return String : the sql part of the new type of the column definition
|
21
21
|
def new_type_for_sql
|
22
|
-
|
23
|
-
info[:
|
22
|
+
info_type = info[:type]
|
23
|
+
return "#{info_type}(#{info[:max_length]})" if info_type =~ /CHAR/
|
24
|
+
|
25
|
+
info_type
|
24
26
|
end
|
25
27
|
|
26
28
|
# @return String : the sql part of the default value of the column definition
|
27
29
|
def default_value_for_sql
|
28
|
-
|
29
|
-
|
30
|
+
info_default = info[:default]
|
31
|
+
return nil unless info_default
|
32
|
+
|
33
|
+
"default '#{info_default}'"
|
30
34
|
end
|
31
35
|
|
32
36
|
# @return Bool : True if the column is of a text type
|
@@ -1,7 +1,9 @@
|
|
1
1
|
module Utf8mb4rails
|
2
2
|
module Migrator
|
3
|
+
# DB introspection (only for columns)
|
3
4
|
class DBInspector
|
4
5
|
# Initializes AR
|
6
|
+
# :reek:UtilityFunction
|
5
7
|
def initialize
|
6
8
|
ActiveRecord::Base.establish_connection(
|
7
9
|
ActiveRecord::Base.connection_config.merge(adapter: 'percona')
|
@@ -10,6 +12,7 @@ module Utf8mb4rails
|
|
10
12
|
|
11
13
|
##
|
12
14
|
# @returns [Array<String>] An array with column names
|
15
|
+
# :reek:UtilityFunction
|
13
16
|
def columns(table)
|
14
17
|
ActiveRecord::Base.connection.columns(table).map(&:name)
|
15
18
|
end
|
@@ -20,6 +23,7 @@ module Utf8mb4rails
|
|
20
23
|
# @param [String] column
|
21
24
|
#
|
22
25
|
# @returns [Hash] { type: String, default: String or nil, charset: String or nil }
|
26
|
+
# :reek:FeatureEnvy
|
23
27
|
def column_info(table, column)
|
24
28
|
sql = "SELECT DATA_TYPE, COLUMN_DEFAULT, CHARACTER_SET_NAME, CHARACTER_MAXIMUM_LENGTH
|
25
29
|
FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '#{table}'
|
@@ -36,6 +40,7 @@ module Utf8mb4rails
|
|
36
40
|
|
37
41
|
private
|
38
42
|
|
43
|
+
# :reek:UtilityFunction
|
39
44
|
def database_name
|
40
45
|
ActiveRecord::Base.connection.current_database
|
41
46
|
end
|
@@ -1,14 +1,19 @@
|
|
1
|
+
require 'English'
|
2
|
+
|
1
3
|
module Utf8mb4rails
|
2
4
|
module Migrator
|
5
|
+
# This class will perform the actual migration, will run sql queries that
|
6
|
+
# alters tables
|
3
7
|
class Runner
|
4
8
|
NEW_COLLATION = ENV.fetch('COLLATION', 'utf8mb4_unicode_520_ci').freeze
|
5
9
|
|
6
|
-
|
10
|
+
attr_reader :inspector
|
7
11
|
|
8
12
|
def initialize
|
9
13
|
@inspector = DBInspector.new
|
10
14
|
end
|
11
15
|
|
16
|
+
# :reek:FeatureEnvy
|
12
17
|
def migrate_column!(table, column)
|
13
18
|
column_info = inspector.column_info(table, column)
|
14
19
|
if column_info.utf8mb4?
|
@@ -22,22 +27,31 @@ module Utf8mb4rails
|
|
22
27
|
ActiveRecord::Base.connection.execute(sql)
|
23
28
|
end
|
24
29
|
|
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
|
-
|
30
30
|
def migrate_table!(table)
|
31
|
+
sql = []
|
31
32
|
inspector.columns(table).each do |column|
|
32
|
-
|
33
|
-
|
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}"
|
34
38
|
end
|
35
|
-
|
39
|
+
sql << "CONVERT TO CHARACTER SET utf8mb4 COLLATE #{NEW_COLLATION}"
|
40
|
+
ActiveRecord::Base.connection.execute("ALTER TABLE `#{table}` #{sql.join(' , ')};")
|
36
41
|
rescue
|
37
42
|
puts "Problems accesing the table #{table}"
|
38
|
-
puts
|
43
|
+
puts $ERROR_INFO
|
39
44
|
exit 1
|
40
45
|
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
|
41
55
|
end
|
42
56
|
end
|
43
57
|
end
|
data/lib/utf8mb4rails/version.rb
CHANGED
data/utf8mb4rails.gemspec
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
|
2
3
|
lib = File.expand_path('../lib', __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'utf8mb4rails/version'
|
@@ -10,19 +11,22 @@ Gem::Specification.new do |spec|
|
|
10
11
|
spec.email = ['joseferper@gmail.com']
|
11
12
|
|
12
13
|
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.
|
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.'
|
14
16
|
spec.homepage = 'https://github.com/magec/utf8mb4rails'
|
15
17
|
spec.license = 'MIT'
|
16
18
|
|
17
|
-
spec.files = `git ls-files -z`.split("\x0").reject
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
18
22
|
spec.bindir = 'exe'
|
19
23
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
24
|
spec.require_paths = ['lib']
|
21
25
|
|
22
|
-
spec.add_runtime_dependency 'departure', '~>
|
26
|
+
spec.add_runtime_dependency 'departure', '~> 6.1'
|
23
27
|
spec.add_runtime_dependency 'mysql2', '~> 0.4.0'
|
24
28
|
|
25
29
|
spec.add_development_dependency 'bundler', '~> 1.12'
|
26
|
-
spec.add_development_dependency 'rake', '
|
30
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
27
31
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utf8mb4rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jose Fernandez (magec)
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: departure
|
@@ -16,20 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
20
|
-
- - ">="
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: 4.0.1
|
19
|
+
version: '6.1'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
26
23
|
requirements:
|
27
24
|
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
30
|
-
- - ">="
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: 4.0.1
|
26
|
+
version: '6.1'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: mysql2
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,16 +56,16 @@ dependencies:
|
|
62
56
|
name: rake
|
63
57
|
requirement: !ruby/object:Gem::Requirement
|
64
58
|
requirements:
|
65
|
-
- - "
|
59
|
+
- - ">="
|
66
60
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
61
|
+
version: 12.3.3
|
68
62
|
type: :development
|
69
63
|
prerelease: false
|
70
64
|
version_requirements: !ruby/object:Gem::Requirement
|
71
65
|
requirements:
|
72
|
-
- - "
|
66
|
+
- - ">="
|
73
67
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
68
|
+
version: 12.3.3
|
75
69
|
- !ruby/object:Gem::Dependency
|
76
70
|
name: rspec
|
77
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,7 +80,8 @@ dependencies:
|
|
86
80
|
- - "~>"
|
87
81
|
- !ruby/object:Gem::Version
|
88
82
|
version: '3.0'
|
89
|
-
description:
|
83
|
+
description: |-
|
84
|
+
Poetry is so XIX century, nowadays we express ourselves using emojis.
|
90
85
|
This gem adds a task to migrate mysql utf8 tables to utf8mb4 in rails projects.
|
91
86
|
email:
|
92
87
|
- joseferper@gmail.com
|
@@ -130,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
130
125
|
version: '0'
|
131
126
|
requirements: []
|
132
127
|
rubyforge_project:
|
133
|
-
rubygems_version: 2.
|
128
|
+
rubygems_version: 2.7.6
|
134
129
|
signing_key:
|
135
130
|
specification_version: 4
|
136
131
|
summary: Adds a task to migrate mysql utf8 tables to utf8mb4 (emojis)
|