Skip to content

Commit

Permalink
Explicitly specify the primary key type
Browse files Browse the repository at this point in the history
  • Loading branch information
lowjoel committed Aug 21, 2017
1 parent acec932 commit 8179e77
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions spec/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

before(:each) do
define_schema do

create_table :users, :force => true do |t|
create_table :users, :id => :integer, :force => true do |t|
t.string :login, :index => { :unique => true }
end

Expand All @@ -17,7 +16,7 @@
create_table :comments, :force => true do |t|
t.string :content
t.integer :user
t.references :user
t.references :user, :type => :integer
t.foreign_key :user_id, :users, :primary_key => :id
end

Expand Down Expand Up @@ -54,15 +53,15 @@ class Comment < ::ActiveRecord::Base ; end

it "should create foreign key with default column" do
recreate_table(@model) do |t|
t.references :user
t.references :user, type: :integer
t.foreign_key :users
end
expect(@model).to reference(:users, :id).on(:user_id)
end

it "should create foreign key with different reference" do
recreate_table(@model) do |t|
t.references :author, :foreign_key => { :references => :users }
t.references :author, :type => :integer, :foreign_key => { :references => :users }
end
expect(@model).to reference(:users, :id).on(:author_id)
end
Expand All @@ -71,7 +70,7 @@ class Comment < ::ActiveRecord::Base ; end
hash = { :references => :users }
hash_original = hash.dup
recreate_table(@model) do |t|
t.references :author, :foreign_key => hash
t.references :author, :type => :integer, :foreign_key => hash
end
expect(hash).to eq(hash_original)
end
Expand All @@ -87,21 +86,21 @@ class Comment < ::ActiveRecord::Base ; end

it "should create foreign key with different reference using shortcut" do
recreate_table(@model) do |t|
t.references :author, :references => :users
t.references :author, :type => :integer, :references => :users
end
expect(@model).to reference(:users, :id).on(:author_id)
end

it "should create foreign key with default name" do
recreate_table @model do |t|
t.references :user, :foreign_key => true
t.references :user, :type => :integer, :foreign_key => true
end
expect(@model).to reference(:users, :id).with_name("fk_#{@model.table_name}_user_id")
end

it "should create foreign key with specified name" do
recreate_table @model do |t|
t.references :user, :foreign_key => { :name => "wugga" }
t.references :user, :type => :integer, :foreign_key => { :name => "wugga" }
end
expect(@model).to reference(:users, :id).with_name("wugga")
end
Expand All @@ -119,8 +118,8 @@ class Comment < ::ActiveRecord::Base ; end

it "should allow multiple foreign keys to be made" do
recreate_table(@model) do |t|
t.references :user, :references => :users
t.references :updater, :references => :users
t.references :user, :type => :integer, :references => :users
t.references :updater, :type => :integer, :references => :users
end
expect(@model).to reference(:users, :id).on(:user_id)
expect(@model).to reference(:users, :id).on(:updater_id)
Expand Down Expand Up @@ -195,14 +194,14 @@ class Comment < ::ActiveRecord::Base ; end

it "should create and detect on_update #{action.inspect}", if_action_supported do
recreate_table @model do |t|
t.references :user, :foreign_key => { :on_update => action }
t.references :user, :type => :integer, :foreign_key => { :on_update => action }
end
expect(@model).to reference.on(:user_id).on_update(action)
end

it "should create and detect on_update #{action.inspect} using shortcut", if_action_supported do
recreate_table @model do |t|
t.references :user, :on_update => action
t.references :user, :type => :integer, :on_update => action
end
expect(@model).to reference.on(:user_id).on_update(action)
end
Expand All @@ -217,14 +216,14 @@ class Comment < ::ActiveRecord::Base ; end

it "should create and detect on_delete #{action.inspect}", if_action_supported do
recreate_table @model do |t|
t.references :user, :foreign_key => { :on_delete => action }
t.references :user, :type => :integer, :foreign_key => { :on_delete => action }
end
expect(@model).to reference.on(:user_id).on_delete(action)
end

