]> git.openstreetmap.org Git - rails.git/blob - db/migrate/023_add_changesets.rb
Merge remote-tracking branch 'openstreetmap/pull/1553'
[rails.git] / db / migrate / 023_add_changesets.rb
1 require "migrate"
2
3 class AddChangesets < ActiveRecord::Migration
4   @conv_user_tables = %w[current_nodes current_relations current_ways nodes relations ways]
5
6   def self.up
7     create_table "changesets", :id => false do |t|
8       t.column "id",             :bigserial, :primary_key => true, :null => false
9       t.column "user_id",        :bigint, :null => false
10       t.column "created_at",     :datetime, :null => false
11       t.column "open",           :boolean, :null => false, :default => true
12       t.column "min_lat",        :integer, :null => true
13       t.column "max_lat",        :integer, :null => true
14       t.column "min_lon",        :integer, :null => true
15       t.column "max_lon",        :integer, :null => true
16     end
17
18     create_table "changeset_tags", :id => false do |t|
19       t.column "id", :bigint, :null => false
20       t.column "k",  :string, :default => "", :null => false
21       t.column "v",  :string, :default => "", :null => false
22     end
23
24     add_index "changeset_tags", ["id"], :name => "changeset_tags_id_idx"
25
26     #
27     # Initially we will have one changeset for every user containing
28     # all edits up to the API change,
29     # all the changesets will have the id of the user that made them.
30     # We need to generate a changeset for each user in the database
31     execute "INSERT INTO changesets (id, user_id, created_at, open)" +
32             "SELECT id, id, creation_time, false from users;"
33
34     @conv_user_tables.each do |tbl|
35       rename_column tbl, :user_id, :changeset_id
36       # foreign keys too
37       add_foreign_key tbl, :changesets, :name => "#{tbl}_changeset_id_fkey"
38     end
39   end
40
41   def self.down
42     # It's not easy to generate the user ids from the changesets
43     raise ActiveRecord::IrreversibleMigration
44     # drop_table "changesets"
45     # drop_table "changeset_tags"
46   end
47 end