自定义命令脚本

目录结构

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

其中,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。

[Doctrine Migrations] 数据库迁移组件的深入解析二:自定义集成的更多相关文章

  1. [Doctrine Migrations] 数据库迁移组件的深入解析四:集成diff方式迁移组件

    场景及优势 熟悉Symfony框架之后,深刻感受到框架集成的ORM组件Doctrine2的强大之处,其中附带的数据迁移也十分方便.Doctrine2是使用Doctrine DBAL组件把代码里面的表结 ...

  2. [Doctrine Migrations] 数据库迁移组件的深入解析一:安装与使用

    场景分析 团队开发中,每个开发人员对于数据库都修改都必须手动记录,上线时需要人工整理,运维成本极高.而且在多个开发者之间数据结构同步也是很大的问题.Doctrine Migrations组件把数据库变 ...

  3. [Doctrine Migrations] 数据库迁移组件的深入解析三:自定义数据字段类型

    自定义type 根据官方文档,新建TinyIntType类,集成Type,并重写getName,getSqlDeclaration,convertToPHPValue,getBindingType等方 ...

  4. MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求

    MVC5中Model层开发数据注解   ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证( ...

  5. EFCodeFirst Migrations数据库迁移

    EFCodeFirst Migrations数据库迁移 数据库迁移 1.生成数据库 修改类文件PortalContext.cs的静态构造函数,取消当数据库模型发生改变时删除当前数据库重建新数据库的设置 ...

  6. EF Code First Migrations数据库迁移

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

  7. C# EF Code First Migrations数据库迁移

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

  8. EF Code First Migrations数据库迁移 (转帖)

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

  9. 【EF】EF Code First Migrations数据库迁移

    1.EF Code First创建数据库 新建控制台应用程序Portal,通过程序包管理器控制台添加EntityFramework. 在程序包管理器控制台中执行以下语句,安装EntityFramewo ...

随机推荐

  1. 对连接到 Azure 中 Linux VM 时出现的问题进行详细的 SSH 故障排除的步骤

    有许多可能的原因会导致 SSH 客户端无法访问 VM 上的 SSH 服务. 如果已经执行了较常规的 SSH 故障排除步骤,则需要进一步排查连接问题. 本文指导用户完成详细的故障排除步骤,以确定 SSH ...

  2. LeetCode-Container With Most Water-zz

    先上代码. #include <iostream> #include <vector> #include <algorithm> using namespace s ...

  3. 【NLP_Stanford课堂】情感分析

    一.简介 实例: 电影评论.产品评论是positive还是negative 公众.消费者的信心是否在增加 公众对于候选人.社会事件等的倾向 预测股票市场的涨跌 Affective States又分为: ...

  4. 【Leetcode】【Medium】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 解题: ...

  5. Linux入门-1 常用命令

    写在前面 当年初学Linux的时候,在网上找到nash_su大神的一套视频,讲的特别好,基础部分看了好几遍,很多知识点让我受益至今. 十分庆幸当年的选择,也十分感谢nash_su大神,祝你事事顺心,每 ...

  6. 总结:从Node爬取数据到前端图表展示

    最近寒假在家学习Node.js开发,光看书或者跟着敲代码还不够,得找一点有趣的事情来玩一玩,于是我决定写一个Node爬虫,爬取一些有意思或者说是有用的数据.这个决定只与我的兴趣有关,与Python或者 ...

  7. 百度地图隐藏LOGO显示

    在引入地图的页面加入下列样式即可隐藏百度地图左下角的LOGO   <style type="text/css">   .anchorBL{display:none;} ...

  8. Spring 的下载、安装和使用

    一.下载 Spring 下载地址:http://repo.spring.io/libs-release-local/org/springframework/spring/4.0.6.RELEASE/  ...

  9. CRT公钥登录

    1.实现原理: 通过CRT生成的密钥对,把公钥上传到Linux服务器指定用户下的.ssh目录中,在客户端上只需输入秘钥的密码即可登陆,而且验证一次以后可以免密码登陆 2.具体过程: 转自:http:/ ...

  10. vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证)

    vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证): html: <el-form-item label="活动时间" required> & ...