[Doctrine Migrations] 数据库迁移组件的深入解析二:自定义集成
自定义命令脚本
目录结构
目前的项目结构是这样的(参照代码库):

其中,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。
[Doctrine Migrations] 数据库迁移组件的深入解析二:自定义集成的更多相关文章
- [Doctrine Migrations] 数据库迁移组件的深入解析四:集成diff方式迁移组件
场景及优势 熟悉Symfony框架之后,深刻感受到框架集成的ORM组件Doctrine2的强大之处,其中附带的数据迁移也十分方便.Doctrine2是使用Doctrine DBAL组件把代码里面的表结 ...
- [Doctrine Migrations] 数据库迁移组件的深入解析一:安装与使用
场景分析 团队开发中,每个开发人员对于数据库都修改都必须手动记录,上线时需要人工整理,运维成本极高.而且在多个开发者之间数据结构同步也是很大的问题.Doctrine Migrations组件把数据库变 ...
- [Doctrine Migrations] 数据库迁移组件的深入解析三:自定义数据字段类型
自定义type 根据官方文档,新建TinyIntType类,集成Type,并重写getName,getSqlDeclaration,convertToPHPValue,getBindingType等方 ...
- MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求
MVC5中Model层开发数据注解 ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证( ...
- EFCodeFirst Migrations数据库迁移
EFCodeFirst Migrations数据库迁移 数据库迁移 1.生成数据库 修改类文件PortalContext.cs的静态构造函数,取消当数据库模型发生改变时删除当前数据库重建新数据库的设置 ...
- EF Code First Migrations数据库迁移
1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...
- C# EF Code First Migrations数据库迁移
1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...
- EF Code First Migrations数据库迁移 (转帖)
1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...
- 【EF】EF Code First Migrations数据库迁移
1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...
随机推荐
- M-AddTwoNumbers-未完成
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- awrsqrpt.sql简介
ORACLE_HOME/RDBMS/admin/awrsqrpt.sql 这个脚本可以很方便地取出某个sql在某两个快照间隔内,消耗cpu时间,执行次数,逻辑读,物理读,sql的执行计划以及sql的 ...
- July 15th 2017 Week 28th Saturday
If I can't hear your heartbeat, you are too far away. 如果我听不见你的心跳,那是因为你离我太远了. Only when the two tight ...
- GO语言(七)多核并行化的问题
package main import "fmt" type Vector []float64 func (v Vector) DoSome(i,n int, u Vector, ...
- 魅族首页导航效果(不兼容IE)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Netty问题小结
1.问题:高并发时连接不够:java.net.SocketException: No buffer space available (maximum connections reached?): co ...
- java8的4大核心函数式接口
//java8的4大核心函数式接口//1.Consumer<T>:消费性接口//需求:public void happy(double money, Consumer<Double& ...
- oracle 导出空表问题
select 'alter table '||table_name||' allocate extent;' from user_tables where num_rows=0
- JedisConnectionException: Failed connecting to host localhost:6379
报错原因:没有启动服务,打开服务即可 redis.clients.jedis.exceptions.JedisConnectionException: Exception at redis.clien ...
- Mysql数据库的简单语法
Mysql数据库是目前使用最为广泛的数据对,对于小型企业的支持度,比oracle数据库友好很多. mysql数据库的基本语法 1:创建并且使用数据库 找出服务器上当前存在什么数据库: SHOW DAT ...