From 270bcebdf79fd033e8451a2b6ab1c3db1dfb87b2 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 12 Feb 2016 07:26:05 +0900 Subject: Extract `default_primary_key?` to refactor `column_spec_for_primary_key` --- .../connection_adapters/abstract/schema_dumper.rb | 6 +++++- .../connection_adapters/mysql/schema_dumper.rb | 15 +++++++-------- .../connection_adapters/postgresql/schema_dumper.rb | 17 +++++++---------- activerecord/lib/active_record/schema_dumper.rb | 2 +- activerecord/test/cases/primary_keys_test.rb | 4 ++-- 5 files changed, 22 insertions(+), 22 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 2209874d0a..e5dc7d9b06 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb @@ -14,7 +14,7 @@ module ActiveRecord end def column_spec_for_primary_key(column) - return if column.type == :integer + return {} if default_primary_key?(column) spec = { id: schema_type(column).inspect } spec.merge!(prepare_column_options(column)) end @@ -56,6 +56,10 @@ module ActiveRecord private + def default_primary_key?(column) + schema_type(column) == :integer + end + def schema_type(column) column.type end 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 914ea98f79..c65e99812f 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -3,18 +3,13 @@ module ActiveRecord module MySQL module ColumnDumper def column_spec_for_primary_key(column) - spec = {} if column.bigint? - spec[:id] = ':bigint' + spec = { id: :bigint.inspect } spec[:default] = schema_default(column) || 'nil' unless column.auto_increment? - spec[:unsigned] = 'true' if column.unsigned? - elsif column.auto_increment? - spec[:unsigned] = 'true' if column.unsigned? - return if spec.empty? else - spec[:id] = schema_type(column).inspect - spec.merge!(prepare_column_options(column).delete_if { |key, _| key == :null }) + spec = super.except!(:null) end + spec[:unsigned] = 'true' if column.unsigned? spec end @@ -30,6 +25,10 @@ module ActiveRecord private + def default_primary_key?(column) + super && 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 5cb2bbbf18..8c983f240c 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_dumper.rb @@ -3,16 +3,9 @@ module ActiveRecord module PostgreSQL module ColumnDumper def column_spec_for_primary_key(column) - spec = {} - if column.serial? - return unless column.bigint? - spec[:id] = ':bigserial' - elsif column.type == :uuid - spec[:id] = ':uuid' - spec[:default] = schema_default(column) || 'nil' - else - spec[:id] = schema_type(column).inspect - spec.merge!(prepare_column_options(column).delete_if { |key, _| key == :null }) + spec = super.except!(:null) + if schema_type(column) == :uuid + spec[:default] ||= 'nil' end spec end @@ -31,6 +24,10 @@ module ActiveRecord private + def default_primary_key?(column) + schema_type(column) == :serial + end + def schema_type(column) return super unless column.serial? diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index f115c7542b..91be063a4f 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -126,7 +126,7 @@ HEADER tbl.print ", primary_key: #{pk.inspect}" unless pk == 'id' pkcol = columns.detect { |c| c.name == pk } pkcolspec = @connection.column_spec_for_primary_key(pkcol) - if pkcolspec + if pkcolspec.present? pkcolspec.each do |key, value| tbl.print ", #{key}: #{value}" end diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 6aaee8de7c..32bccce2ed 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -351,9 +351,9 @@ if current_adapter?(:PostgreSQLAdapter, :Mysql2Adapter) test "schema dump primary key with bigserial" do schema = dump_table_schema "widgets" if current_adapter?(:PostgreSQLAdapter) - assert_match %r{create_table "widgets", id: :bigserial}, schema + assert_match %r{create_table "widgets", id: :bigserial, force: :cascade}, schema else - assert_match %r{create_table "widgets", id: :bigint}, schema + assert_match %r{create_table "widgets", id: :bigint, force: :cascade}, schema end end -- cgit v1.2.3