自定義命令腳本
目錄結構
目前的項目結構是這樣的(參照代碼庫):
其中,db/migrations文件夾是遷移類文件夾,config/db.php是我們項目原有的db配置,migrations.php和migrations-db.php是遷移組件需要的配置文件。
編寫自定義命令腳本
現在先在根目錄新建文件:migrate,沒有後綴名,並且添加可執行權限。
並且參照組件原有的命令腳本vendor/doctrine/migrations/doctrine-migrations.php,首先獲取項目原有的數據庫配置信息,替換掉migrations-db.php數據庫配置文件:
$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配置文件:
$configuration = new Configuration($connection);
$configuration->setName('Doctrine Migrations');
$configuration->setMigrationsNamespace('db\migrations');
$configuration->setMigrationsTableName('migration_versions');
$configuration->setMigrationsDirectory('db/migrations');
最後是完整的命令腳本代碼:
#!/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.php和migrations-db.php這兩個配置文件也可以刪除了。
PhpStorm集成遷移命令
如果你使用PhpStorm,命令行還可以集成到開發工具中去,會有自動提示,大大提高工作效率:
PhpStorm->Preferences->Command Line Tool Support-> 添加- Choose tool選擇Tool based on Symfony Console,點擊OK
- Alias輸入m, Path to PHP executable 選擇php路徑,path to script選擇跟目錄下的migrate文件
- 點擊OK,提示 Found 9 commands 則配置成功。
PhpStorm->Tools->Run Command,彈出命令窗口,輸入m即會出現命令提示。
結語
到此,數據遷移組件就已經很靈活的集成到我們自己的項目中了,而且你還可以根據自己的項目靈活修改自定義命令腳本文件。
但是如果使用久了會發現現在的數據遷移會有兩個問題:
-
不支持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.
所以只能添加自定義類型。 - 另外一個問題,就是不支持enum類型。深入代碼之後,發現enum類型的特殊格式並不好融入到組件中去,自定義類型也不能支持,但是如果你是半途中加入數據遷移組件,現在項目中如果已經有了數據表結構,且包含enum類型的字段,那麼使用遷移組件是會報錯。
那麼,下一章我們就來完成這兩件事:添加tinyint的自定義類型,解決enum報錯的問題。
在我的代碼庫可以查看這篇文章的詳細代碼,歡迎star。