1 class OrderRelationMembers < ActiveRecord::Migration[4.2]
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 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 alter_primary_key("relation_members", [: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 execute("update current_relation_members set sequence_id = mod(member_id, 16384)")
23 alter_primary_key("current_relation_members", [:id, :member_type, :member_id, :member_role, :sequence_id])
27 alter_primary_key("current_relation_members", [:id, :member_type, :member_id, :member_role])
28 remove_column :relation_members, :sequence_id
30 alter_primary_key("relation_members", [:id, :version, :member_type, :member_id, :member_role])
31 remove_column :current_relation_members, :sequence_id