antimattr/mongodb-migrations-bundle

该软件包已被放弃,不再维护。未建议替代软件包。

Symfony MongoDBMigrationsBundle

v1.0.2 2016-09-16 20:06 UTC

This package is not auto-updated.

Last update: 2018-06-29 18:51:19 UTC


README

此扩展集成了AntiMattr MongoDB Migrations库,使其能够在Symfony中安全快速地管理MongoDB迁移。

安装

将以下内容添加到您的composer.json文件中

{
    "require": {
        "antimattr/mongodb-migrations": "~1.0@stable",
        "antimattr/mongodb-migrations-bundle": "~1.0@stable"
    }
}

通过运行以下命令安装库

composer install

如果一切顺利,MonogDBMigrationsBundle现在可以在vendor/antimattr/mongodb-migrations-bundle中找到。

最后,请确保在AppKernel.php中启用扩展,包括以下内容

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

配置

mongo_db_migrations:
    collection_name: "migration_versions"
    database_name: "opensky_devo"
    dir_name: "%kernel.root_dir%/../src/OpenSky/Bundle/MainBundle/Migrations/MongoDB"
    script_dir_name: "%kernel.root_dir%/scripts"
    name: "OpenSky DEVO MongoDB Migrations"
    namespace: "OpenSky\Bundle\MainBundle\Migrations\MongoDB"

容器感知迁移

在某些情况下,您可能需要访问容器以确保数据结构的正确更新。这可能需要使用一些特定逻辑更新关系或创建新实体。

因此,您只需实现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(Database $db)
    {
        // ... migration content
    }

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

MongoDB游标超时

在某些情况下,您可能需要扩展游标超时。当然,您可以在每个迁移的基础上这样做,或者您可以通过扩展基本迁移并在构造函数中添加来为此所有迁移执行此操作。

// ...
use AntiMattr\MongoDB\Migrations\AbstractMigration as BaseMigration;
use AntiMattr\MongoDB\Migrations\Version;
use MongoCursor;

abstract class AbstractMigration extends BaseMigration
{
    /**
     * @var AntiMattr\MongoDB\Migrations\Version
     */
    public function __construct(Version $version)
    {
        parent::__construct($version);
        MongoCursor::$timeout = -1;
    }
}

功能

有关可用功能的完整列表,请参阅底层库中的README.md

https://github.com/antimattr/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)

功能 - 版本升级或降级

由于某些原因,您的迁移历史记录是否同步?您可以在不运行底层迁移的情况下手动添加或删除历史记录中的记录。

您可以删除

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

您可以添加

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