doesntmattr/mongodb-migrations-bundle

Symfony MongoDBMigrationsBundle

v3.0.1 2020-04-30 09:33 UTC

This package is auto-updated.

Last update: 2024-09-05 01:01:15 UTC


README

MIT license Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads

MongoDB 迁移包

此包将 MongoDB Migrations 库集成到 Symfony 中,以便您能够更快地设置。

它已从 antimattr/mongodb-migrations-bundle 移动到 doesntmattr 组织,以继续维护(参见 问题 16)。

原始作者是 @rcatlin 和 @matthewfitz

PHP 版本支持

如果您需要 php 5.6 支持,请使用版本 ^1.0。版本 ^3.0 至少需要 php 7.1。 1.x 版本将仅接收错误修复。

安装

使用 composer 安装

# For php 5.6
composer require "doesntmattr/mongodb-migrations-bundle=^1.0"

# For php 7.1
composer require "doesntmattr/mongodb-migrations-bundle=^3.0"

然后通过包含以下内容在 AppKernel.php 中启用包:

// app/AppKernel.php
public function registerBundles()
{
    $bundles = [
        //...
        new AntiMattr\Bundle\MongoDBMigrationsBundle\MongoDBMigrationsBundle(),
    ];
}

配置

将以下配置行添加到 config.yml 文件中。

# app/config/config.yml
mongo_db_migrations:
    collection_name: "migration_versions"
    database_name: "opensky_devo"
    dir_name: "%kernel.project_dir%/src/OpenSky/Bundle/MainBundle/Migrations/MongoDB"
    script_dir_name: "%kernel.project_dir%/scripts"
    name: "OpenSky DEVO MongoDB Migrations"
    namespace: "OpenSky\\Bundle\\MainBundle\\MigrationsMongoDB"

容器感知迁移

在某些情况下,您可能希望访问容器中定义的一些服务。例如,您可能想使用工厂创建所需结构中的新实体。

要访问容器,只需实现 ContainerAwareInterface 并包含所需的 setContainer() 方法。

// ...
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(Database $db)
    {
        // ... migration content
    }

    public function postUp(Database $db)
    {
        $dm = $this->container->get('doctrine.odm.default_document_manager');
        // ... update the entities
    }
}

MongoDB 游标超时

在某些情况下,您可能需要扩展游标超时。如果是这样,请将 MongoDB 选项 ['socketTimeoutMs' => -1] 添加到您的更新方法中。

功能

有关所有可用功能的完整列表,请参阅 MongoDB Migrations 库中的 README.md。

https://github.com/doesntmattr/mongodb-migrations/blob/master/README.md

与底层库的差异仅限于控制台命令,即数据库配置由 Symfony 的依赖注入容器处理,因此您不需要将它们作为命令行参数传递。

以下为具有以下差异的命令行参数示例

生成新迁移

> ./console mongodb:migrations:generate
Generated new migration class to "Example/Migrations/TestAntiMattr/MongoDB/Version20140822185742.php"

迁移状态

> ./console mongodb:migrations:status

 == Configuration

    >> Name:                                AntiMattr Example Migrations
    >> Database Driver:                     MongoDB
    >> Database Name:                       test_antimattr_migrations
    >> Configuration Source:                demo/ConsoleApplication/config/test_antimattr_mongodb.yml
    >> Version Collection Name:             migration_versions
    >> Migrations Namespace:                Example\Migrations\TestAntiMattr\MongoDB
    >> Migrations Directory:                Example/Migrations/TestAntiMattr/MongoDB
    >> Current Version:                     0
    >> Latest Version:                      2014-08-22 18:57:44 (20140822185744)
    >> Executed Migrations:                 0
    >> Executed Unavailable Migrations:     0
    >> Available Migrations:                3
    >> New Migrations:                      3

迁移所有迁移

这是您在部署过程中将执行的操作。

./console mongodb:migrations:migrate
                                                                    
                    AntiMattr Example Migrations                    
                                                                    

WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/n)y
Migrating up to 20140822185744 from 0

  ++ migrating 20140822185742


     Collection test_a

     metric           before               after                difference           
     ================================================================================
     count            100                  100                  0                   
     size             20452                20452                0                   
     avgObjSize       204.52               204.52               0                   
     storageSize      61440                61440                0                   
     numExtents       2                    2                    0                   
     nindexes         1                    2                    1                   
     lastExtentSize   49152                49152                0                   
     paddingFactor    1                    1                    0                   
     totalIndexSize   8176                 16352                8176                

  ++ migrated (0.03s)

  ++ migrating 20140822185743


  ++ migrated (0s)

  ++ migrating 20140822185744


  ++ migrated (0s)

  ------------------------

  ++ finished in 0.03
  ++ 3 migrations executed

执行单个迁移

./console mongodb:migrations:execute 20140822185742
WARNING! You are about to execute a database migration that could result in data lost. Are you sure you wish to continue? (y/n)y

  ++ migrating 20140822185742


     Collection test_a

     metric           before               after                difference           
     ================================================================================
     count            100                  100                  0                   
     size             20620                20620                0                   
     avgObjSize       206.2                206.2                0                   
     storageSize      61440                61440                0                   
     numExtents       2                    2                    0                   
     nindexes         1                    2                    1                   
     lastExtentSize   49152                49152                0                   
     paddingFactor    1                    1                    0                   
     totalIndexSize   8176                 16352                8176                

  ++ migrated (0.02s)

如果需要重新运行已执行的迁移,请使用 --replay

版本升级或降级

由于某种原因,您的迁移历史记录是否已不同步?您可以手动添加或删除历史记录中的记录,而无需运行底层迁移。

您可以删除

./console mongodb:migrations:version --delete 20140822185744

您可以添加

./console mongodb:migrations:version --add 20140822185744