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