[Doctrine Migrations] 数据库迁移组件的深刻解析二:自定义集成

自定义命令脚本

目录结构

目前的项目结构是这样的(参照代码库):php

file

其中,db/migrations文件夹是迁移类文件夹,config/db.php是咱们项目原有的db配置,migrations.phpmigrations-db.php是迁移组件须要的配置文件。html

编写自定义命令脚本

如今先在根目录新建文件:migrate,没有后缀名,而且添加可执行权限。mysql

而且参照组件原有的命令脚本vendor/doctrine/migrations/doctrine-migrations.php,首先获取项目原有的数据库配置信息,替换掉migrations-db.php数据库配置文件:git

$db_config = include 'config/db.php';
$db_params = [
    'driver' => 'pdo_mysql',
    'host' => $db_config['host'],
    'port' => $db_config['port'],
    'dbname' => $db_config['dbname'],
    'user' => $db_config['user'],
    'password' => $db_config['password'],
];
try {
    $connection = DriverManager::getConnection($db_params);
} catch (DBALException $e) {
    echo $e->getMessage() . PHP_EOL;
    exit;
}

而后配置组件,替换掉migrations.php配置文件:github

$configuration = new Configuration($connection);
$configuration->setName('Doctrine Migrations');
$configuration->setMigrationsNamespace('db\migrations');
$configuration->setMigrationsTableName('migration_versions');
$configuration->setMigrationsDirectory('db/migrations');

最后是完整的命令脚本代码:sql

#!/usr/bin/env php
<?php
require_once 'vendor/autoload.php';
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Tools\Console\ConsoleRunner;
use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
// 读取数据库配置信息
$db_config = include 'config/db.php';
$db_params = [
    'driver' => 'pdo_mysql',
    'host' => $db_config['host'],
    'port' => $db_config['port'],
    'dbname' => $db_config['dbname'],
    'user' => $db_config['user'],
    'password' => $db_config['password'],
];
try {
    $connection = DriverManager::getConnection($db_params);
} catch (DBALException $e) {
    echo $e->getMessage() . PHP_EOL;
    exit;
}
// 迁移组件配置
$configuration = new Configuration($connection);
$configuration->setName('Doctrine Migrations');
$configuration->setMigrationsNamespace('db\migrations');
$configuration->setMigrationsTableName('migration_versions');
$configuration->setMigrationsDirectory('db/migrations');
// 建立命令脚本
$helper_set = new HelperSet([
    'question' => new QuestionHelper(),
    'db' => new ConnectionHelper($connection),
    new ConfigurationHelper($connection, $configuration),
]);
$cli = ConsoleRunner::createApplication($helper_set);
try {
    $cli->run();
} catch (Exception $e) {
    echo $e->getMessage() . PHP_EOL;
}

如今执行迁移相关命令时,用./migrate替换以前的vendor/bin/doctrine-migrations部分便可。数据库

同时migrations.phpmigrations-db.php这两个配置文件也能够删除了。segmentfault

PhpStorm集成迁移命令

若是你使用PhpStorm,命令行还能够集成到开发工具中去,会有自动提示,大大提升工做效率:app

  1. PhpStorm -> Preferences -> Command Line Tool Support -> 添加
  2. Choose tool选择Tool based on Symfony Console,点击OK
  3. Alias输入m, Path to PHP executable 选择php路径,path to script选择跟目录下的migrate文件
  4. 点击OK,提示 Found 9 commands 则配置成功。
  5. PhpStorm -> Tools -> Run Command,弹出命令窗口,输入m即会出现命令提示。

结语

到此,数据迁移组件就已经很灵活的集成到咱们本身的项目中了,并且你还能够根据本身的项目灵活修改自定义命令脚本文件。ide

可是若是使用久了会发现如今的数据迁移会有两个问题:

  1. 不支持tinyint类型,在相关issues中,官方开发人员有回复:

    "Tiny integer" is not supported by many database vendors. DBAL's type system is an abstraction layer for SQL types including type conversion from PHP to database value and back. I am assuming that your issue refers to MySQL, where we do not have a distinct native SQL type to use for BooleanType mapping. Therefore MySQL's TINYINT is used for that purpose as is fits best from all available native types and in DBAL we do not have an abstraction for tiny integers as such (as mentioned above).

    What you can do is implement your own tiny integer custom type and tell DBAL to use column comments (for distinction from BooleanType in MySQL). That should work. See the documentation.

    To tell DBAL to use column comments for the custom type, simply override the requiresSQLCommentHint() method to return true.
    因此只能添加自定义类型。

  2. 另一个问题,就是不支持enum类型。深刻代码以后,发现enum类型的特殊格式并很差融入到组件中去,自定义类型也不能支持,可是若是你是半途中加入数据迁移组件,如今项目中若是已经有了数据表结构,且包含enum类型的字段,那么使用迁移组件是会报错。

那么,下一章咱们就来完成这两件事:添加tinyint的自定义类型,解决enum报错的问题。

个人代码库能够查看这篇文章的详细代码,欢迎star。
相关文章
相关标签/搜索