]> git.openstreetmap.org Git - rails.git/blob - db/migrate/020_populate_node_tags_and_remove.rb
reworking terms page + replacing pad classes with inner classes.
[rails.git] / db / migrate / 020_populate_node_tags_and_remove.rb
1 require 'migrate'
2
3 class PopulateNodeTagsAndRemove < ActiveRecord::Migration
4   def self.up
5     have_nodes = select_value("SELECT count(*) FROM current_nodes").to_i != 0
6
7     if have_nodes
8       prefix = File.join Dir.tmpdir, "020_populate_node_tags_and_remove.#{$$}."
9
10       cmd = "db/migrate/020_populate_node_tags_and_remove_helper"
11       src = "#{cmd}.c"
12       if not File.exists? cmd or File.mtime(cmd) < File.mtime(src) then 
13         system 'cc -O3 -Wall `mysql_config --cflags --libs` ' +
14           "#{src} -o #{cmd}" or fail
15       end
16
17       conn_opts = ActiveRecord::Base.connection.instance_eval { @connection_options }
18       args = conn_opts.map { |arg| arg.to_s } + [prefix]
19       fail "#{cmd} failed" unless system cmd, *args
20
21       tempfiles = ['nodes', 'node_tags', 'current_nodes', 'current_node_tags'].
22         map { |base| prefix + base }
23       nodes, node_tags, current_nodes, current_node_tags = tempfiles
24     end
25
26     execute "TRUNCATE nodes"
27     remove_column :nodes, :tags
28     remove_column :current_nodes, :tags
29
30     add_column :nodes, :version, :bigint, :limit => 20, :null => false
31
32     create_table :current_node_tags, innodb_table do |t|
33       t.column :id,          :bigint, :limit => 64, :null => false
34       t.column :k,           :string, :default => "", :null => false
35       t.column :v,           :string, :default => "", :null => false
36     end
37
38     create_table :node_tags, innodb_table do |t|
39       t.column :id,          :bigint, :limit => 64, :null => false
40       t.column :version,     :bigint, :limit => 20, :null => false
41       t.column :k,           :string, :default => "", :null => false
42       t.column :v,           :string, :default => "", :null => false
43     end
44
45     # now get the data back
46     csvopts = "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n'"
47
48     if have_nodes
49       execute "LOAD DATA INFILE '#{nodes}' INTO TABLE nodes #{csvopts} (id, latitude, longitude, user_id, visible, timestamp, tile, version)";
50       execute "LOAD DATA INFILE '#{node_tags}' INTO TABLE node_tags #{csvopts} (id, version, k, v)"
51       execute "LOAD DATA INFILE '#{current_node_tags}' INTO TABLE current_node_tags #{csvopts} (id, k, v)"
52     end
53
54     tempfiles.each { |fn| File.unlink fn } if have_nodes
55   end
56
57   def self.down
58     raise ActiveRecord::IrreversibleMigration
59 #    add_column :nodes, "tags", :text, :default => "", :null => false
60 #    add_column :current_nodes, "tags", :text, :default => "", :null => false
61   end
62 end