From 99ce1fcfd6d06ab3f862f83466f22ba9d50961e0 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Wed, 8 Feb 2023 10:45:25 -0800 Subject: [PATCH] map: add migration --- lib/Db/TimelineQueryDays.php | 6 +- lib/Db/TimelineQueryFilters.php | 8 +- lib/Db/TimelineWrite.php | 49 ++++++------ .../Version401100Date20230208181533.php | 79 +++++++++++++++++++ 4 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 lib/Migration/Version401100Date20230208181533.php diff --git a/lib/Db/TimelineQueryDays.php b/lib/Db/TimelineQueryDays.php index 6a8e166c..8c6bbd2e 100644 --- a/lib/Db/TimelineQueryDays.php +++ b/lib/Db/TimelineQueryDays.php @@ -206,8 +206,8 @@ trait TimelineQueryDays $query = $this->connection->getQueryBuilder(); // Get the average location of each cluster - $avgLat = $query->createFunction('AVG(latitude) AS avgLat'); - $avgLng = $query->createFunction('AVG(longitude) AS avgLng'); + $avgLat = $query->createFunction('AVG(lat) AS avgLat'); + $avgLng = $query->createFunction('AVG(lon) AS avgLng'); $count = $query->createFunction('COUNT(*) AS count'); $query->select($avgLat, $avgLng, $count) ->from('memories', 'm') @@ -217,7 +217,7 @@ trait TimelineQueryDays $query = $this->joinFilecache($query, $root, $recursive, $archive); // Group by cluster - $groupFunction = $query->createFunction('latitude DIV '.$boxSize.', longitude DIV '.$boxSize); + $groupFunction = $query->createFunction('lat DIV '.$boxSize.', lon DIV '.$boxSize); $query->groupBy($groupFunction); // Apply all transformations (including map bounds) diff --git a/lib/Db/TimelineQueryFilters.php b/lib/Db/TimelineQueryFilters.php index 7cc8ea64..7e4bebd9 100644 --- a/lib/Db/TimelineQueryFilters.php +++ b/lib/Db/TimelineQueryFilters.php @@ -44,10 +44,10 @@ trait TimelineQueryFilters { $query->andWhere( $query->expr()->andX( - $query->expr()->gte('m.latitude', $query->createNamedParameter($minLat, IQueryBuilder::PARAM_STR)), - $query->expr()->lte('m.latitude', $query->createNamedParameter($maxLat, IQueryBuilder::PARAM_STR)), - $query->expr()->gte('m.longitude', $query->createNamedParameter($minLng, IQueryBuilder::PARAM_STR)), - $query->expr()->lte('m.longitude', $query->createNamedParameter($maxLng, IQueryBuilder::PARAM_STR)) + $query->expr()->gte('m.lat', $query->createNamedParameter($minLat, IQueryBuilder::PARAM_STR)), + $query->expr()->lte('m.lat', $query->createNamedParameter($maxLat, IQueryBuilder::PARAM_STR)), + $query->expr()->gte('m.lon', $query->createNamedParameter($minLng, IQueryBuilder::PARAM_STR)), + $query->expr()->lte('m.lon', $query->createNamedParameter($maxLng, IQueryBuilder::PARAM_STR)) ) ); } diff --git a/lib/Db/TimelineWrite.php b/lib/Db/TimelineWrite.php index 2c0933f2..10cc011a 100644 --- a/lib/Db/TimelineWrite.php +++ b/lib/Db/TimelineWrite.php @@ -158,6 +158,8 @@ class TimelineWrite } // Store location data + $lat = null; + $lon = null; if (\array_key_exists('GPSLatitude', $exif) && \array_key_exists('GPSLongitude', $exif)) { try { $lat = (float) $exif['GPSLatitude']; @@ -169,40 +171,39 @@ class TimelineWrite } } + // Parameters for insert or update + $params = [ + 'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), + 'objectid' => $query->createNamedParameter((string) $fileId, IQueryBuilder::PARAM_STR), + 'dayid' => $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT), + 'datetaken' => $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR), + 'mtime' => $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT), + 'isvideo' => $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT), + 'video_duration' => $query->createNamedParameter($videoDuration, IQueryBuilder::PARAM_INT), + 'w' => $query->createNamedParameter($w, IQueryBuilder::PARAM_INT), + 'h' => $query->createNamedParameter($h, IQueryBuilder::PARAM_INT), + 'exif' => $query->createNamedParameter($exifJson, IQueryBuilder::PARAM_STR), + 'liveid' => $query->createNamedParameter($liveid, IQueryBuilder::PARAM_STR), + 'lat' => $query->createNamedParameter($lat, IQueryBuilder::PARAM_STR), + 'lon' => $query->createNamedParameter($lon, IQueryBuilder::PARAM_STR), + ]; + if ($prevRow) { // Update existing row // No need to set objectid again $query->update('memories') - ->set('dayid', $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT)) - ->set('datetaken', $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR)) - ->set('mtime', $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT)) - ->set('isvideo', $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT)) - ->set('video_duration', $query->createNamedParameter($videoDuration, IQueryBuilder::PARAM_INT)) - ->set('w', $query->createNamedParameter($w, IQueryBuilder::PARAM_INT)) - ->set('h', $query->createNamedParameter($h, IQueryBuilder::PARAM_INT)) - ->set('exif', $query->createNamedParameter($exifJson, IQueryBuilder::PARAM_STR)) - ->set('liveid', $query->createNamedParameter($liveid, IQueryBuilder::PARAM_STR)) ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) ; + foreach ($params as $key => $value) { + if ('objectid' !== $key && 'fileid' !== $key) { + $query->set($key, $value); + } + } $query->executeStatement(); } else { // Try to create new row try { - $query->insert('memories') - ->values([ - 'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), - 'objectid' => $query->createNamedParameter((string) $fileId, IQueryBuilder::PARAM_STR), - 'dayid' => $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT), - 'datetaken' => $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR), - 'mtime' => $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT), - 'isvideo' => $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT), - 'video_duration' => $query->createNamedParameter($videoDuration, IQueryBuilder::PARAM_INT), - 'w' => $query->createNamedParameter($w, IQueryBuilder::PARAM_INT), - 'h' => $query->createNamedParameter($h, IQueryBuilder::PARAM_INT), - 'exif' => $query->createNamedParameter($exifJson, IQueryBuilder::PARAM_STR), - 'liveid' => $query->createNamedParameter($liveid, IQueryBuilder::PARAM_STR), - ]) - ; + $query->insert('memories')->values($params); $query->executeStatement(); } catch (\Exception $ex) { error_log('Failed to create memories record: '.$ex->getMessage()); diff --git a/lib/Migration/Version401100Date20230208181533.php b/lib/Migration/Version401100Date20230208181533.php new file mode 100644 index 00000000..98486b49 --- /dev/null +++ b/lib/Migration/Version401100Date20230208181533.php @@ -0,0 +1,79 @@ + + * @author Your name + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OCA\Memories\Migration; + +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +/** + * Auto-generated migration step: Please modify to your needs! + */ +class Version401100Date20230208181533 extends SimpleMigrationStep +{ + /** + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + */ + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void + { + } + + /** + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options): ?ISchemaWrapper + { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $table = $schema->getTable('memories'); + + if (!$table->hasColumn('lat')) { + $table->addColumn('lat', Types::DECIMAL, [ + 'notnull' => false, + 'default' => null, + 'precision' => 8, + 'scale' => 6, + ]); + $table->addColumn('lon', Types::DECIMAL, [ + 'notnull' => false, + 'default' => null, + 'precision' => 9, + 'scale' => 6, + ]); + + $table->addIndex(['lat', 'lon'], 'memories_lat_lon_index'); + } + + return $schema; + } + + /** + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + */ + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void + { + } +}