]> git.openstreetmap.org Git - rails.git/blob - db/migrate/022_order_relation_members.rb
e3820d4e610ce5a4cca6573f3b1293216f6755ad
[rails.git] / db / migrate / 022_order_relation_members.rb
1 class OrderRelationMembers < ActiveRecord::Migration
2   def self.up
3     # add sequence column. rails won't let us define an ordering here,
4     # as defaults must be constant.
5     add_column(:relation_members, :sequence_id, :integer,
6                :default => 0, :null => false)
7
8     # update the sequence column with default (partial) ordering by 
9     # element ID. the sequence ID is a smaller int type, so we can't
10     # just copy the member_id.
11     ActiveRecord::Base.connection().execute("update relation_members set sequence_id = mod(member_id, 16384)")
12
13     # need to update the primary key to include the sequence number, 
14     # otherwise the primary key will barf when we have repeated members.
15     # mysql barfs on this anyway, so we need a single command. this may
16     # not work in postgres... needs testing.
17     ActiveRecord::Base.connection().execute("alter table relation_members drop primary key, add primary key (id, version, member_type, member_id, member_role, sequence_id)")
18
19     # do the same for the current tables
20     add_column(:current_relation_members, :sequence_id, :integer,
21                :default => 0, :null => false)
22     ActiveRecord::Base.connection().execute("update current_relation_members set sequence_id = mod(member_id, 16384)")
23     ActiveRecord::Base.connection().execute("alter table current_relation_members drop primary key, add primary key (id, member_type, member_id, member_role, sequence_id)")
24   end
25
26   def self.down
27     ActiveRecord::Base.connection().execute("alter table current_relation_members drop primary key, add primary key (id, member_type, member_id, member_role)")
28     remove_column :relation_members, :sequence_id
29
30     ActiveRecord::Base.connection().execute("alter table relation_members drop primary key, add primary key (id, version, member_type, member_id, member_role)")
31     remove_column :current_relation_members, :sequence_id
32   end
33 end