1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #4396 from kennyj/fix_4259

Fix GH #4259. When we execute schema dumper, we must remove table_name_prefix and table_name_suffix.
This commit is contained in:
Rafael Mendonça França 2012-06-20 14:08:23 -03:00
commit 1bdc098d97
2 changed files with 40 additions and 4 deletions

View file

@ -70,8 +70,8 @@ HEADER
@connection.tables.sort.each do |tbl|
next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
case ignored
when String; tbl == ignored
when Regexp; tbl =~ ignored
when String; remove_prefix_and_suffix(tbl) == ignored
when Regexp; remove_prefix_and_suffix(tbl) =~ ignored
else
raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
end
@ -92,7 +92,7 @@ HEADER
pk = @connection.primary_key(table)
end
tbl.print " create_table #{table.inspect}"
tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}"
if columns.detect { |c| c.name == pk }
if pk != 'id'
tbl.print %Q(, :primary_key => "#{pk}")
@ -185,7 +185,7 @@ HEADER
if (indexes = @connection.indexes(table)).any?
add_index_statements = indexes.map do |index|
statement_parts = [
('add_index ' + index.table.inspect),
('add_index ' + remove_prefix_and_suffix(index.table).inspect),
index.columns.inspect,
(':name => ' + index.name.inspect),
]
@ -206,5 +206,9 @@ HEADER
stream.puts
end
end
def remove_prefix_and_suffix(table)
table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/, "\\2")
end
end
end

View file

@ -301,4 +301,36 @@ class SchemaDumperTest < ActiveRecord::TestCase
output = standard_dump
assert_match %r{create_table "subscribers", :id => false}, output
end
class CreateDogMigration < ActiveRecord::Migration
def up
create_table("dogs") do |t|
t.column :name, :string
end
add_index "dogs", [:name]
end
def down
drop_table("dogs")
end
end
def test_schema_dump_with_table_name_prefix_and_suffix
original, $stdout = $stdout, StringIO.new
ActiveRecord::Base.table_name_prefix = 'foo_'
ActiveRecord::Base.table_name_suffix = '_bar'
migration = CreateDogMigration.new
migration.migrate(:up)
output = standard_dump
assert_no_match %r{create_table "foo_.+_bar"}, output
assert_no_match %r{create_index "foo_.+_bar"}, output
assert_no_match %r{create_table "schema_migrations"}, output
ensure
migration.migrate(:down)
ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
$stdout = original
end
end