Version20200105165647.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Types\Types;
  7. use Doctrine\Migrations\AbstractMigration;
  8. use function Functional\some;
  9. final class Version20200105165647 extends AbstractMigration
  10. {
  11. private const COLUMNS = ['lat' => 'latitude', 'lon' => 'longitude'];
  12. /**
  13. * @throws Exception
  14. */
  15. public function preUp(Schema $schema): void
  16. {
  17. $visitLocations = $schema->getTable('visit_locations');
  18. $this->skipIf(some(
  19. self::COLUMNS,
  20. fn (string $v, string $newColName) => $visitLocations->hasColumn($newColName),
  21. ), 'New columns already exist');
  22. foreach (self::COLUMNS as $columnName) {
  23. $qb = $this->connection->createQueryBuilder();
  24. $qb->update('visit_locations')
  25. ->set($columnName, ':zeroValue')
  26. ->where($qb->expr()->orX(
  27. $qb->expr()->eq($columnName, ':emptyString'),
  28. $qb->expr()->isNull($columnName),
  29. ))
  30. ->setParameters([
  31. 'zeroValue' => '0',
  32. 'emptyString' => '',
  33. ])
  34. ->execute();
  35. }
  36. }
  37. /**
  38. * @throws Exception
  39. */
  40. public function up(Schema $schema): void
  41. {
  42. $visitLocations = $schema->getTable('visit_locations');
  43. foreach (self::COLUMNS as $newName => $oldName) {
  44. $visitLocations->addColumn($newName, Types::FLOAT, [
  45. 'default' => '0.0',
  46. ]);
  47. }
  48. }
  49. /**
  50. * @throws Exception
  51. */
  52. public function postUp(Schema $schema): void
  53. {
  54. $platformName = $this->connection->getDatabasePlatform()->getName();
  55. $castType = $platformName === 'postgres' ? 'DOUBLE PRECISION' : 'DECIMAL(9,2)';
  56. foreach (self::COLUMNS as $newName => $oldName) {
  57. $qb = $this->connection->createQueryBuilder();
  58. $qb->update('visit_locations')
  59. ->set($newName, 'CAST(' . $oldName . ' AS ' . $castType . ')')
  60. ->execute();
  61. }
  62. }
  63. public function preDown(Schema $schema): void
  64. {
  65. foreach (self::COLUMNS as $newName => $oldName) {
  66. $qb = $this->connection->createQueryBuilder();
  67. $qb->update('visit_locations')
  68. ->set($oldName, $newName)
  69. ->execute();
  70. }
  71. }
  72. /**
  73. * @throws Exception
  74. */
  75. public function down(Schema $schema): void
  76. {
  77. $visitLocations = $schema->getTable('visit_locations');
  78. foreach (self::COLUMNS as $colName => $oldName) {
  79. $visitLocations->dropColumn($colName);
  80. }
  81. }
  82. /**
  83. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  84. */
  85. public function isTransactional(): bool
  86. {
  87. return false;
  88. }
  89. }