diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2017-02-06 22:16:19 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2017-02-06 22:16:19 -0700 |
commit | 968e66b5df92e5ae88c033dc552e17b4c736874b (patch) | |
tree | d3ff117eef279026e544fc12bc81aaf25fb4c411 /activerecord/lib | |
parent | e0e20d62a75d2c05c7f68e0f316effd5c2f5c26f (diff) | |
parent | 29c70abc58357e7b0f6b05e9ca89ba7a95617ed5 (diff) | |
download | rails-968e66b5df92e5ae88c033dc552e17b4c736874b.tar.gz rails-968e66b5df92e5ae88c033dc552e17b4c736874b.tar.bz2 rails-968e66b5df92e5ae88c033dc552e17b4c736874b.zip |
Merge pull request #27389 from kamipo/fix_mysql_pk_dumping_correctly
Restore the behaviour of the compatibility layer for integer-like PKs
* kamipo/fix_mysql_pk_dumping_correctly:
Restore custom primary key tests lost at #26266
Restore the behaviour of the compatibility layer for integer-like PKs
Correctly dump integer-like primary key with default nil
Diffstat (limited to 'activerecord/lib')
6 files changed, 25 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb index f0f4b5e565..34036d8a1d 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb @@ -14,6 +14,8 @@ module ActiveRecord return {} if default_primary_key?(column) spec = { id: schema_type(column).inspect } spec.merge!(prepare_column_options(column).except!(:null)) + spec[:default] ||= "nil" if explicit_primary_key_default?(column) + spec end # This can be overridden on an Adapter level basis to support other @@ -60,6 +62,10 @@ module ActiveRecord schema_type(column) == :bigint end + def explicit_primary_key_default?(column) + false + end + def schema_type_with_virtual(column) if supports_virtual_columns? && column.virtual? :virtual diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb index 3a3b65c2d0..a06dd0f6b8 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -2,15 +2,6 @@ module ActiveRecord module ConnectionAdapters module MySQL module ColumnDumper # :nodoc: - def column_spec_for_primary_key(column) - spec = super - if [:integer, :bigint].include?(schema_type(column)) && !column.auto_increment? - spec[:default] ||= schema_default(column) || "nil" - end - spec[:unsigned] = "true" if column.unsigned? - spec - end - def prepare_column_options(column) spec = super spec[:unsigned] = "true" if column.unsigned? @@ -34,6 +25,10 @@ module ActiveRecord super && column.auto_increment? end + def explicit_primary_key_default?(column) + column.type == :integer && !column.auto_increment? + end + def schema_type(column) if column.sql_type == "tinyblob" :blob diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb index c3e182b43d..0443bd8bdd 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb @@ -42,7 +42,7 @@ module ActiveRecord # a record (as primary keys cannot be +nil+). This might be done via the # +SecureRandom.uuid+ method and a +before_save+ callback, for instance. def primary_key(name, type = :primary_key, **options) - options[:auto_increment] = true if [:primary_key, :integer, :bigint].include?(type) && !options.key?(:default) + options[:auto_increment] = true if [:integer, :bigint].include?(type) && !options.key?(:default) if type == :uuid options[:default] = options.fetch(:default, "gen_random_uuid()") elsif options.delete(:auto_increment) == true && %i(integer bigint).include?(type) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb index 4aa0cbef50..5555a46b6b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb @@ -2,14 +2,6 @@ module ActiveRecord module ConnectionAdapters module PostgreSQL module ColumnDumper # :nodoc: - def column_spec_for_primary_key(column) - spec = super - if schema_type(column) == :uuid - spec[:default] ||= "nil" - end - spec - end - # Adds +:array+ option to the default set def prepare_column_options(column) spec = super @@ -28,6 +20,10 @@ module ActiveRecord schema_type(column) == :bigserial end + def explicit_primary_key_default?(column) + column.type == :uuid || (column.type == :integer && !column.serial?) + end + def schema_type(column) return super unless column.serial? diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb index 2dca0b0f9c..eec018eda3 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb @@ -7,6 +7,10 @@ module ActiveRecord def default_primary_key?(column) schema_type(column) == :integer end + + def explicit_primary_key_default?(column) + column.bigint? + end end end end diff --git a/activerecord/lib/active_record/migration/compatibility.rb b/activerecord/lib/active_record/migration/compatibility.rb index ffeca2c91e..f6c9127d34 100644 --- a/activerecord/lib/active_record/migration/compatibility.rb +++ b/activerecord/lib/active_record/migration/compatibility.rb @@ -21,6 +21,12 @@ module ActiveRecord end end + unless adapter_name == "Mysql2" && options[:id] == :bigint + if [:integer, :bigint].include?(options[:id]) && !options.key?(:default) + options[:default] = nil + end + end + # Since 5.1 Postgres adapter uses bigserial type for primary # keys by default and MySQL uses bigint. This compat layer makes old migrations utilize # serial/int type instead -- the way it used to work before 5.1. |