]> git.openstreetmap.org Git - rails.git/blobdiff - db/migrate/20180204153242_tile_users.rb
Add an indexed home_tile column to the user table
[rails.git] / db / migrate / 20180204153242_tile_users.rb
diff --git a/db/migrate/20180204153242_tile_users.rb b/db/migrate/20180204153242_tile_users.rb
new file mode 100644 (file)
index 0000000..cf20aed
--- /dev/null
@@ -0,0 +1,17 @@
+class TileUsers < ActiveRecord::Migration[5.1]
+  def up
+    add_column :users, :home_tile, :bigint
+    add_index :users, [:home_tile], :name => "users_home_idx"
+
+    if ENV["USE_DB_FUNCTIONS"]
+      User.update_all("home_tile = tile_for_point(cast(round(home_lat * #{GeoRecord::SCALE}) as integer), cast(round(home_lon * #{GeoRecord::SCALE}) as integer))")
+    else
+      User.all.each(&:save!)
+    end
+  end
+
+  def down
+    remove_index :users, :name => "users_home_idx"
+    remove_column :users, :home_tile
+  end
+end