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

news/2024/6/18 18:30:35

自定义命令脚本

目录结构

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

file

其中,db/migrations文件夹是迁移类文件夹,config/db.php是我们项目原有的db配置,migrations.phpmigrations-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.phpmigrations-db.php这两个配置文件也可以删除了。

PhpStorm集成迁移命令

如果你使用PhpStorm,命令行还可以集成到开发工具中去,会有自动提示,大大提高工作效率:

  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即会出现命令提示。

结语

到此,数据迁移组件就已经很灵活的集成到我们自己的项目中了,而且你还可以根据自己的项目灵活修改自定义命令脚本文件。

但是如果使用久了会发现现在的数据迁移会有两个问题:

  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。

http://www.niftyadmin.cn/n/3723656.html

相关文章

ALLyeSNO 优化版浩方 第二版 Ver:2007.06.15 清除广告 自动挤房间

allyesno 优化版浩方 第二版 Ver:2007.06.15 正式发布 ALLyeSNO优化版浩方 2007.06.151.按CtrlF1挤房间 按CtrlF2停止挤房间 (一般情况下会自动停止 无需按CtrlF2停止)2.可以挤浩方对战平台的任意房间,包括浩方大厅房间,浩方引擎房间,CS房间注&#xff1a;如果进去游戏后 再次挤…

【附视频】GOOGLE 谷歌中国 首次与网民冲突事件实录

欢迎查看视频http://www.youku.com/v_show/id_XNTA1MDM0MA.htmlhttp://www.bullog.cn/blogs/zola/archives/71022.aspx 以下是文字版&#xff1a;引用 Zola说&#xff1a;现在我们要闯Google了&#xff0c;没有保安&#xff0c;&#xff08;遇到喷泉&#xff09;妈的,Google浪费…

C# 全网最详尽Linq 常用功能解析,带使用示例 持续更新... 20230706

文章目录 SelectSelectManyTakeTakeWhileTakeSkipWhileJoinGroupJoinOrderByOrderByDescendingThenBy Select 当谈到 C# LINQ 的 Select 方法时&#xff0c;它是一个非常强大的工具&#xff0c;可以用于各种不同的用途。下面是一些常见的用法&#xff1a; 投影&#xff08;Pr…

动网产品失败, 合作伙伴寒心离去

感谢匿名人士的投递看到了“动网新闻”倒戈转投DZ阵营的新闻,这应该是业界比较大的突发事件了吧,然而看到这篇新闻我丝毫没感到惊讶. 其实,细心的人应该可以发现,动网这两年来一直就没增加过新的合作伙伴,更别说比较强大或者成功的了.现在的合作伙伴也都是多年前建立的关系了,老…

黑客软件大众化 【爆吧机的出现】杨丞琳贴吧遭网友攻陷

互联网的出现&#xff0c;使人类的矛盾得到了延伸&#xff0c;未来的网络战争似乎是一种不可避免的趋势。但是&#xff0c;与俄罗斯-爱沙尼亚间的网络攻防战不同&#xff0c;很显然&#xff0c;多次出现的百度“爆吧”行动更像是依靠人海战术的网络战斗&#xff0c;因为这种攻击…

关于硬盘坏道(绝对有收获) 转至 蓝图湾

转至&#xff1a;http://lantowan.kmip.net/?actionshow&id87 跳出硬盘认识的误区 多年来一直误导着高朋的几个常识性问题是&#xff1a;   1&#xff0e;硬盘逻辑坏道可以修复&#xff0c;而物理坏道不可修复。实际情况是&#xff0c;坏道并不分为逻辑坏道和物理坏道&a…

面试官:来,给本官讲讲前端页面性能优化及错误处理有哪些?(看你懵不懵)...

题目&#xff1a;提升页面性能的方法有哪些&#xff1f; 资源压缩合并&#xff0c;减少HTTP请求非核心代码异步加载-->异步加载方式 --> 异步加载区别利用浏览器缓存 --> 缓存的分类 --> 缓存原理使用CDN预解析DNS<meta http-equiv"x-dns-prefetch-control…

Http请求和响应的编码问题

前言&#xff1a; 今天来谈谈Tomcat服务器和网页之间编码和解码之间的关系。关于URL编码的问题可以看链接&#xff1a;廖雪峰修正&#xff1a;百度现在编码也是UTF-8 关于编码之间的转换问题可以看链接&#xff1a;编码转换问题 浏览器端的编码: 默认解码是GB2312影响form提交数…