Repository: symfony/symfony
File: src/Symfony/Bridge/Doctrine/SchemaListener/AbstractSchemaListener.php (PHP)

Code before:
            $key = bin2hex(random_bytes(7));
            $table = new Table('_schema_subscriber_check');
            $table->addColumn('id', Types::INTEGER)
                ->setAutoincrement(true)
                ->setNotnull(true);
            $table->addColumn('key', Types::STRING)
                ->setLength(14)
                ->setNotNull(true)
            ;

            if (class_exists(PrimaryKeyConstraint::class)) {
                $table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
            } else {
                $table->setPrimaryKey(['id']);
            }

            try {
                $schemaManager->createTable($table);
            } catch (DatabaseObjectExistsException) {
            }

            $connection->executeStatement('INSERT INTO _schema_subscriber_check (key) VALUES (:key)', ['key' => $key], ['key' => Types::STRING]);

            try {
                $platform = $connection->getDatabasePlatform();
                $exec(\sprintf("DELETE FROM _schema_subscriber_check WHERE %s == %s", $platform->quoteSingleIdentifier('key'), $platform->quoteStringLiteral($key)));
            } catch (DatabaseObjectNotFoundException|ConnectionException) {
            }

            try {
                return !$connection->executeStatement('DELETE FROM _schema_subscriber_check WHERE key == :key', ['key' => $key], ['key' => Types::STRING]);
            } finally {
                if (!$connection->executeQuery('SELECT count(id) FROM _schema_subscriber_check')->fetchOne()) {
                    try {
                        $schemaManager->dropTable('_schema_subscriber_check');
                    } catch (DatabaseObjectNotFoundException) {
                    }
                }
            }
        };
    }
}


Review comment (suggestion):
we need to build the full SQL here as we don't have control about how the closure referenced with `$exec` is shaped, e.g. it can be `PDO::exec()`:https://github.com/symfony/symfony/blob/608e126bcc6fa58eac68d49cff47ab23c913dc47/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php#L189)

Code after:
    {
        return static function (\Closure $exec) use ($connection): bool {
            $schemaManager = method_exists($connection, 'createSchemaManager') ? $connection->createSchemaManager() : $connection->getSchemaManager();
            $key = bin2hex(random_bytes(7));
            $table = new Table('_schema_subscriber_check');
            $table->addColumn('id', Types::INTEGER)
                ->setAutoincrement(true)
                ->setNotnull(true);
            $table->addColumn('random_key', Types::STRING)
                ->setLength(14)
                ->setNotNull(true)
            ;

            if (class_exists(PrimaryKeyConstraint::class)) {
                $table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
            } else {
                $table->setPrimaryKey(['id']);
            }

            try {
                $schemaManager->createTable($table);
            } catch (DatabaseObjectExistsException) {
            }

            $connection->executeStatement('INSERT INTO _schema_subscriber_check (random_key) VALUES (:key)', ['key' => $key], ['key' => Types::STRING]);

            try {
                $exec(\sprintf('DELETE FROM _schema_subscriber_check WHERE random_key = %s', $connection->getDatabasePlatform()->quoteStringLiteral($key)));
            } catch (DatabaseObjectNotFoundException|ConnectionException) {
            }

            try {
                return !$connection->executeStatement('DELETE FROM _schema_subscriber_check WHERE random_key = :key', ['key' => $key], ['key' => Types::STRING]);
            } finally {
                if (!$connection->executeQuery('SELECT count(id) FROM _schema_subscriber_check')->fetchOne()) {
                    try {
                        $schemaManager->dropTable('_schema_subscriber_check');
                    } catch (DatabaseObjectNotFoundException) {
                    }
                }
            }
        };
    }
}