From 26fd61a3ed4112ac9b67e9049c91230d6c781678 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 20 Oct 2018 09:00:29 +0200 Subject: [PATCH 1/4] Created migrations to rename camel case columns to snake case --- build.sh | 1 + data/migrations/Version20181020060559.php | 68 +++++++++++++++++++++++ data/migrations/Version20181020065148.php | 41 ++++++++++++++ data/migrations_template.txt | 23 ++++++++ migrations.yml | 1 + 5 files changed, 134 insertions(+) create mode 100644 data/migrations/Version20181020060559.php create mode 100644 data/migrations/Version20181020065148.php create mode 100644 data/migrations_template.txt diff --git a/build.sh b/build.sh index 8cf290e3..ea502d69 100755 --- a/build.sh +++ b/build.sh @@ -18,6 +18,7 @@ rm -rf "${builtcontent}" mkdir -p "${builtcontent}" rsync -av * "${builtcontent}" \ --exclude=data/infra \ + --exclude=data/migrations_template.txt \ --exclude=**/.gitignore \ --exclude=CHANGELOG.md \ --exclude=composer.lock \ diff --git a/data/migrations/Version20181020060559.php b/data/migrations/Version20181020060559.php new file mode 100644 index 00000000..0e2c862d --- /dev/null +++ b/data/migrations/Version20181020060559.php @@ -0,0 +1,68 @@ + 'country_code', + 'countryName' => 'country_name', + 'regionName' => 'region_name', + 'cityName' => 'city_name', + ]; + + /** + * @param Schema $schema + * @throws SchemaException + */ + public function up(Schema $schema): void + { + $this->createColumns($schema->getTable('visit_locations'), self::COLUMNS); + } + + private function createColumns(Table $visitLocations, array $columnNames): void + { + foreach ($columnNames as $name) { + if (! $visitLocations->hasColumn($name)) { + $visitLocations->addColumn($name, Type::STRING, ['notnull' => false]); + } + } + } + + /** + * @throws SchemaException + * @throws DBALException + */ + public function postUp(Schema $schema): void + { + $visitLocations = $schema->getTable('visit_locations'); + + // If the camel case columns do not exist, do nothing + if (! $visitLocations->hasColumn('countryCode')) { + return; + } + + $qb = $this->connection->createQueryBuilder(); + $qb->update('visit_locations'); + foreach (self::COLUMNS as $camelCaseName => $snakeCaseName) { + $qb->set($snakeCaseName, $camelCaseName); + } + $qb->execute(); + } + + public function down(Schema $schema): void + { + // No down + } +} diff --git a/data/migrations/Version20181020065148.php b/data/migrations/Version20181020065148.php new file mode 100644 index 00000000..709c672f --- /dev/null +++ b/data/migrations/Version20181020065148.php @@ -0,0 +1,41 @@ +getTable('visit_locations'); + + foreach (self::CAMEL_CASE_COLUMNS as $name) { + if ($visitLocations->hasColumn($name)) { + $visitLocations->dropColumn($name); + } + } + } + + public function down(Schema $schema): void + { + // No down + } +} diff --git a/data/migrations_template.txt b/data/migrations_template.txt new file mode 100644 index 00000000..41c4a8c8 --- /dev/null +++ b/data/migrations_template.txt @@ -0,0 +1,23 @@ +; + +use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; + +/** + * Auto-generated Migration: Please modify to your needs! + */ +final class Version extends AbstractMigration +{ + public function up(Schema $schema): void + { + + } + + public function down(Schema $schema): void + { + + } +} diff --git a/migrations.yml b/migrations.yml index e732a0dc..db3a57b3 100644 --- a/migrations.yml +++ b/migrations.yml @@ -2,3 +2,4 @@ name: ShlinkMigrations migrations_namespace: ShlinkMigrations table_name: migrations migrations_directory: data/migrations +custom_template: data/migrations_template.txt From 55e021ba20c229de174b35a62831610c9f36fcc7 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 20 Oct 2018 09:02:50 +0200 Subject: [PATCH 2/4] Added snake case column names to VisitLocation entity --- module/Core/src/Entity/VisitLocation.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/module/Core/src/Entity/VisitLocation.php b/module/Core/src/Entity/VisitLocation.php index f00a5db6..af75b8d6 100644 --- a/module/Core/src/Entity/VisitLocation.php +++ b/module/Core/src/Entity/VisitLocation.php @@ -19,37 +19,37 @@ class VisitLocation extends AbstractEntity implements ArraySerializableInterface { /** * @var string - * @ORM\Column(nullable=true) + * @ORM\Column(nullable=true, name="country_code") */ private $countryCode; /** * @var string - * @ORM\Column(nullable=true) + * @ORM\Column(nullable=true, name="country_name") */ private $countryName; /** * @var string - * @ORM\Column(nullable=true) + * @ORM\Column(nullable=true, name="region_name") */ private $regionName; /** * @var string - * @ORM\Column(nullable=true) + * @ORM\Column(nullable=true, name="city_name") */ private $cityName; /** * @var string - * @ORM\Column(nullable=true) + * @ORM\Column(nullable=true, name="latitude") */ private $latitude; /** * @var string - * @ORM\Column(nullable=true) + * @ORM\Column(nullable=true, name="longitude") */ private $longitude; /** * @var string - * @ORM\Column(nullable=true) + * @ORM\Column(nullable=true, name="timezone") */ private $timezone; From 13c64b0db034845cf54498f95c9ce5089ff2b6c2 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 20 Oct 2018 09:06:07 +0200 Subject: [PATCH 3/4] Fixed coding styles --- data/migrations/Version20181020065148.php | 1 - 1 file changed, 1 deletion(-) diff --git a/data/migrations/Version20181020065148.php b/data/migrations/Version20181020065148.php index 709c672f..432dded8 100644 --- a/data/migrations/Version20181020065148.php +++ b/data/migrations/Version20181020065148.php @@ -5,7 +5,6 @@ namespace ShlinkMigrations; use Doctrine\DBAL\Schema\Schema; use Doctrine\DBAL\Schema\SchemaException; -use Doctrine\DBAL\Schema\Table; use Doctrine\Migrations\AbstractMigration; /** From cd58855e1fddf18707236310d35c71daf9ba66d4 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 20 Oct 2018 09:08:50 +0200 Subject: [PATCH 4/4] Updated changelog --- CHANGELOG.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ac31f85..08a977ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,28 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org). +## [Unreleased] + +#### Added + +* *Nothing* + +#### Changed + +* [#241](https://github.com/shlinkio/shlink/issues/241) Fixed columns in `visit_locations` table, to be snake_case instead of camelCase. + +#### Deprecated + +* *Nothing* + +#### Removed + +* *Nothing* + +#### Fixed + +* *Nothing* + ## 1.13.2 - 2018-10-18