]> git.openstreetmap.org Git - rails.git/blob - db/migrate/024_order_relation_members.rb
Tweak migration to ensure that migrating from a database with existing
[rails.git] / db / migrate / 024_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     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     alter_primary_key("relation_members", [: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     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])
24   end
25
26   def self.down
27     alter_primary_key("current_relation_members", [:id, :member_type, :member_id, :member_role])
28     remove_column :relation_members, :sequence_id
29
30     alter_primary_key("relation_members", [:id, :version, :member_type, :member_id, :member_role])
31     remove_column :current_relation_members, :sequence_id
32   end
33 end