|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Phinx\Migration\AbstractMigration; |
| 6 | + |
| 7 | +final class FixGeokretNearHome extends AbstractMigration { |
| 8 | + public function up(): void { |
| 9 | + $this->execute(<<<'SQL' |
| 10 | +BEGIN; |
| 11 | +
|
| 12 | +DROP VIEW geokrety.gk_geokrety_near_users_homes; |
| 13 | +DROP MATERIALIZED VIEW geokrety.gk_geokrety_in_caches; |
| 14 | +
|
| 15 | +CREATE MATERIALIZED VIEW geokrety.gk_geokrety_in_caches AS |
| 16 | +SELECT |
| 17 | + id, gkid, tracking_code, name, mission, owner, distance, caches_count, |
| 18 | + pictures_count, last_position, last_log, holder, avatar, |
| 19 | + created_on_datetime, updated_on_datetime, missing, type, "position", |
| 20 | + lat, lon, waypoint, elevation, country, move_type, author, |
| 21 | + moved_on_datetime, author_username, owner_username, avatar_key |
| 22 | +FROM geokrety.gk_geokrety_with_details |
| 23 | +WHERE move_type IN (0,3) |
| 24 | + AND position IS NOT NULL |
| 25 | +WITH NO DATA; |
| 26 | +
|
| 27 | +REFRESH MATERIALIZED VIEW geokrety.gk_geokrety_in_caches; |
| 28 | +
|
| 29 | +CREATE VIEW geokrety.gk_geokrety_near_users_homes |
| 30 | + AS SELECT c_user.id AS c_user_id, |
| 31 | + c_user.username AS c_username, |
| 32 | + gk_geokrety_in_caches.id, |
| 33 | + gk_geokrety_in_caches.gkid, |
| 34 | + gk_geokrety_in_caches.tracking_code, |
| 35 | + gk_geokrety_in_caches.name, |
| 36 | + gk_geokrety_in_caches.mission, |
| 37 | + gk_geokrety_in_caches.owner, |
| 38 | + gk_geokrety_in_caches.distance, |
| 39 | + gk_geokrety_in_caches.caches_count, |
| 40 | + gk_geokrety_in_caches.pictures_count, |
| 41 | + gk_geokrety_in_caches.last_position, |
| 42 | + gk_geokrety_in_caches.last_log, |
| 43 | + gk_geokrety_in_caches.holder, |
| 44 | + gk_geokrety_in_caches.avatar, |
| 45 | + gk_geokrety_in_caches.created_on_datetime, |
| 46 | + gk_geokrety_in_caches.updated_on_datetime, |
| 47 | + gk_geokrety_in_caches.missing, |
| 48 | + gk_geokrety_in_caches.type, |
| 49 | + gk_geokrety_in_caches."position", |
| 50 | + gk_geokrety_in_caches.lat, |
| 51 | + gk_geokrety_in_caches.lon, |
| 52 | + gk_geokrety_in_caches.waypoint, |
| 53 | + gk_geokrety_in_caches.elevation, |
| 54 | + gk_geokrety_in_caches.country, |
| 55 | + gk_geokrety_in_caches.move_type, |
| 56 | + gk_geokrety_in_caches.author, |
| 57 | + gk_geokrety_in_caches.moved_on_datetime, |
| 58 | + gk_geokrety_in_caches.author_username, |
| 59 | + gk_geokrety_in_caches.owner_username, |
| 60 | + gk_geokrety_in_caches.avatar_key, |
| 61 | + st_distance(gk_geokrety_in_caches."position", coords2position(c_user.home_latitude, c_user.home_longitude)) / 1000::double precision AS home_distance |
| 62 | + FROM gk_geokrety_in_caches, |
| 63 | + gk_users c_user |
| 64 | + WHERE st_dwithin(gk_geokrety_in_caches."position", coords2position(c_user.home_latitude, c_user.home_longitude), (c_user.observation_area * 1000)::double precision) |
| 65 | + ORDER BY (st_distance(gk_geokrety_in_caches."position", coords2position(c_user.home_latitude, c_user.home_longitude)) < (c_user.observation_area * 1000)::double precision); |
| 66 | +COMMIT; |
| 67 | +SQL |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function down(): void { |
| 72 | + $this->execute(<<<'SQL' |
| 73 | +BEGIN; |
| 74 | +
|
| 75 | +DROP VIEW geokrety.gk_geokrety_near_users_homes; |
| 76 | +DROP MATERIALIZED VIEW geokrety.gk_geokrety_in_caches; |
| 77 | +
|
| 78 | +CREATE MATERIALIZED VIEW geokrety.gk_geokrety_in_caches AS |
| 79 | +SELECT |
| 80 | + id, gkid, tracking_code, name, mission, owner, distance, caches_count, |
| 81 | + pictures_count, last_position, last_log, holder, avatar, |
| 82 | + created_on_datetime, updated_on_datetime, missing, type, "position", |
| 83 | + lat, lon, waypoint, elevation, country, move_type, author, |
| 84 | + moved_on_datetime, author_username, owner_username, avatar_key |
| 85 | +FROM geokrety.gk_geokrety_with_details |
| 86 | +WHERE (move_type = ANY (geokrety.moves_types_markable_as_missing())) |
| 87 | +WITH NO DATA; |
| 88 | +
|
| 89 | +REFRESH MATERIALIZED VIEW geokrety.gk_geokrety_in_caches; |
| 90 | +
|
| 91 | +CREATE VIEW geokrety.gk_geokrety_near_users_homes |
| 92 | + AS SELECT c_user.id AS c_user_id, |
| 93 | + c_user.username AS c_username, |
| 94 | + gk_geokrety_in_caches.id, |
| 95 | + gk_geokrety_in_caches.gkid, |
| 96 | + gk_geokrety_in_caches.tracking_code, |
| 97 | + gk_geokrety_in_caches.name, |
| 98 | + gk_geokrety_in_caches.mission, |
| 99 | + gk_geokrety_in_caches.owner, |
| 100 | + gk_geokrety_in_caches.distance, |
| 101 | + gk_geokrety_in_caches.caches_count, |
| 102 | + gk_geokrety_in_caches.pictures_count, |
| 103 | + gk_geokrety_in_caches.last_position, |
| 104 | + gk_geokrety_in_caches.last_log, |
| 105 | + gk_geokrety_in_caches.holder, |
| 106 | + gk_geokrety_in_caches.avatar, |
| 107 | + gk_geokrety_in_caches.created_on_datetime, |
| 108 | + gk_geokrety_in_caches.updated_on_datetime, |
| 109 | + gk_geokrety_in_caches.missing, |
| 110 | + gk_geokrety_in_caches.type, |
| 111 | + gk_geokrety_in_caches."position", |
| 112 | + gk_geokrety_in_caches.lat, |
| 113 | + gk_geokrety_in_caches.lon, |
| 114 | + gk_geokrety_in_caches.waypoint, |
| 115 | + gk_geokrety_in_caches.elevation, |
| 116 | + gk_geokrety_in_caches.country, |
| 117 | + gk_geokrety_in_caches.move_type, |
| 118 | + gk_geokrety_in_caches.author, |
| 119 | + gk_geokrety_in_caches.moved_on_datetime, |
| 120 | + gk_geokrety_in_caches.author_username, |
| 121 | + gk_geokrety_in_caches.owner_username, |
| 122 | + gk_geokrety_in_caches.avatar_key, |
| 123 | + st_distance(gk_geokrety_in_caches."position", coords2position(c_user.home_latitude, c_user.home_longitude)) / 1000::double precision AS home_distance |
| 124 | + FROM gk_geokrety_in_caches, |
| 125 | + gk_users c_user |
| 126 | + WHERE st_dwithin(gk_geokrety_in_caches."position", coords2position(c_user.home_latitude, c_user.home_longitude), (c_user.observation_area * 1000)::double precision) |
| 127 | + ORDER BY (st_distance(gk_geokrety_in_caches."position", coords2position(c_user.home_latitude, c_user.home_longitude)) < (c_user.observation_area * 1000)::double precision); |
| 128 | +COMMIT; |
| 129 | +SQL |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments