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 e23ec4c79c7f402f3d306771616c1d597132e2fa 1 parent b3693bf
Rafael Mendonça França 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}")
@@ -181,7 +181,7 @@ def indexes(table, stream)
181 181
         if (indexes = @connection.indexes(table)).any?
182 182
           add_index_statements = indexes.map do |index|
183 183
             statement_parts = [
184  
-              ('add_index ' + index.table.inspect),
  184
+              ('add_index ' + remove_prefix_and_suffix(index.table).inspect),
185 185
               index.columns.inspect,
186 186
               (':name => ' + index.name.inspect),
187 187
             ]
@@ -200,5 +200,9 @@ def indexes(table, stream)
200 200
           stream.puts
201 201
         end
202 202
       end
  203
+
  204
+      def remove_prefix_and_suffix(table)
  205
+        table.gsub(/^(#{ActiveRecord::Base.table_name_prefix})(.+)(#{ActiveRecord::Base.table_name_suffix})$/,  "\\2")
  206
+      end
203 207
   end
204 208
 end
32  activerecord/test/cases/schema_dumper_test.rb
@@ -243,4 +243,36 @@ def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_
243 243
     output = standard_dump
244 244
     assert_match %r{create_table "subscribers", :id => false}, output
245 245
   end
  246
+
  247
+  class CreateDogMigration < ActiveRecord::Migration
  248
+    def up
  249
+      create_table("dogs") do |t|
  250
+        t.column :name, :string
  251
+      end
  252
+      add_index "dogs", [:name]
  253
+    end
  254
+    def down
  255
+      drop_table("dogs")
  256
+    end
  257
+  end
  258
+
  259
+  def test_schema_dump_with_table_name_prefix_and_suffix
  260
+    original, $stdout = $stdout, StringIO.new
  261
+    ActiveRecord::Base.table_name_prefix = 'foo_'
  262
+    ActiveRecord::Base.table_name_suffix = '_bar'
  263
+
  264
+    migration = CreateDogMigration.new
  265
+    migration.migrate(:up)
  266
+
  267
+    output = standard_dump
  268
+    assert_no_match %r{create_table "foo_.+_bar"}, output
  269
+    assert_no_match %r{create_index "foo_.+_bar"}, output
  270
+    assert_no_match %r{create_table "schema_migrations"}, output
  271
+  ensure
  272
+    migration.migrate(:down)
  273
+
  274
+    ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
  275
+    $stdout = original
  276
+  end
  277
+
246 278
 end

0 notes on commit e23ec4c

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