From 211dcdeaa922c74ac20d274308fb5d41ad490194 Mon Sep 17 00:00:00 2001 From: kennyj Date: Tue, 10 Jan 2012 03:09:45 +0900 Subject: [PATCH] Fix GH #4259. We must remove table_name_prefix and table_name_suffix, when we execute schema dumper. --- .../lib/active_record/schema_dumper.rb | 12 ++++--- activerecord/test/cases/schema_dumper_test.rb | 32 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 2a565b51c6..0172a3822d 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -70,8 +70,8 @@ def tables(stream) @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 @@ def table(table, stream) 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 @@ def indexes(table, stream) 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), ] @@ -204,5 +204,9 @@ def indexes(table, stream) 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 diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 724632489b..791e2dc975 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -242,4 +242,36 @@ def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_ 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