addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(64)'); $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT'); $this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS NULL'); } public function getDescription(): string { return 'Upgrade phonenumber on third parties'; } public function up(Schema $schema): void { $carrierCode = getenv('DEFAULT_CARRIER_CODE'); // Directly from the .env if (!$carrierCode) { throw new \RuntimeException('Environment variable DEFAULT_CARRIER_CODE is not set.'); } $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(35)'); $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone DROP DEFAULT'); $this->addSql('ALTER TABLE chill_3party.third_party ALTER telephone TYPE VARCHAR(35)'); $this->addSql('COMMENT ON COLUMN chill_3party.third_party.telephone IS \'(DC2Type:phone_number)\''); $this->addSql( 'UPDATE chill_3party.third_party SET '. $this->buildMigrationPhonenumberClause($carrierCode, 'telephone') ); } private function buildMigrationPhoneNumberClause(string $defaultCarriercode, string $field): string { $util = PhoneNumberUtil::getInstance(); $countryCode = $util->getCountryCodeForRegion($defaultCarriercode); return sprintf('%s=CASE WHEN %s = \'\' THEN NULL WHEN LEFT(%s, 1) = \'0\' THEN \'+%s\' || replace(replace(substr(%s, 2), \'(0)\', \'\'), \' \', \'\') ELSE replace(replace(%s, \'(0)\', \'\'),\' \', \'\') END', $field, $field, $field, $countryCode, $field, $field); } }