php 在Doctrine 2中以编程方式执行迁移

szqfcxe2  于 2023-03-11  发布在  PHP
关注(0)|答案(3)|浏览(128)

假设我有一个到Doctrine迁移类的路径,我如何在Doctrine2中以编程方式执行迁移?
我认为应该有一种干净的方法来通过API执行迁移,因为它可以通过Doctrine的早期版本完成,如下所述:http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/migrations.html

velaa5lx

velaa5lx1#

由于我没有看到任何答案,我将尝试提供我的解决方案,用于在原则中以编程方式(使用代码)执行迁移。

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\MigratorConfiguration;
use Doctrine\Migrations\Provider\SchemaProvider;
use Doctrine\Migrations\Version\Direction;

// connection
$dbParams = [
    'dbname' => 'database-name',
    'user' => 'database-username',
    'password' => 'database-password',
    'host' => 'hostname',
    'port' => 'port',
    'driver' => 'pdo_mysql',
];
try {
    $connection = DriverManager::getConnection($dbParams);
} catch (\Doctrine\DBAL\Exception $e) {
    echo 'Problem connecting to DB. '.$e->getMessage();
    die();
}

// configuration - Be careful what namespace you use
$configuration = new Configuration($connection);
$configuration->addMigrationsDirectory('MyAppNamespace\Migrations', __DIR__ . '/../migrations');
// we want the execution of the migration to make changes to the table doctrine_migration_versions - so the system is aware that executed the migration
$storageConfiguration = new TableMetadataStorageConfiguration();
$storageConfiguration->setTableName('doctrine_migration_versions');
$configuration->setMetadataStorageConfiguration($storageConfiguration);

$dependencyFactory = DependencyFactory::fromConnection(
    new ExistingConfiguration($configuration),
    new ExistingConnection($connection))
);
$planCalculator = $dependencyFactory->getMigrationPlanCalculator();

// which migration to execute / I assume latest /
$latestMigrationVersion = $dependencyFactory->getVersionAliasResolver()->resolveVersionAlias('latest');

// check if we have at least one migration version to execute
if(!$latestMigrationVersion->equals(new Version(0))){ 
    try {
        // so we will execute only latest ONE migration, if you need more, just find a way to list them in the first parameter of method getPlanForVersions()
        $planUp = $planCalculator->getPlanForVersions(
            [$latestMigrationVersion],
            Direction::UP
        );
        $dependencyFactory->getMetadataStorage()->ensureInitialized();

        // do the migration
        $dependencyFactory->getMigrator()->migrate($planUp, (new MigratorConfiguration())->setAllOrNothing(false));
    }catch (Exception $e){
        echo 'There were problems during db-migration.'."\n".$e->getMessage()."\n\n";
    }
}

希望它能帮助另一个开发人员快速启动他的原型。我试着详细说明代码,这样人们就不会浪费时间去弄清楚每一个依赖项。

wsewodh2

wsewodh22#

如果您在Symfony框架中使用Doctrine,并在寻找从命令外部运行迁移的方法时发现了这个问题,请记住,您可以通过自动装配访问已经配置的DependencyFactory。

services:
    Doctrine\Migrations\DependencyFactory:
        alias: 'doctrine.migrations.dependency_factory'

和往常一样自动接线:

public function someControllerAction(DependencyFactory $df): Response {

好的。
也就是说,在正常操作期间,您很可能会使用没有管理权限的受限用户连接数据库,遵循最低权限原则。如果您这样做,从应用运行迁移将失败。如果您不这样做,请考虑这样做。
我发现上面的代码对于在健康检查端点响应中包含迁移信息非常有用,但不一定要运行迁移。

bq3bfh9z

bq3bfh9z3#

假设您使用的是Symfony的DoctrineMigrationsBundle
要迁移到最新的可用版本,请使用用途:doctrine:migrations:migrate命令。
以下是更多可用命令。

相关问题