数据库迁移特征是数据库抽象层的扩展,允许你用编程的方式,安全、方便、标准化实现数据库结构的更新。

安装

首先使用composer安装

    $ composer require doctrine/doctrine-migrations-bundle "^1.0"

如果安装正确的话可以在vendor/doctrine/doctrine-migrations-bundle找到DoctrineMigrationsBundle

然后在AppKernel.php里面加载DoctrineMigrationsBundle

// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
//...
new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
);
}

配置

你可以在config.yml里面配置路径、命名空间、表名和名称。下例是默认值:

# app/config/config.yml
doctrine_migrations:
dir_name: "%kernel.root_dir%/DoctrineMigrations"
namespace: Application\Migrations
table_name: migration_versions
name: Application Migrations

使用

所有Migration功能包含一些控制台命令

doctrine:migrations
:diff Generate a migration by comparing your current database to your mapping information.
:execute Execute a single migration version up or down manually.
:generate Generate a blank migration class.
:migrate Execute a migration to a specified version or the latest available version.
:status View the status of a set of migrations.
:version Manually add and delete migration versions from the version table.

使用status命令获取你的migration状态

app/console doctrine:migrations:status

 == Configuration

    >> Name:                                               Application Migrations
>> Database Driver: pdo_mysql
>> Database Name: tech-edu
>> Configuration Source: manually configured
>> Version Table Name: migration_versions
>> Version Column Name: version
>> Migrations Namespace: Application\Migrations
>> Migrations Directory: /var/www/gitlab/XGC/app/DoctrineMigrations
>> Previous Version: Already at first version
>> Current Version: 0
>> Next Version: Already at latest version
>> Latest Version: 0
>> Executed Migrations: 0
>> Executed Unavailable Migrations: 0
>> Available Migrations: 0
>> New Migrations: 0

现在,你可以开始使用migrations生成一个空的migration class,后面你将会知道doctrine是如何为你自动生成Migrations的

$ php app/console doctrine:migrations:generate
Generated new migration class to "/path/to/project/app/DoctrineMigrations/Version20100621140655.php"

打开新生成的文件,你将会看到如下样子:

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema; class Version20100621140655 extends AbstractMigration
{
public function up(Schema $schema)
{ } public function down(Schema $schema)
{ }
}

现在再执行status命令你将会看到有一个新的migration可以执行

$ php app/console doctrine:migrations:status --show-versions

 == Configuration

   >> Name:                                               Application Migrations
>> Configuration Source: manually configured
>> Version Table Name: migration_versions
>> Migrations Namespace: Application\Migrations
>> Migrations Directory: /path/to/project/app/DoctrineMigrations
>> Current Version: 0
>> Latest Version: 2010-06-21 14:06:55 (20100621140655)
>> Executed Migrations: 0
>> Available Migrations: 1
>> New Migrations: 1 == Migration Versions >> 2010-06-21 14:06:55 (20100621140655) not migrated

之后你可以在up() down()里面添加migration 代码,最后使用migrate命令执行文件:

$ php app/console doctrine:migrations:migrate 20100621140655

如何书写migration代码请参考doctrine migration官方文档

在部署应用中使用migration

使用migration的最终目的是在你部署应用的时候能够更新你的数据库表结构。通过在本地环境下运行migration确保运行的结果是正确的。

在你部署应用的最后环节,你只需要运行doctrine:migrations:migrate命令。Doctrine在你的数据库里面创建了一张migration_versions表,记录了哪些migration被执行,因此不管你执行多少遍migrate命令,它都只会执行那些没有被执行过的migration文件。

跳过指定migration文件

运行下面命令将指定版本的migration文件添加进migration_versions表

$ php app/console doctrine:migrations:version YYYYMMDDHHMMSS --add

这样在执行的时候就会忽略改文件了。

自动生成migrations

现实中,你应该很少需要手动写migrations,因为migrations库可以对比你的doctrine映射与实际表结构自动生成migration。

例如:你创建了一个新的用户实例并且在orm里面添加了映射

// src/Acme/HelloBundle/Entity/User.php
namespace Acme\HelloBundle\Entity; use Doctrine\ORM\Mapping as ORM; /**
* @ORM\Entity
* @ORM\Table(name="hello_user")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id; /**
* @ORM\Column(type="string", length=255)
*/
protected $name;
}

有了这些信息,doctrine已经可以帮你把user对象和hello_user表持久化了。当然,表现在还不存在。用diff命令可以自动生成

$ php app/console doctrine:migrations:diff

