Skip to content

Commit

Permalink
Revert "Merge pull request #282 from reenhanced/nh-no-foreign-keys-in…
Browse files Browse the repository at this point in the history
…-schema-dump"

This reverts commit 5eac011, reversing
changes made to 33d8be0.
  • Loading branch information
ebeigarts committed Feb 24, 2013
1 parent c692c82 commit df470ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 57 deletions.
Expand Up @@ -59,21 +59,15 @@ def primary_key_trigger(table_name, stream)
def foreign_keys(table_name, stream)
if @connection.respond_to?(:foreign_keys) && (foreign_keys = @connection.foreign_keys(table_name)).any?
add_foreign_key_statements = foreign_keys.map do |foreign_key|
from_table = foreign_key.from_table
to_table = foreign_key.to_table

from_table.extend TableInspect
to_table.extend TableInspect

statement_parts = [ ('add_foreign_key ' + from_table.inspect) ]
statement_parts << to_table.inspect

statement_parts = [ ('add_foreign_key ' + foreign_key.from_table.inspect) ]
statement_parts << foreign_key.to_table.inspect

if foreign_key.options[:columns].size == 1
column = foreign_key.options[:columns].first
if column != "#{to_table.singularize}_id"
if column != "#{foreign_key.to_table.singularize}_id"
statement_parts << (':column => ' + column.inspect)
end

if foreign_key.options[:references].first != 'id'
statement_parts << (':primary_key => ' + foreign_key.options[:primary_key].inspect)
end
Expand All @@ -82,7 +76,7 @@ def foreign_keys(table_name, stream)
end

statement_parts << (':name => ' + foreign_key.options[:name].inspect)

unless foreign_key.options[:dependent].blank?
statement_parts << (':dependent => ' + foreign_key.options[:dependent].inspect)
end
Expand Down Expand Up @@ -159,12 +153,12 @@ def oracle_enhanced_table(table, stream)
elsif @connection.respond_to?(:primary_key)
pk = @connection.primary_key(table)
end

tbl.print " create_table #{table.inspect}"

# addition to make temporary option work
tbl.print ", :temporary => true" if @connection.temporary_table?(table)

if columns.detect { |c| c.name == pk }
if pk != 'id'
tbl.print %Q(, :primary_key => "#{pk}")
Expand Down Expand Up @@ -223,7 +217,7 @@ def oracle_enhanced_table(table, stream)

tbl.puts " end"
tbl.puts

indexes(table, tbl)

tbl.rewind
Expand All @@ -233,20 +227,20 @@ def oracle_enhanced_table(table, stream)
stream.puts "# #{e.message}"
stream.puts
end

stream
end

def remove_prefix_and_suffix(table)
table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/, "\\2")
end

end
# remove table name prefix and suffix when doing #inspect (which is used in tables method)
module TableInspect #:nodoc:
def inspect
remove_prefix_and_suffix(self)
end

private
def remove_prefix_and_suffix(table_name)
if table_name =~ /\A#{ActiveRecord::Base.table_name_prefix.to_s.gsub('$','\$')}(.*)#{ActiveRecord::Base.table_name_suffix.to_s.gsub('$','\$')}\Z/
Expand Down
Expand Up @@ -149,43 +149,6 @@ def drop_test_posts_table

end

describe "foreign key constraints with table prefix" do
before(:all) do
ActiveRecord::Base.table_name_prefix = "xxx_"
schema_define do
create_table :test_posts, :force => true do |t|
t.string :title
end
create_table :test_comments, :force => true do |t|
t.string :body, :limit => 4000
t.references :test_post
end
end
end

after(:each) do
schema_define do
remove_foreign_key :test_comments, :test_posts rescue nil
remove_foreign_key :test_comments, :name => 'comments_posts_baz_fooz_fk' rescue nil
end
end
after(:all) do
schema_define do
drop_table :test_comments rescue nil
drop_table :test_posts rescue nil
end
ActiveRecord::Base.table_name_prefix = ''
end

it "should not include table prefix in schema dump" do
schema_define do
add_foreign_key :test_comments, :test_posts
end
standard_dump.should =~ /add_foreign_key "test_comments", "test_posts", :column => "test_post_id", :name => "xxx_tes_com_tes_pos_id_fk"/
ActiveRecord::Base.table_name_prefix = ''
end
end

describe "foreign key constraints" do
before(:all) do
schema_define do
Expand Down

0 comments on commit df470ef

Please sign in to comment.