Skip to content
This repository
Browse code

Merge pull request #5267 from kennyj/fix_4674

Fix GH #4674. Reset column information and sequence name when setting table_name.
  • Loading branch information...
commit 2fdb5219fb70a6368602c9d6073be8fa70b720fa 2 parents b839657 + 200d994
Jon Leighton jonleighton authored
19 activerecord/lib/active_record/model_schema.rb
@@ -114,10 +114,17 @@ def table_name
114 114 # You can also just define your own <tt>self.table_name</tt> method; see
115 115 # the documentation for ActiveRecord::Base#table_name.
116 116 def table_name=(value)
117   - @table_name = value && value.to_s
118   - @quoted_table_name = nil
119   - @arel_table = nil
120   - @relation = Relation.new(self, arel_table)
  117 + value = value && value.to_s
  118 + if defined?(@table_name)
  119 + return if value == @table_name
  120 +
  121 + reset_column_information
  122 + end
  123 + @table_name = value
  124 + @quoted_table_name = nil
  125 + @arel_table = nil
  126 + @sequence_name = nil unless defined?(@explicitly_sequence_name) && @explicitly_sequence_name
  127 + @relation = Relation.new(self, arel_table)
121 128 end
122 129
123 130 # Returns a quoted version of the table name, used to construct SQL statements.
@@ -163,7 +170,8 @@ def sequence_name
163 170 end
164 171
165 172 def reset_sequence_name #:nodoc:
166   - self.sequence_name = connection.default_sequence_name(table_name, primary_key)
  173 + @sequence_name = connection.default_sequence_name(table_name, primary_key)
  174 + @explicitly_sequence_name = false
167 175 end
168 176
169 177 # Sets the name of the sequence to use when generating ids to the given
@@ -182,6 +190,7 @@ def reset_sequence_name #:nodoc:
182 190 # end
183 191 def sequence_name=(value)
184 192 @sequence_name = value.to_s
  193 + @explicitly_sequence_name = true
185 194 end
186 195
187 196 # Indicates whether the table associated with this class exists
24 activerecord/test/cases/base_test.rb
@@ -1479,6 +1479,30 @@ def test_switching_between_table_name
1479 1479 end
1480 1480 end
1481 1481
  1482 + def test_clear_cash_when_setting_table_name
  1483 + Joke.table_name = "cold_jokes"
  1484 + before_columns = Joke.columns
  1485 + before_seq = Joke.sequence_name
  1486 +
  1487 + Joke.table_name = "funny_jokes"
  1488 + after_columns = Joke.columns
  1489 + after_seq = Joke.sequence_name
  1490 +
  1491 + assert_not_equal before_columns, after_columns
  1492 + assert_not_equal before_seq, after_seq unless before_seq.blank? && after_seq.blank?
  1493 + end
  1494 +
  1495 + def test_dont_clear_sequence_name_when_setting_explicitly
  1496 + Joke.sequence_name = "black_jokes_seq"
  1497 + Joke.table_name = "cold_jokes"
  1498 + before_seq = Joke.sequence_name
  1499 +
  1500 + Joke.table_name = "funny_jokes"
  1501 + after_seq = Joke.sequence_name
  1502 +
  1503 + assert_equal before_seq, after_seq unless before_seq.blank? && after_seq.blank?
  1504 + end
  1505 +
1482 1506 def test_set_table_name_symbol_converted_to_string
1483 1507 Joke.table_name = :cold_jokes
1484 1508 assert_equal 'cold_jokes', Joke.table_name

0 comments on commit 2fdb521

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