it "should create and detect on_delete #{action.inspect} using shortcut", if_action_supported do
recreate_table @model do |t|
t.references :user, :on_delete => action
t.references :user, :type => :integer, :on_delete => action
end
expect(@model).to reference.on(:user_id).on_delete(action)
end
Expand All @@ -242,7 +241,7 @@ class Comment < ::ActiveRecord::Base ; end
[false, true, :initially_deferred].each do |status|
it "should create and detect deferrable #{status.inspect}", :mysql => :skip do
recreate_table @model do |t|
t.references :user, :on_delete => :cascade, :deferrable => status
t.references :user, :type => :integer, :on_delete => :cascade, :deferrable => status
end
expect(@model).to reference.on(:user_id).deferrable(status)
end
Expand All @@ -251,7 +250,7 @@ class Comment < ::ActiveRecord::Base ; end
it "should use default on_delete action" do
with_fk_config(:on_delete => :cascade) do
recreate_table @model do |t|
t.references :user, foreign_key: true
t.references :user, :type => :integer, :foreign_key => true
end
expect(@model).to reference.on(:user_id).on_delete(:cascade)
end
Expand All @@ -260,7 +259,7 @@ class Comment < ::ActiveRecord::Base ; end
it "should override on_update action per table" do
with_fk_config(:on_update => :cascade) do
recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
t.references :user, foreign_key: true
t.references :user, :type => :integer, :foreign_key => true
end
expect(@model).to reference.on(:user_id).on_update(:restrict)
end
Expand All @@ -269,7 +268,7 @@ class Comment < ::ActiveRecord::Base ; end
it "should override on_delete action per table" do
with_fk_config(:on_delete => :cascade) do
recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
t.references :user, foreign_key: true
t.references :user, :type => :integer, :foreign_key => true
end
expect(@model).to reference.on(:user_id).on_delete(:restrict)
end
Expand All @@ -278,7 +277,7 @@ class Comment < ::ActiveRecord::Base ; end
it "should override on_update action per column" do
with_fk_config(:on_update => :cascade) do
recreate_table @model, :foreign_keys => {:on_update => :restrict} do |t|
t.references :user, :foreign_key => { :on_update => :nullify }
t.references :user, :type => :integer, :foreign_key => { :on_update => :nullify }
end
expect(@model).to reference.on(:user_id).on_update(:nullify)
end
Expand All @@ -287,7 +286,7 @@ class Comment < ::ActiveRecord::Base ; end
it "should override on_delete action per column" do
with_fk_config(:on_delete => :cascade) do
recreate_table @model, :foreign_keys => {:on_delete => :restrict} do |t|
t.references :user, :foreign_key => { :on_delete => :nullify }
t.references :user, :type => :integer, :foreign_key => { :on_delete => :nullify }
end
expect(@model).to reference.on(:user_id).on_delete(:nullify)
end
Expand All @@ -296,15 +295,15 @@ class Comment < ::ActiveRecord::Base ; end
it "should raise an error for an invalid on_update action" do
expect {
recreate_table @model do |t|
t.references :user, :foreign_key => { :on_update => :invalid }
t.references :user, :type => :integer, :foreign_key => { :on_update => :invalid }
end
}.to raise_error(ArgumentError)
end

it "should raise an error for an invalid on_delete action" do
expect {
recreate_table @model do |t|
t.references :user, :foreign_key => { :on_delete => :invalid }
t.references :user, :type => :integer, :foreign_key => { :on_delete => :invalid }
end
}.to raise_error(ArgumentError)
end
Expand All @@ -320,7 +319,7 @@ class Comment < ::ActiveRecord::Base ; end

it "should create a foreign key constraint"+suffix, :sqlite3 => :skip do
change_table(@model, :bulk => bulk) do |t|
t.references :user, foreign_key: true
t.references :user, :type => :integer, :foreign_key => true
end
expect(@model).to reference(:users, :id).on(:user_id)
end
Expand All @@ -332,7 +331,7 @@ class Comment < ::ActiveRecord::Base ; end
migration = Class.new ::ActiveRecord::Migration.latest_version do
define_method(:change) {
change_table("comments", :bulk => bulk) do |t|
t.references :user, foreign_key: true
t.references :user, :type => :integer, :foreign_key => true
end
}
end
Expand Down

0 comments on commit 8179e77

Please sign in to comment.