Merge pull request #748 from airblade/dummy_migration_should_match_generators

Fixes "Unknown collation: 'utf8mb4_col'" in MySQL
This commit is contained in:
Jared Beck 2016-03-31 17:09:03 -04:00
commit 6f630afca6
4 changed files with 27 additions and 6 deletions

View File

@ -3,7 +3,9 @@
# Offense count: 19
Metrics/AbcSize:
Max: 159
Exclude:
- 'test/dummy/db/migrate/20110208155312_set_up_test_tables.rb'
Max: 46 # Goal: 15
# Offense count: 1
Metrics/BlockNesting:

View File

@ -58,7 +58,7 @@ class CreateVersions < ActiveRecord::Migration
#
def versions_table_options
if MYSQL_ADAPTERS.include?(connection.class.name)
{ options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_col" }
{ options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }
else
{}
end

View File

@ -36,7 +36,7 @@ RSpec.describe CreateVersions do
it "uses utf8mb4_col collation" do
migration.change
expect(migration).to have_received(:create_table) do |_, arg2|
expect(arg2[:options]).to match(/COLLATE=utf8mb4_col/)
expect(arg2[:options]).to match(/COLLATE=utf8mb4_general_ci/)
end
end
else

View File

@ -1,4 +1,13 @@
# Keep this migration in sync with
# `lib/generators/paper_trail/templates/create_versions.rb`
# TODO: Is there a way to avoid duplication?
class SetUpTestTables < ActiveRecord::Migration
MYSQL_ADAPTERS = [
"ActiveRecord::ConnectionAdapters::MysqlAdapter",
"ActiveRecord::ConnectionAdapters::Mysql2Adapter"
].freeze
TEXT_BYTES = 1_073_741_823
def up
create_table :skippers, force: true do |t|
t.string :name
@ -21,13 +30,13 @@ class SetUpTestTables < ActiveRecord::Migration
t.timestamps null: true
end
create_table :versions, force: true do |t|
create_table :versions, versions_table_options do |t|
t.string :item_type, null: false
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.text :object
t.text :object_changes
t.text :object, limit: TEXT_BYTES
t.text :object_changes, limit: TEXT_BYTES
t.integer :transaction_id
t.datetime :created_at
@ -286,4 +295,14 @@ class SetUpTestTables < ActiveRecord::Migration
drop_table :version_associations
drop_table :callback_modifiers
end
private
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"
end
opts
end
end