Skip to content
This repository
Browse code

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.
  • Loading branch information...
commit 1bdc098d9771ed5639de87ea6e94e80372b76a26 2 parents 4f700ed + 211dcde
Rafael Mendonça França authored June 20, 2012
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
             ]
@@ -206,5 +206,9 @@ def indexes(table, stream)
206 206
           stream.puts
207 207
         end
208 208
       end
  209
+
  210
+      def remove_prefix_and_suffix(table)
  211
+        table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/,  "\\2")
  212
+      end
209 213
   end
210 214
 end
32  activerecord/test/cases/schema_dumper_test.rb
@@ -301,4 +301,36 @@ def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_
301 301
     output = standard_dump
302 302
     assert_match %r{create_table "subscribers", :id => false}, output
303 303
   end
  304
+
  305
+  class CreateDogMigration < ActiveRecord::Migration
  306
+    def up
  307
+      create_table("dogs") do |t|
  308
+        t.column :name, :string
  309
+      end
  310
+      add_index "dogs", [:name]
  311
+    end
  312
+    def down
  313
+      drop_table("dogs")
  314
+    end
  315
+  end
  316
+
  317
+  def test_schema_dump_with_table_name_prefix_and_suffix
  318
+    original, $stdout = $stdout, StringIO.new
  319
+    ActiveRecord::Base.table_name_prefix = 'foo_'
  320
+    ActiveRecord::Base.table_name_suffix = '_bar'
  321
+
  322
+    migration = CreateDogMigration.new
  323
+    migration.migrate(:up)
  324
+
  325
+    output = standard_dump
  326
+    assert_no_match %r{create_table "foo_.+_bar"}, output
  327
+    assert_no_match %r{create_index "foo_.+_bar"}, output
  328
+    assert_no_match %r{create_table "schema_migrations"}, output
  329
+  ensure
  330
+    migration.migrate(:down)
  331
+
  332
+    ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
  333
+    $stdout = original
  334
+  end
  335
+
304 336
 end

0 notes on commit 1bdc098

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