Skip to content

Commit

Permalink
Merge pull request #4396 from kennyj/fix_4259
Browse files Browse the repository at this point in the history
Fix GH #4259. When we execute schema dumper, we must remove table_name_prefix and table_name_suffix.
  • Loading branch information
rafaelfranca committed Jun 20, 2012
2 parents 4f700ed + 211dcde commit 1bdc098
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
12 changes: 8 additions & 4 deletions activerecord/lib/active_record/schema_dumper.rb
Expand Up @@ -70,8 +70,8 @@ def tables(stream)
@connection.tables.sort.each do |tbl| @connection.tables.sort.each do |tbl|
next if ['schema_migrations', ignore_tables].flatten.any? do |ignored| next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
case ignored case ignored
when String; tbl == ignored when String; remove_prefix_and_suffix(tbl) == ignored
when Regexp; tbl =~ ignored when Regexp; remove_prefix_and_suffix(tbl) =~ ignored
else else
raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.' raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
end end
Expand All @@ -92,7 +92,7 @@ def table(table, stream)
pk = @connection.primary_key(table) pk = @connection.primary_key(table)
end 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 columns.detect { |c| c.name == pk }
if pk != 'id' if pk != 'id'
tbl.print %Q(, :primary_key => "#{pk}") tbl.print %Q(, :primary_key => "#{pk}")
Expand Down Expand Up @@ -185,7 +185,7 @@ def indexes(table, stream)
if (indexes = @connection.indexes(table)).any? if (indexes = @connection.indexes(table)).any?
add_index_statements = indexes.map do |index| add_index_statements = indexes.map do |index|
statement_parts = [ statement_parts = [
('add_index ' + index.table.inspect), ('add_index ' + remove_prefix_and_suffix(index.table).inspect),
index.columns.inspect, index.columns.inspect,
(':name => ' + index.name.inspect), (':name => ' + index.name.inspect),
] ]
Expand All @@ -206,5 +206,9 @@ def indexes(table, stream)
stream.puts stream.puts
end end
end end

def remove_prefix_and_suffix(table)
table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/, "\\2")
end
end end
end end
32 changes: 32 additions & 0 deletions activerecord/test/cases/schema_dumper_test.rb
Expand Up @@ -301,4 +301,36 @@ def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_
output = standard_dump output = standard_dump
assert_match %r{create_table "subscribers", :id => false}, output assert_match %r{create_table "subscribers", :id => false}, output
end 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 end

0 comments on commit 1bdc098

Please sign in to comment.