你可以看到根据这两者的差异自动生成了一个migration class。里面有创建table_user表的sql语句,下一步只需要执行migrate指令

$ php app/console doctrine:migrations:migrate

所以,每当你改变了映射信息,你需要执行diff命令来生成migration class

假如你在项目最开始的时候就这样做的话,你就可以在任何时候使用migrate命令来获取到最干净的结构

你也可以通过下面的命令来跳过所有的migration

$ php app/console doctrine:migrations:version --add --all

Container Aware Migrations

在某些情况下,您可能需要访问容器,以确保您的数据结构的正确更新。这可能在你创建一些有特殊逻辑的实体的时候会用到。

你可以加载ContainerAwareInterface来获取容器的访问

// ...
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; class Version20130326212938 extends AbstractMigration implements ContainerAwareInterface
{ private $container; public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
} public function up(Schema $schema)
{
// ... migration content
} public function postUp(Schema $schema)
{
$converter = $this->container->get('my_service.convert_data_to');
// ... convert the data from markdown to html for instance
}
}

Manual Tables

It is a common use case, that in addition to your generated database structure based on your doctrine entities you might need custom tables. By default such tables will be removed by the doctrine:migrations:diff command.

If you follow a specific scheme you can configure doctrine/dbal to ignore those tables. Let's say all custom tables will be prefixed by t_. In this case you just have to add the following configuration option to your doctrine configuration:

doctrine:
dbal:
schema_filter: ~^(?!t_)~

This ignores the tables on the DBAL level and they will be ignored by the diff command.

Note that if you have multiple connections configured then the schema_filter configuration will need to be placed per-connection.

(最后部分以后再看。。。。。。)

DoctrineMigrationsBundle的更多相关文章

  1. 跟我一起用Symfony写一个博客网站;

    我的微信公众号感兴趣的话可以扫一下, 或者加微信号   whenDreams 第一部分:基础设置,跑起一个页面-首页 第一步: composer create-project symfony/fram ...

  2. symfony 数据库表生成实体、迁移数据库

    从数据库表生成实体 1. 由数据库生成模型: php bin/console doctrine:mapping:convert --from-database yml D:\db\ D:\test_b ...

随机推荐

  1. 自动启动docker container

    当系统启动的时候要启动docker container, 可以利用systemctl来实现 比如拿mongodb为例 创建 /usr/lib/systemd/system/docker_mongodb ...

  2. django 模板视图,表单视图,各种视图

    Generic editing views¶ The following views are described on this page and provide a foundation for e ...

  3. Hdu1097(计算a的b次幂最后一位数值)

    #include <stdio.h> #include <math.h> int main() { int Num1,Num2; while(scanf("%d %d ...

  4. ucos 创建 空闲任务的目的

    几乎任何操作系统都需要有空闲任务. 因为CPU(提供CPU级休眠的不算)没办法停下来,尤其是嵌入式系统这一块. CPU停下来的唯一情况就是断电了,而要保持操作系统任何时候都能及时的对外做出响应,就必须 ...

  5. 如何用js检测手机是否安装某个app

    问题描述 如果本地安装了app那么直接打开,否则苹果要跳转到app-store,安卓则要跳到对应的市场 解决方案 一 //html代码中 的 a 标签,以微信为例,默认的是调用weixin schem ...

  6. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  7. 工控主板对ISO7816智能卡标准的支持

    ISO7816是一套协议标准,这套协议不仅规定了智能IC卡的机械电气特性,而且还规定了智能IC卡的应用方法.智能IC卡的主要用途可归为身份识别.支付安全.加密/解密和信息存储四个方面.智能IC卡已经广 ...

  8. Qt事件机制浅析(定义,产生,异步事件循环,转发,与信号的区别。感觉QT事件与Delphi的事件一致,而信号则与Windows消息一致)

    Qt事件机制 Qt程序是事件驱动的, 程序的每个动作都是由幕后某个事件所触发.. Qt事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. Qt事件的类型很多, 常见的qt的事件如下: 键盘事 ...

  9. 利用ThinkPHP搭建网站后台架构

    记录一下ThinkPHP搭建网站后台.调整好样式等操作步骤 下载好ThinkPHP(3.2.3),解压后将核心文件夹ThinkPHP以及index.php等文件复制到网站根目录如下图 对index.p ...

  10. 戴着镣铐起舞——从logo设计说起

    一天中午走在路上,顺道去看了学校新媒体艺术与设计系的学生优秀作品展.看到一个logo设计时引发了我很大的兴趣,觉得设计的非常清新活泼,不过并没有拍下来,在这里不能贴图.但是logo设计大家应该都有所概 ...