我正在尝试将数据库迁移集成到我的项目中。我的项目是一个由插件驱动的php框架的插件(每个插件都有自己的迁移脚本)。通常(或总是...)这些迁移脚本表示为.sql脚本。框架在安装/卸载过程中应用这些脚本。
因此,我想在我的插件中使用另一种迁移方式:doctrine-migrations。
因为迁移的执行是由框架从代码中调用的,所以我也想从代码中调用原理。但是..。没有如何做到这一点的教程:http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/
如何以最小的配置调用来自php代码的原理迁移?
发布于 2019-12-05 04:59:49
我对一些Wordpress插件使用Doctrine Migrations (2.2.0),并从如下代码中调用它们:
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\Migrations\Configuration\Configuration;
use Doctrine\Migrations\Tools\Console\Command\MigrateCommand;
use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
$connection = DriverManager::getConnection([
'dbname' => 'migrations_docs_example',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
]);
$configuration = new Configuration($connection);
$configuration->setName('My Doctrine Migrations');
$configuration->setMigrationsDirectory(__DIR__ . '/database/migrations');
$configuration->setMigrationsNamespace('database\migrations');
$configuration->setMigrationsTableName('doctrine_migration_versions');
$helperSet = new HelperSet();
$helperSet->set(new ConnectionHelper($connection), 'db');
$helperSet->set(new ConfigurationHelper($connection, $configuration));
$command = new MigrateCommand();
$command->setHelperSet($helperSet);
$input = new ArrayInput([]);
$input->setInteractive(false);
$output = new BufferedOutput();
$exitcode = $command->run($input, $output);
if ($exitcode > 0) {
throw new Exception($output->fetch());
}现在,如果出现严重错误,迁移会抛出异常,因此将其包装在try-catch块中可能是明智的。
https://stackoverflow.com/questions/48448979
复制相似问题