Version20200110182849.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. use function Functional\each;
  7. use function Functional\partial_left;
  8. final class Version20200110182849 extends AbstractMigration
  9. {
  10. private const DEFAULT_EMPTY_VALUE = '';
  11. private const COLUMN_DEFAULTS_MAP = [
  12. 'visits' => [
  13. 'referer',
  14. 'user_agent',
  15. ],
  16. 'visit_locations' => [
  17. 'timezone',
  18. 'country_code',
  19. 'country_name',
  20. 'region_name',
  21. 'city_name',
  22. ],
  23. ];
  24. public function up(Schema $schema): void
  25. {
  26. each(
  27. self::COLUMN_DEFAULTS_MAP,
  28. fn (array $columns, string $tableName) =>
  29. each($columns, partial_left([$this, 'setDefaultValueForColumnInTable'], $tableName)),
  30. );
  31. }
  32. public function setDefaultValueForColumnInTable(string $tableName, string $columnName): void
  33. {
  34. $qb = $this->connection->createQueryBuilder();
  35. $qb->update($tableName)
  36. ->set($columnName, ':emptyValue')
  37. ->setParameter('emptyValue', self::DEFAULT_EMPTY_VALUE)
  38. ->where($qb->expr()->isNull($columnName))
  39. ->execute();
  40. }
  41. public function down(Schema $schema): void
  42. {
  43. // No need (and no way) to undo this migration
  44. }
  45. /**
  46. * @fixme Workaround for https://github.com/doctrine/migrations/issues/1104
  47. */
  48. public function isTransactional(): bool
  49. {
  50. return false;
  51. }
  52. }