diff --git a/db/migrate/20181219043340_add_fields_to_node.rb b/db/migrate/20181219043340_add_fields_to_node.rb new file mode 100644 index 0000000000..a004172e26 --- /dev/null +++ b/db/migrate/20181219043340_add_fields_to_node.rb @@ -0,0 +1,35 @@ +class AddFieldsToNode < ActiveRecord::Migration[5.2] + def up + add_column :node, :latitude, :decimal, :precision => 20, :scale => 17 + add_column :node, :longitude, :decimal, :precision => 20, :scale => 17 + add_column :node, :precision, :integer + + sql = "SELECT `node`.`nid`,`term_data`.`name` FROM `node` LEFT OUTER JOIN `community_tags` ON `community_tags`.`nid` = `node`.`nid` LEFT OUTER JOIN `term_data` ON `term_data`.`tid` = `community_tags`.`tid` WHERE (term_data.name LIKE 'lat:%' or term_data.name LIKE 'lon:%')" + tags_array = ActiveRecord::Base.connection.exec_query(sql) + tags_array.each do |tag| + node_id = tag["nid"] + if tag["name"].include? 'lon:' + long = tag["name"].gsub("lon:","").to_f + update "UPDATE `node` SET `precision` = '" + decimals(long).to_s + "' WHERE nid = '" + node_id.to_s + "'" + update "UPDATE `node` SET `longitude` = '" + long.to_s + "' WHERE nid = '" + node_id.to_s + "'" + else + lati = tag["name"].gsub("lat:","") + update "UPDATE `node` SET `latitude` = '" + lati + "' WHERE nid = '" + node_id.to_s + "'" + end + end + end + + def down + remove_column :node, :latitude + remove_column :node, :longitude + remove_column :node, :precision + end + + def decimals(n) + if not n.to_s.include? '.' + return 0 + else + return n.to_s.split('.').last.size + end + end +end diff --git a/db/schema.rb.example b/db/schema.rb.example index ec33cff4d6..625a153c0d 100644 --- a/db/schema.rb.example +++ b/db/schema.rb.example @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_11_29_100000) do +ActiveRecord::Schema.define(version: 2018_12_19_043340) do create_table "answer_selections", force: true do |t| t.integer "user_id" @@ -234,6 +234,9 @@ ActiveRecord::Schema.define(version: 2018_11_29_100000) do t.string "slug" t.integer "legacy_views", default: 0 t.integer "views", default: 0 + t.decimal "latitude", precision: 20, scale: 17 + t.decimal "longitude", precision: 20, scale: 17 + t.integer "precision" end add_index "node", ["changed"], name: "node_changed", using: :btree