1 class OrderRelationMembers < ActiveRecord::Migration
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)
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)")
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)")
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)")
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
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