1 class PopulateNodeTagsAndRemove < ActiveRecord::Migration
3 have_nodes = select_value("SELECT count(*) FROM current_nodes").to_i != 0
6 prefix = File.join Dir.tmpdir, "020_populate_node_tags_and_remove.#{$$}."
8 cmd = "db/migrate/020_populate_node_tags_and_remove_helper"
10 if not File.exists? cmd or File.mtime(cmd) < File.mtime(src) then
11 system 'cc -O3 -Wall `mysql_config --cflags --libs` ' +
12 "#{src} -o #{cmd}" or fail
15 conn_opts = ActiveRecord::Base.connection.instance_eval { @connection_options }
16 args = conn_opts.map { |arg| arg.to_s } + [prefix]
17 fail "#{cmd} failed" unless system cmd, *args
19 tempfiles = ['nodes', 'node_tags', 'current_nodes', 'current_node_tags'].
20 map { |base| prefix + base }
21 nodes, node_tags, current_nodes, current_node_tags = tempfiles
24 execute "TRUNCATE nodes"
25 remove_column :nodes, :tags
26 remove_column :current_nodes, :tags
28 add_column :nodes, :version, :bigint, :limit => 20, :null => false
30 create_table :current_node_tags, innodb_table do |t|
31 t.column :id, :bigint, :limit => 64, :null => false
32 t.column :k, :string, :default => "", :null => false
33 t.column :v, :string, :default => "", :null => false
36 create_table :node_tags, innodb_table do |t|
37 t.column :id, :bigint, :limit => 64, :null => false
38 t.column :version, :bigint, :limit => 20, :null => false
39 t.column :k, :string, :default => "", :null => false
40 t.column :v, :string, :default => "", :null => false
43 # now get the data back
44 csvopts = "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n'"
47 execute "LOAD DATA INFILE '#{nodes}' INTO TABLE nodes #{csvopts} (id, latitude, longitude, user_id, visible, timestamp, tile, version)";
48 execute "LOAD DATA INFILE '#{node_tags}' INTO TABLE node_tags #{csvopts} (id, version, k, v)"
49 execute "LOAD DATA INFILE '#{current_node_tags}' INTO TABLE current_node_tags #{csvopts} (id, k, v)"
52 tempfiles.each { |fn| File.unlink fn } if have_nodes
56 raise IrreversibleMigration.new
57 # add_column :nodes, "tags", :text, :default => "", :null => false
58 # add_column :current_nodes, "tags", :text, :default => "", :null => false