Skip to content
This repository
Browse code

Fix GH #4259. We must remove table_name_prefix and table_name_suffix,…

… when we execute schema dumper.
  • Loading branch information...
commit 211dcdeaa922c74ac20d274308fb5d41ad490194 1 parent c763b03
Toshinori Kajihara kennyj authored
12 activerecord/lib/active_record/schema_dumper.rb
@@ -70,8 +70,8 @@ def tables(stream)
70 70 @connection.tables.sort.each do |tbl|
71 71 next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
72 72 case ignored
73   - when String; tbl == ignored
74   - when Regexp; tbl =~ ignored
  73 + when String; remove_prefix_and_suffix(tbl) == ignored
  74 + when Regexp; remove_prefix_and_suffix(tbl) =~ ignored
75 75 else
76 76 raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
77 77 end
@@ -92,7 +92,7 @@ def table(table, stream)
92 92 pk = @connection.primary_key(table)
93 93 end
94 94
95   - tbl.print " create_table #{table.inspect}"
  95 + tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}"
96 96 if columns.detect { |c| c.name == pk }
97 97 if pk != 'id'
98 98 tbl.print %Q(, :primary_key => "#{pk}")
@@ -185,7 +185,7 @@ def indexes(table, stream)
185 185 if (indexes = @connection.indexes(table)).any?
186 186 add_index_statements = indexes.map do |index|
187 187 statement_parts = [
188   - ('add_index ' + index.table.inspect),
  188 + ('add_index ' + remove_prefix_and_suffix(index.table).inspect),
189 189 index.columns.inspect,
190 190 (':name => ' + index.name.inspect),
191 191 ]
@@ -204,5 +204,9 @@ def indexes(table, stream)
204 204 stream.puts
205 205 end
206 206 end
  207 +
  208 + def remove_prefix_and_suffix(table)
  209 + table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/, "\\2")
  210 + end
207 211 end
208 212 end
32 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_
242 242 output = standard_dump
243 243 assert_match %r{create_table "subscribers", :id => false}, output
244 244 end
  245 +
  246 + class CreateDogMigration < ActiveRecord::Migration
  247 + def up
  248 + create_table("dogs") do |t|
  249 + t.column :name, :string
  250 + end
  251 + add_index "dogs", [:name]
  252 + end
  253 + def down
  254 + drop_table("dogs")
  255 + end
  256 + end
  257 +
  258 + def test_schema_dump_with_table_name_prefix_and_suffix
  259 + original, $stdout = $stdout, StringIO.new
  260 + ActiveRecord::Base.table_name_prefix = 'foo_'
  261 + ActiveRecord::Base.table_name_suffix = '_bar'
  262 +
  263 + migration = CreateDogMigration.new
  264 + migration.migrate(:up)
  265 +
  266 + output = standard_dump
  267 + assert_no_match %r{create_table "foo_.+_bar"}, output
  268 + assert_no_match %r{create_index "foo_.+_bar"}, output
  269 + assert_no_match %r{create_table "schema_migrations"}, output
  270 + ensure
  271 + migration.migrate(:down)
  272 +
  273 + ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
  274 + $stdout = original
  275 + end
  276 +
245 277 end

0 comments on commit 211dcde

Please sign in to comment.
Something went wrong with that request. Please try again.