From 43a378bed6f2d8298c9021ecaddfd62c5f8e8057 Mon Sep 17 00:00:00 2001 From: David Molineus Date: Tue, 27 Jan 2015 00:02:38 +0100 Subject: [PATCH] Add migrate script for the coordinates. --- composer.json | 5 ++- runonce/migrate.php | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 runonce/migrate.php diff --git a/composer.json b/composer.json index b26c527..3c1fa60 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,10 @@ "prefix": "core-", "languages_cto": "module/languages", "languages_tx": ".tx" - } + }, + "runonce": [ + "runonce/migrate.php" + ] } } } diff --git a/runonce/migrate.php b/runonce/migrate.php new file mode 100644 index 0000000..4a91026 --- /dev/null +++ b/runonce/migrate.php @@ -0,0 +1,79 @@ +fieldExists('coordinates', 'tl_leaflet_marker')) { + $this->createFields($database); + $this->convertCoordinates($database); + } + } + + /** + * @param $database + * + * @param \Database $database The database connection. + * + * @return array + */ + protected function createFields(\Database $database) + { + foreach (array('latitude', 'longitude', 'altitude') as $field) { + if (!$database->fieldExists('latitude', 'tl_leaflet_marker')) { + $database->execute(sprintf('ALTER TABLE tl_leaflet_marker ADD %s float NULL;', $field)); + } + } + + } + + /** + * Convert coordinates to new splitted fields. + * + * @param \Database $database The database connection. + * + * @return void + */ + private function convertCoordinates(\Database $database) + { + $query = << '' AND latitude IS NULL aND longitude IS NULL AND altitude IS NULL +SQL; + + $result = $database->query($query); + + while ($result->next()) { + list($latitude, $longitude, $altitude) = trimsplit(',', $result->coordinates); + + $database + ->prepare('UPDATE tl_leaflet_marker %s WHERE id=?') + ->set( + array( + 'latitude' => $latitude, + 'longitude' => $longitude, + 'altitude' => $altitude + ) + ) + ->execute($result->id); + } + } +} + +$controller = new migrate(); +$controller->execute();