. */ namespace Application\Migrations; use Doctrine\DBAL\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; class VersionAuthserverExternalAccount20171020220239 extends AbstractMigration { public function up(Schema $schema) { $condition = 'WHERE ea1.id > ea2.id AND ea1.provider = ea2.provider AND ea1.provider_ref = ea2.provider_ref'; $duplicates = $this->connection->fetchAll('SELECT DISTINCT ea1.id AS id FROM vierbergenlars_external_account_external_user AS ea1 JOIN vierbergenlars_external_account_external_user ea2 ' . $condition); $duplicateIds = array_map(function ($row) { return $row['id']; }, $duplicates); $this->connection->executeUpdate('DELETE FROM vierbergenlars_external_account_external_user WHERE id IN(' . implode(',', $duplicateIds) . ')'); } public function down(Schema $schema) { $this->throwIrreversibleMigrationException('Duplicate external account references have been cleared.'); } }