diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-01-22 09:54:14 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-02-04 20:28:14 +0900 |
commit | 605837a61933fc1e94097eea30e659bb9ef516eb (patch) | |
tree | f722b4a3b8b8852f70fe782b27b173cb9dde74b1 /activerecord/lib/active_record/connection_adapters | |
parent | d13bc5df902a2c82c3096b627830be97acbedf50 (diff) | |
download | rails-605837a61933fc1e94097eea30e659bb9ef516eb.tar.gz rails-605837a61933fc1e94097eea30e659bb9ef516eb.tar.bz2 rails-605837a61933fc1e94097eea30e659bb9ef516eb.zip |
Correctly dump integer-like primary key with default nil
The PR #27384 changed integer-like primary key to be autoincrement
unless an explicit default. This means that integer-like primary key is
restored as autoincrement unless dumping the default nil explicitly.
We should dump integer-like primary key with default nil correctly.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
4 files changed, 21 insertions, 20 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 d6c912a69e..08406e6b2b 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 @@ -59,6 +61,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 7a277b8cfd..a06dd0f6b8 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -1,16 +1,7 @@ module ActiveRecord module ConnectionAdapters module MySQL - module ColumnDumper - 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 - + module ColumnDumper # :nodoc: 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_dumper.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb index 7808d37deb..5555a46b6b 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb @@ -1,15 +1,7 @@ module ActiveRecord module ConnectionAdapters module PostgreSQL - module ColumnDumper - def column_spec_for_primary_key(column) - spec = super - if schema_type(column) == :uuid - spec[:default] ||= "nil" - end - spec - end - + module ColumnDumper # :nodoc: # 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 c027fef83c..eec018eda3 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb @@ -1,12 +1,16 @@ module ActiveRecord module ConnectionAdapters module SQLite3 - module ColumnDumper + module ColumnDumper # :nodoc: private def default_primary_key?(column) schema_type(column) == :integer end + + def explicit_primary_key_default?(column) + column.bigint? + end end end end |