Skip to content

Commit

Permalink
Fix primary key issue for Rails 3.x and 4.0 and mysql 5.7
Browse files Browse the repository at this point in the history
MySQL 5.7 no longer supports null default values for the primary key.
Override the Native default for primary keys in MySql.
https://stackoverflow.com/a/34555109
  • Loading branch information
p8 authored and albus522 committed Aug 20, 2019
1 parent 1307e3d commit 340add1
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@
ActiveRecord::Base.logger = Delayed::Worker.logger
ActiveRecord::Migration.verbose = false

# MySQL 5.7 no longer supports null default values for the primary key
# Override the default primary key type in Rails <= 4.0
# https://stackoverflow.com/a/34555109
if db_adapter == "mysql2" &&
(::ActiveRecord::VERSION::MAJOR == 3 ||
(::ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0))
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
end

migration_template = File.open("lib/generators/delayed_job/templates/migration.rb")

# need to eval the template with the migration_version intact
Expand Down

0 comments on commit 340add1

Please sign in to comment.