Developed with love by KnpLabs Hire us for your project!
29

mongodb-migrations-bundle

by doesntmattr

Symfony MongoDBMigrationsBundle

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

MongoDB Migrations Bundle

This bundle integrates the MongoDB Migrations library into Symfony to get you set up more quickly.

It was moved to the doesntmattr organisation from antimattr/mongodb-migrations-bundle to continue maintenance (See issue 16).

The original authors are @rcatlin and @matthewfitz

PHP Version Support

If you require php 5.6 support use version ^1.0. Version ^2.0 requires at least php 7.1. The 1.x releases will only receive bug fixes.

Installation

Install with composer:

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

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

then enable the bundle in AppKernel.php by including the following:

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

Configuration

Add following configuration lines to config.yml file.

# app/config/config.yml
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\\MigrationsMongoDB"

Container Aware Migrations

In some cases you might want to access some services you have defined in the container. For example you may want to use a Factory to create new entities in the structure you need.

To get access to the container simply implement the ContainerAwareInterface including the required method 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 Cursor Timeouts

In some cases you may need the Cursor timeout to be extended. If so, add the MongoDB option ['socketTimeoutMs' => -1] to your update method.

Features

For a full list of available features, see the README.md in the MongoDB Migrations library:

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

Differences from the underlying library are limited to the Console commands, namely database configurations are handled by Symfony's Dependency injection container, so you don't pass them as command line args.

Examples of the Command Line args with the difference below:

Generate a New Migration

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

Status of Migrations

> ./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

Migrate all Migrations

This is what you will execute during your deployment process.

./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

Execute a Single Migration

./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)

Use --replay if you need to re-run an executed migration.

Version Up or Down

Is your migration history out of sync for some reason? You can manually add or remove a record from the history without running the underlying migration.

You can delete

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

You can add

./console mongodb:migrations:version --add 20140822185744
Copyright (c) 2014 AntiMattr MongoDB Migrations Bundle

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
mongo_db_migrations:
collection_name: migration_versions
database_name: ~
dir_name: %kernel.root_dir%/MongoDBMigrations
name: Application MongoDB Migrations
namespace: Application\MongoDBMigrations
script_dir_name: ~
  • Merge pull request #28 from JoppeDC/patch-1
    By web-flow, 13 days ago
  • Symfony 5 Support
    By web-flow, 14 days ago
  • Merge pull request #24 from arendjantetteroo/patch-2
    By web-flow, 1 year ago
  • Fix symfony 4.2 treebuilder deprecation
    By web-flow, 1 year ago
  • Merge remote-tracking branch 'origin/master'
    By redthor, 1 year ago
  • For the v2 release
    By redthor, 1 year ago
  • Merge pull request #21 from redthor/v2/readme-updates
    By web-flow, 1 year ago
  • Update README stating PHP version support and various other small changes
    By redthor, 1 year ago
  • Merge pull request #20 from redthor/v2/code-quality
    By web-flow, 1 year ago
  • Implement code quality via scrutinizer
    By redthor, 1 year ago
  • Found while running code coverage we missed a dependency - symfony config
    By redthor, 1 year ago
  • Merge pull request #19 from redthor/v2/drop-duplicated-commands
    By web-flow, 1 year ago
  • Merge pull request #18 from redthor/v2/array-bracket-syntax
    By web-flow, 1 year ago
  • Merge pull request #17 from redthor/v2/phpunit-7
    By web-flow, 1 year ago
  • Set minimum version to php7.1, drop php5.6 (#16)
    By caciobanu, 1 year ago
  • Remove duplicated commands
    By redthor, 1 year ago
  • Add php-cs-fixer config, update to array bracket syntax and run linter
    By redthor, 1 year ago
  • Switch to phpunit 7
    By redthor, 1 year ago
  • Set minimum version to php7.1, drop php5.6
    By redthor, 1 year ago
  • Merge pull request #13 from redthor/feature/sf4-support
    By web-flow, 1 year ago
  • Travis build improvements (#14)
    By caciobanu, 1 year ago
  • Add sf4 support, care of @w473
    By redthor, 1 year ago
  • Merge pull request #12 from redthor/improvement/deprecate-dup-commands
    By web-flow, 1 year ago
  • Merge pull request #11 from redthor/feature/add-travis-build
    By web-flow, 1 year ago
  • Add deprecated notices to duplicate Doctrine commands, for #1
    By redthor, 1 year ago
  • Trial a different travis build to get 5.6 running
    By redthor, 1 year ago
  • Update dependencies
    By redthor, 1 year ago
  • Add a test and travis config
    By redthor, 1 year ago
  • Merge pull request #6 from manuelbcd/master
    By web-flow, 2 years ago
  • Added comment to config.yml
    By Manuel Boira Cuevas, 2 years ago