[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 ...
随机推荐
- SQLServer 2008 新增T-SQL 简写语法
1.定义变量时可以直接赋值 DECLARE @Id int = 5 2.Insert 语句可以一次插入多行数据 INSERT INTO StateList VALUES(@Id, 'WA'), (@I ...
- SQL Server ->> 高可用与灾难恢复(HADR)技术 -- AlwaysOn(实战篇)之AlwaysOn可用性组搭建
因为篇幅原因,AlwaysOn可用性组被拆成了两部分:理论部分和实战部分.而实战部分又被拆成了准备工作和AlwaysOn可用性组搭建. 三篇文章各自的链接: SQL Server ->> ...
- 200. Number of Islands + 695. Max Area of Island
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- 利用Kali进行WiFi钓鱼测试实战
文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用.请不要做一只咖啡馆里安静的猥琐大叔. 写在前面 从至少一年前我就一直想在自己跑kali的笔记本上架个钓鱼热点.然而由于网上的 ...
- bzoj2000 [Hnoi2010]stone 取石头游戏
Description A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参加比赛. 与经典的取石子游戏相 ...
- [原创] SiteServer 3.5 批量导入文章的SQL处理脚本
2005时做过一个小网站,当时是用ASP+Access做的,功能很简单,但里面的文章不少 现在就像把它转移到SS上来,重点就是如何导入文章 本来SS本身提供了批量导入功能,但对于在WEB上一次性导入一 ...
- Centos7 安装eclipse IDE for C++
1.安装前eclipse需要java, yum -y install java 查看版本java -version 2.下载eclipse IDE http://www.eclipse.org/dow ...
- 检测Android和IOS
var u=navigator.userAgent; var isAndroid=u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; / ...
- C++Builder编写计算器
用C++Builder确实能快速上手, 只要是会一点C++基础的,都能很快的编写一些小程序,而且VCL库组件也很丰富,比微软MFC强多了. 自己动手写了一个计算器来增加自己的兴趣.C++基础以后有空还 ...
- TCP Congestion Control
TCP Congestion Control Congestion occurs when total arrival rate from all packet flows exceeds R ove ...