[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。
相關文章
相關標籤/搜索