Fix MySQL 5.6 bug re: item_type length

[Fixes #651]
This commit is contained in:
Jared Beck 2016-05-26 14:17:20 -04:00
parent 17c8f34f18
commit 445c93f317
3 changed files with 32 additions and 9 deletions

View File

@ -1,4 +1,4 @@
## 5.?.? (Unreleased)
## 5.1.1 (Unreleased)
### Breaking Changes
@ -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)

View File

@ -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
@ -43,6 +43,18 @@ class CreateVersions < ActiveRecord::Migration
private
# MySQL 5.6 utf8mb4 limit is 191 chars for keys used in indexes.
# See https://github.com/airblade/paper_trail/issues/651
def item_type_options
opt = { null: false }
opt[:limit] = 191 if mysql?
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
@ -59,7 +71,7 @@ class CreateVersions < ActiveRecord::Migration
# - 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
{}

View File

@ -38,7 +38,7 @@ class SetUpTestTables < ActiveRecord::Migration
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
@ -323,11 +323,21 @@ class SetUpTestTables < ActiveRecord::Migration
private
def item_type_options
opt = { null: false }
opt[:limit] = 191 if mysql?
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