Howdy folks
I recently updated one of our apps to Rails 2.3.4 from 2.1.2. The
following migrations, which worked previously, now fail:
Migration 1:
class CreateInitialSchema < ActiveRecord::Migration
self.up
create_table :states, :id => false do |t|
t.column :state_code, :integer
t.column :state_name, :string
end
add_index “states”, “state_code”, :name =>
“index_state_by_code”, :unique => true
add_index “states”, “state_name”, :name =>
“index_state_by_name”, :unique => true
State.create(:state_code => 0, :state_name => ‘Antarctica’)
…etc…
Migration 2:
class State < ActiveRecord::Base; set_primary_key “state_digit”; end
class AddStateCode < ActiveRecord::Migration
self.up
rename_column :states, :state_code, :state_digit
add_column :states, :state_code, :string, :limit => 3
State.reset_column_information
State.find(:all).each do |s|
s.state_code =
case s.state_digit.to_i
when 0; “ANT”
…edit…
else raise “No such state #{s.state_digit}”
end
s.save!
end
…etc…
Migration 3:
class State < ActiveRecord::Base; set_primary_key “state_digit”; end
class RemoveUnusedStates < ActiveRecord::Migration
self.up
State.find_by_state_name(“Antarctica”).destroy
State.find_by_state_name(“External Territories”).destroy
…etc…
It would seem the problem is in migration 2. Renaming the primary key
column and adding a new column with the name of the previous primary
key causes problems in the model which the call to
reset_column_information can’t resolve. In migration 2 and 3,
state_digit is always nil for all records.
Because migration 3 was also failing I take it that Rails caches
classes between migrations?
I don’t what changed from Rails 2.1.2 and 2.3.4 to break these
migrations. If anyone’s encountered this issue or can shed some light
on the problem it would give me some peace of mind.
I worked around the problem by dynamically creating and destroying
classes to access the state table each time I need them, but I’d much
rather understand the root cause of the problem and fix it if needed.
Cheers
Nick