]> git.openstreetmap.org Git - rails.git/blob - db/migrate/008_remove_segments.rb
Merge remote-tracking branch 'upstream/pull/3177'
[rails.git] / db / migrate / 008_remove_segments.rb
1 class RemoveSegments < ActiveRecord::Migration[4.2]
2   def self.up
3     have_segs = select_value("SELECT count(*) FROM current_segments").to_i.nonzero?
4
5     if have_segs
6       prefix = File.join Dir.tmpdir, "008_remove_segments.#{$PROCESS_ID}."
7
8       cmd = "db/migrate/008_remove_segments_helper"
9       src = "#{cmd}.cc"
10       if !File.exist?(cmd) || File.mtime(cmd) < File.mtime(src)
11         system("c++ -O3 -Wall `mysql_config --cflags --libs` " \
12           "#{src} -o #{cmd}") || raise
13       end
14
15       conn_opts = ApplicationRecord.connection
16                                    .instance_eval { @connection_options }
17       args = conn_opts.map(&:to_s) + [prefix]
18       raise "#{cmd} failed" unless system cmd, *args
19
20       tempfiles = %w[ways way_nodes way_tags relations relation_members relation_tags]
21                   .map { |base| prefix + base }
22       ways, way_nodes, way_tags,
23   relations, relation_members, relation_tags = tempfiles
24     end
25
26     drop_table :segments
27     drop_table :way_segments
28     create_table :way_nodes, :id => false do |t|
29       t.column :id,          :bigint, :null => false
30       t.column :node_id,     :bigint, :null => false
31       t.column :version,     :bigint, :null => false
32       t.column :sequence_id, :bigint, :null => false
33     end
34     add_primary_key :way_nodes, [:id, :version, :sequence_id]
35
36     drop_table :current_segments
37     drop_table :current_way_segments
38     create_table :current_way_nodes, :id => false do |t|
39       t.column :id,          :bigint, :null => false
40       t.column :node_id,     :bigint, :null => false
41       t.column :sequence_id, :bigint, :null => false
42     end
43     add_primary_key :current_way_nodes, [:id, :sequence_id]
44     add_index :current_way_nodes, [:node_id], :name => "current_way_nodes_node_idx"
45
46     execute "TRUNCATE way_tags"
47     execute "TRUNCATE ways"
48     execute "TRUNCATE current_way_tags"
49     execute "TRUNCATE current_ways"
50
51     # now get the data back
52     csvopts = "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n'"
53
54     tempfiles.each { |fn| File.chmod 0o644, fn } if have_segs
55
56     if have_segs
57       execute "LOAD DATA INFILE '#{ways}' INTO TABLE ways #{csvopts} (id, user_id, timestamp) SET visible = 1, version = 1"
58       execute "LOAD DATA INFILE '#{way_nodes}' INTO TABLE way_nodes #{csvopts} (id, node_id, sequence_id) SET version = 1"
59       execute "LOAD DATA INFILE '#{way_tags}' INTO TABLE way_tags #{csvopts} (id, k, v) SET version = 1"
60
61       execute "INSERT INTO current_ways SELECT id, user_id, timestamp, visible FROM ways"
62       execute "INSERT INTO current_way_nodes SELECT id, node_id, sequence_id FROM way_nodes"
63       execute "INSERT INTO current_way_tags SELECT id, k, v FROM way_tags"
64     end
65
66     if have_segs
67       execute "LOAD DATA INFILE '#{relations}' INTO TABLE relations #{csvopts} (id, user_id, timestamp) SET visible = 1, version = 1"
68       execute "LOAD DATA INFILE '#{relation_members}' INTO TABLE relation_members #{csvopts} (id, member_type, member_id, member_role) SET version = 1"
69       execute "LOAD DATA INFILE '#{relation_tags}' INTO TABLE relation_tags #{csvopts} (id, k, v) SET version = 1"
70
71       # FIXME: This will only work if there were no relations before the
72       # migration!
73       execute "INSERT INTO current_relations SELECT id, user_id, timestamp, visible FROM relations"
74       execute "INSERT INTO current_relation_members SELECT id, member_type, member_id, member_role FROM relation_members"
75       execute "INSERT INTO current_relation_tags SELECT id, k, v FROM relation_tags"
76     end
77
78     tempfiles.each { |fn| File.unlink fn } if have_segs
79   end
80
81   def self.down
82     raise ActiveRecord::IrreversibleMigration
83   end
84 end