Skip to content

Commit

Permalink
Fix MySQL 5.6 bug re: item_type length
Browse files Browse the repository at this point in the history
[Fixes #651]
  • Loading branch information
jaredbeck committed May 26, 2016
1 parent 17c8f34 commit b562147
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 5.?.? (Unreleased)
## 5.1.1 (Unreleased)

### Breaking Changes

Expand All @@ -10,7 +10,8 @@

### Fixed

- None
- [#651](https://github.com/airblade/paper_trail/issues/651) -
Bug with installing PT on MySQL <= 5.6

## 5.1.0 (2016-05-20)

Expand Down
18 changes: 16 additions & 2 deletions lib/generators/paper_trail/templates/create_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreateVersions < ActiveRecord::Migration

def change
create_table :versions, versions_table_options do |t|
t.string :item_type, null: false
t.string :item_type, item_type_options
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
Expand All @@ -43,6 +43,20 @@ def change

private

def item_type_options
opt = { null: false }
if mysql?
# MySQL 5.6 utf8mb4 limit is 191 chars for keys used in indexes.
# See https://github.com/airblade/paper_trail/issues/651
opt[:limit] = 191
end
opt
end

def mysql?
MYSQL_ADAPTERS.include?(connection.class.name)
end

# Even modern versions of MySQL still use `latin1` as the default character
# encoding. Many users are not aware of this, and run into trouble when they
# try to use PaperTrail in apps that otherwise tend to use UTF-8. Postgres, by
Expand All @@ -59,7 +73,7 @@ def change
# - https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
#
def versions_table_options
if MYSQL_ADAPTERS.include?(connection.class.name)
if mysql?
{ options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }
else
{}
Expand Down
22 changes: 17 additions & 5 deletions test/dummy/db/migrate/20110208155312_set_up_test_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def up
end

create_table :versions, versions_table_options do |t|
t.string :item_type, null: false
t.string :item_type, item_type_options
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
Expand Down Expand Up @@ -323,11 +323,23 @@ def down

private

def item_type_options
opt = { null: false }
if mysql?
opt[:limit] = 191
end
opt
end

def mysql?
MYSQL_ADAPTERS.include?(connection.class.name)
end

def versions_table_options
opts = { force: true }
if MYSQL_ADAPTERS.include?(connection.class.name)
opts[:options] = "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci"
if mysql?
{ options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }
else
{}
end
opts
end
end

0 comments on commit b562147

Please sign in to comment.