3 class RemoveSegments < ActiveRecord::Migration
 
   5     have_segs = select_value("SELECT count(*) FROM current_segments").to_i != 0
 
   8       prefix = File.join Dir.tmpdir, "008_remove_segments.#{$$}."
 
  10       cmd = "db/migrate/008_remove_segments_helper"
 
  12       if not File.exists? cmd or File.mtime(cmd) < File.mtime(src) then 
 
  13         system 'c++ -O3 -Wall `mysql_config --cflags --libs` ' +
 
  14           "#{src} -o #{cmd}" or fail
 
  17       conn_opts = ActiveRecord::Base.connection.
 
  18         instance_eval { @connection_options }
 
  19       args = conn_opts.map { |arg| arg.to_s } + [prefix]
 
  20       fail "#{cmd} failed" unless system cmd, *args
 
  22       tempfiles = ['ways', 'way_nodes', 'way_tags',
 
  23         'relations', 'relation_members', 'relation_tags'].
 
  24         map { |base| prefix + base }
 
  25       ways, way_nodes, way_tags,
 
  26         relations, relation_members, relation_tags = tempfiles
 
  30     drop_table :way_segments
 
  31     create_table :way_nodes, myisam_table do |t|
 
  32       t.column :id,          :bigint, :limit => 64, :null => false
 
  33       t.column :node_id,     :bigint, :limit => 64, :null => false
 
  34       t.column :version,     :bigint, :limit => 20, :null => false
 
  35       t.column :sequence_id, :bigint, :limit => 11, :null => false
 
  37     add_primary_key :way_nodes, [:id, :version, :sequence_id]
 
  39     drop_table :current_segments
 
  40     drop_table :current_way_segments
 
  41     create_table :current_way_nodes, innodb_table do |t|
 
  42       t.column :id,          :bigint, :limit => 64, :null => false
 
  43       t.column :node_id,     :bigint, :limit => 64, :null => false
 
  44       t.column :sequence_id, :bigint, :limit => 11, :null => false
 
  46     add_primary_key :current_way_nodes, [:id, :sequence_id]
 
  47     add_index :current_way_nodes, [:node_id], :name => "current_way_nodes_node_idx"
 
  49     execute "TRUNCATE way_tags"
 
  50     execute "TRUNCATE ways"
 
  51     execute "TRUNCATE current_way_tags"
 
  52     execute "TRUNCATE current_ways"
 
  54     # now get the data back
 
  55     csvopts = "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n'"
 
  57     tempfiles.each { |fn| File.chmod 0644, fn } if have_segs
 
  60       execute "LOAD DATA INFILE '#{ways}' INTO TABLE ways #{csvopts} (id, user_id, timestamp) SET visible = 1, version = 1"
 
  61       execute "LOAD DATA INFILE '#{way_nodes}' INTO TABLE way_nodes #{csvopts} (id, node_id, sequence_id) SET version = 1"
 
  62       execute "LOAD DATA INFILE '#{way_tags}' INTO TABLE way_tags #{csvopts} (id, k, v) SET version = 1"
 
  64       execute "INSERT INTO current_ways SELECT id, user_id, timestamp, visible FROM ways"
 
  65       execute "INSERT INTO current_way_nodes SELECT id, node_id, sequence_id FROM way_nodes"
 
  66       execute "INSERT INTO current_way_tags SELECT id, k, v FROM way_tags"
 
  70       execute "LOAD DATA INFILE '#{relations}' INTO TABLE relations #{csvopts} (id, user_id, timestamp) SET visible = 1, version = 1"
 
  71       execute "LOAD DATA INFILE '#{relation_members}' INTO TABLE relation_members #{csvopts} (id, member_type, member_id, member_role) SET version = 1"
 
  72       execute "LOAD DATA INFILE '#{relation_tags}' INTO TABLE relation_tags #{csvopts} (id, k, v) SET version = 1"
 
  74       # FIXME: This will only work if there were no relations before the
 
  76       execute "INSERT INTO current_relations SELECT id, user_id, timestamp, visible FROM relations"
 
  77       execute "INSERT INTO current_relation_members SELECT id, member_type, member_id, member_role FROM relation_members"
 
  78       execute "INSERT INTO current_relation_tags SELECT id, k, v FROM relation_tags"
 
  81     tempfiles.each { |fn| File.unlink fn } if have_segs
 
  85     raise IrreversibleMigration.new