Version20181020060559.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkMigrations;
  4. use Doctrine\DBAL\Exception;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\DBAL\Schema\SchemaException;
  7. use Doctrine\DBAL\Schema\Table;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\Migrations\AbstractMigration;
  10. /**
  11. * Auto-generated Migration: Please modify to your needs!
  12. */
  13. final class Version20181020060559 extends AbstractMigration
  14. {
  15. private const COLUMNS = [
  16. 'countryCode' => 'country_code',
  17. 'countryName' => 'country_name',
  18. 'regionName' => 'region_name',
  19. 'cityName' => 'city_name',
  20. ];
  21. /**
  22. * @throws SchemaException
  23. */
  24. public function up(Schema $schema): void
  25. {
  26. $this->createColumns($schema->getTable('visit_locations'), self::COLUMNS);
  27. }
  28. private function createColumns(Table $visitLocations, array $columnNames): void
  29. {
  30. foreach ($columnNames as $name) {
  31. if (! $visitLocations->hasColumn($name)) {
  32. $visitLocations->addColumn($name, Types::STRING, ['notnull' => false]);
  33. }
  34. }
  35. }
  36. /**
  37. * @throws SchemaException
  38. * @throws Exception
  39. */
  40. public function postUp(Schema $schema): void
  41. {
  42. $visitLocations = $schema->getTable('visit_locations');
  43. // If the camel case columns do not exist, do nothing
  44. if (! $visitLocations->hasColumn('countryCode')) {
  45. return;
  46. }
  47. $qb = $this->connection->createQueryBuilder();
  48. $qb->update('visit_locations');
  49. foreach (self::COLUMNS as $camelCaseName => $snakeCaseName) {
  50. $qb->set($snakeCaseName, $camelCaseName);
  51. }
  52. $qb->execute();
  53. }
  54. public function down(Schema $schema): void
  55. {
  56. // No down
  57. }
  58. /**
  59. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  60. */
  61. public function isTransactional(): bool
  62. {
  63. return false;
  64. }
  65. }