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

安装

首先使用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. ocos 信号量

    信号量分为  :声明信号量.互斥信号量 转: ucos-ii学习笔记——信号量的原理   ucos-ii学习笔记——信号量的原理及使用 #include "INCLUDES.h" ...

  2. JS中的phototype是JS中比较难理解的一个部分

    本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展.我们称B的原 ...

  3. Lintcode--003(乱序字符串)

    给出一个字符串数组S,找到其中所有的乱序字符串(Anagram).如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中. 注意事项 所有的字符串都只包含小写字母   样例 ...

  4. 免小号QQ空间说说刷赞器

    小伙伴们赶紧用等待免小号QQ空间说说刷赞器,几分钟就可以刷好几百赞了噢, 给大家一个下载地址:http://www.dedewl.com/TA/gotoB.php?id=770319205B

  5. Unity脚本的生命周期中几个重要的方法

    1.function Update () {} 正常更新,用于更新逻辑.此方法每帧都会由系统自动调用一次.2.function LateUpdate () {} 推迟更新,此方法在Update() 方 ...

  6. ELK 下载地址elastic

    lasticsearch 2.3.5 Elasticsearch can also be installed from our repositories using apt or yum. See R ...

  7. BZOJ1108: [POI2007]天然气管道Gaz

    1108: [POI2007]天然气管道Gaz Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 410  Solved: 211[Submit][Sta ...

  8. pip 错误Requested **, but installing version **

    使用pip升级时,虽然指定了版本,也使用了--upgrade参数,但pip就是不升级,Requested **, but installing version **,手动删了源文件也不行.后来发现一个 ...

  9. CCF软考---《有趣的数》

    脑子一热报了CCF的软测..但是又觉得好像并没有什么卵用,就当为蓝桥杯预热然后顺便去软件学院玩一玩吧,遇到一个有意思的题: time limits : 1s 问题描述 我们把一个数称为有趣的,当且仅当 ...

  10. python3-day4(递归)

    递归 特点 递归算法是一种直接或者间接地调用自身算法的过程.在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于理解. 递归算法解决问题的特点: (1) 递归就是在 ...