首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从php代码运行原理迁移?

如何从php代码运行原理迁移?
EN

Stack Overflow用户
提问于 2018-01-26 01:45:22
回答 1查看 484关注 0票数 1

我正在尝试将数据库迁移集成到我的项目中。我的项目是一个由插件驱动的php框架的插件(每个插件都有自己的迁移脚本)。通常(或总是...)这些迁移脚本表示为.sql脚本。框架在安装/卸载过程中应用这些脚本。

因此,我想在我的插件中使用另一种迁移方式:doctrine-migrations

因为迁移的执行是由框架从代码中调用的,所以我也想从代码中调用原理。但是..。没有如何做到这一点的教程:http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/

如何以最小的配置调用来自php代码的原理迁移?

EN

回答 1

Stack Overflow用户

发布于 2019-12-05 04:59:49

我对一些Wordpress插件使用Doctrine Migrations (2.2.0),并从如下代码中调用它们:

代码语言:javascript
复制
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块中可能是明智的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48448979

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档