Skip to content

Commit

Permalink
Supports table_name prefix/suffix for add_foreign_key
Browse files Browse the repository at this point in the history
Closes reenhanced#1

LGTM given by: @codenamev

Squashed commit of the following:

commit b1c0fed
Author: Nicholas Hance <nhance@reenhanced.com>
Date:   Tue Dec 11 11:16:39 2012 -0500

    typo

commit 6f776e4
Author: Nicholas Hance <nhance@reenhanced.com>
Date:   Tue Dec 11 11:07:25 2012 -0500

    Supports table_name_suffix for add_foreign key

commit db3c265
Author: Nicholas Hance <nhance@reenhanced.com>
Date:   Tue Dec 11 10:44:34 2012 -0500

    Support table prefix for add_foreign_key
  • Loading branch information
nhance committed Jan 15, 2013
1 parent 96074b2 commit 0eab85d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Expand Up @@ -94,9 +94,11 @@ def foreign_key_definition(to_table, options = {}) #:nodoc:
references = options[:references] ? options[:references].first : nil
references_sql = quote_column_name(options[:primary_key] || references || "id")
end

sql = "FOREIGN KEY (#{columns_sql}) REFERENCES #{quote_table_name(to_table)}(#{references_sql})"


table_name = ActiveRecord::Migrator.proper_table_name(to_table)

sql = "FOREIGN KEY (#{columns_sql}) REFERENCES #{quote_table_name(table_name)}(#{references_sql})"

case options[:dependent]
when :nullify
sql << " ON DELETE SET NULL"
Expand Down
Expand Up @@ -545,7 +545,12 @@ class ::TestEmployee < ActiveRecord::Base; end
end

describe "foreign key constraints" do
let(:table_name_prefix) { nil }
let(:table_name_suffix) { nil }

before(:each) do
ActiveRecord::Base.table_name_prefix = table_name_prefix
ActiveRecord::Base.table_name_suffix = table_name_suffix
schema_define do
create_table :test_posts, :force => true do |t|
t.string :title
Expand All @@ -571,6 +576,8 @@ class ::TestComment < ActiveRecord::Base
drop_table :test_comments rescue nil
drop_table :test_posts rescue nil
end
ActiveRecord::Base.table_name_prefix = nil
ActiveRecord::Base.table_name_suffix = nil
ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
end

Expand All @@ -583,6 +590,34 @@ class ::TestComment < ActiveRecord::Base
end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.TEST_COMMENTS_TEST_POST_ID_FK/}
end

context "with table_name_prefix" do
let(:table_name_prefix) { 'xxx_' }

it "should use table_name_prefix for foreign table" do
schema_define do
add_foreign_key :test_comments, :test_posts
end

lambda do
TestComment.create(:body => "test", :test_post_id => 1)
end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.XXX_TES_COM_TES_POS_ID_FK/}
end
end

context "with table_name_suffix" do
let(:table_name_suffix) { '_xxx' }

it "should use table_name_suffix for foreign table" do
schema_define do
add_foreign_key :test_comments, :test_posts
end

lambda do
TestComment.create(:body => "test", :test_post_id => 1)
end.should raise_error() {|e| e.message.should =~ /ORA-02291.*\.TES_COM_XXX_TES_POS_ID_FK/}
end
end

it "should add foreign key with name" do
schema_define do
add_foreign_key :test_comments, :test_posts, :name => "comments_posts_fk"
Expand Down

0 comments on commit 0eab85d

Please sign in to comment.