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