PHPackages                             datingvip/php-mongo-migrator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. datingvip/php-mongo-migrator

AbandonedArchivedLibrary

datingvip/php-mongo-migrator
============================

Migrations for MongoDB

1.0.2(6y ago)02661MITPHPPHP &gt;=5.6 || ^7.0

Since Jun 15Pushed 5y agoCompare

[ Source](https://github.com/DatingVIP/php-mongo-migrator)[ Packagist](https://packagist.org/packages/datingvip/php-mongo-migrator)[ Docs](http://phpmongokit.github.io/)[ RSS](/packages/datingvip-php-mongo-migrator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (23)Used By (0)

PHPMongo Migrator
=================

[](#phpmongo-migrator)

Migrations for MongoDB based on [PHPMongo ODM](https://github.com/sokil/php-mongo)

[![Build Status](https://camo.githubusercontent.com/73abae80ee21c68dce75bc9cad4c734a78c36ed54765d529ad898cb7d5f605e6/68747470733a2f2f7472617669732d63692e6f72672f736f6b696c2f7068702d6d6f6e676f2d6d69677261746f722e706e673f6272616e63683d6d61737465722631)](https://travis-ci.org/sokil/php-mongo-migrator)[![Daily Downloads](https://camo.githubusercontent.com/e2def0ec5a91fee0555d09133457d56422fbecb24430f6a9ace3bb19eef7263f/68747470733a2f2f706f7365722e707567782e6f72672f736f6b696c2f7068702d6d6f6e676f2d6d69677261746f722f642f6461696c79)](https://packagist.org/packages/sokil/php-mongo-migrator)[![Latest Stable Version](https://camo.githubusercontent.com/ffa1dfd1378d01806b07c20ca88bc0bf1bd76b239d81936e2395e08e3a6d3e31/68747470733a2f2f706f7365722e707567782e6f72672f736f6b696c2f7068702d6d6f6e676f2d6d69677261746f722f762f737461626c652e706e67)](https://packagist.org/packages/sokil/php-mongo-migrator)[![Coverage Status](https://camo.githubusercontent.com/69ba7a200d3c267881ba9295ac84885e13d20f80ac9c3249254afdbfc3b5e912/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f736f6b696c2f7068702d6d6f6e676f2d6d69677261746f722f62616467652e706e67)](https://coveralls.io/r/sokil/php-mongo-migrator)[![Gitter](https://camo.githubusercontent.com/dec8b6b49273f8effc1aa09187106b398af3d7ee24dd47ba2aa02bdb93338f9e/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e5f436861742e737667)](https://gitter.im/sokil/php-mongo-migrator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Schema not required in MongoDb, so we dont need to create databases, collections or altering them. However there are some cases when migrations required in schemaless databases:

- Creating collections with special parameters, like capped collection;
- Renaming or deleting collections;
- Creating, renaming or deleting fields;
- Creating, changing or deleting indexes;

Requirements
------------

[](#requirements)

- PHP 5
    - PHP 5.3 not supported starting from 2018-10-19
    - PHP 5.4 - PHP 5.6
    - [PHP Mongo Extension](https://pecl.php.net/package/mongo) 0.9 or above (Some features require &gt;= 1.5)
- PHP 7
    - [PHP MongoDB Extension](https://pecl.php.net/package/mongodb) 1.0 or above
    - [Compatibility layer](https://github.com/alcaeus/mongo-php-adapter). Please, note some [restrictions](#compatibility-with-php-7)
- HHVM
    - HHVM Driver [not supported](https://derickrethans.nl/mongodb-hhvm.html).

Installation
------------

[](#installation)

#### Install locally through composer

[](#install-locally-through-composer)

```
composer require sokil/php-mongo-migrator

```

After installation you will be able to run commands in console by running ./vendor/bin/mongo-migrator command.

#### Install using phive

[](#install-using-phive)

```
phive install sokil/php-mongo-migrator

```

#### Compatibility with PHP 7

[](#compatibility-with-php-7)

> PHPMongo currently based on old [ext-mongo](https://pecl.php.net/package/mongo) entension. To use this ODM with PHP 7, you need to add [compatibility layer](https://github.com/alcaeus/mongo-php-adapter), which implement API of old extension over new [ext-mongodb](https://pecl.php.net/package/mongodb). To start using PHPMongo with PHP7, add requirement [alcaeus/mongo-php-adapter](https://github.com/alcaeus/mongo-php-adapter) to composer. Restrictions for using ODM with compatibility layer you can read in [known issues](https://github.com/alcaeus/mongo-php-adapter#known-issues) of original adapter.

You need to require adapter:

```
composer require alcaeus/mongo-php-adapter

```

Usage
-----

[](#usage)

```
$ ./mongo-migrator
Console Tool

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  create    Create new migration
  help      Displays help for a command
  init      Initialize migrations project
  list      Lists commands
  migrate   Migrate to specific revision of database
  rollback  Rollback to specific version of database
  status    Show status of migrations

```

Initialisation of migrations
----------------------------

[](#initialisation-of-migrations)

Every command run in project root where composer.json and vendor dir placed. First we need to create new migration project. To do that go to project root and run:

```
vendor/bin/mongo-migrator init

```

This creates config file mongo-migrator.yaml and directory "./migrations", where migrations placed. Also you can use php config instead of yaml. Just initialise your project with php config format:

```
vendor/bin/mongo-migrator init --configFormat=php

```

You may explicitly define path to conficuration file, and also to migration dir:

```
vendor/bin/mongo-migrator init --configuration=confins/monfo-migrations.yaml --migrationDir=../migrations/mongo

```

If migration dir defined relatively, it points to dir where configuration stored. In example above migrations dir will be `confins/../migrations/mongo`.

Configuration
-------------

[](#configuration)

#### Configuration format

[](#configuration-format)

YAML configuration file placed in file "./mongo-migrator.yaml". PHP has same structure.

```
default_environment: development

path:
    migrations: migrations

environments:
    development:
        dsn: mongodb://localhost

        default_database: test

        log_database: test
        log_collection: migrations

    staging:
        dsn: mongodb://localhost

        default_database: test

        log_database: test
        log_collection: migrations

    production:
        dsn: mongodb://localhost

        default_database: test

        log_database: test
        log_collection: migrations
```

Environment is set of configuration parameters, defined for concrete place, like development machine, test or production server.

- default\_environment - some commands required to know environment, where they executed. This parameters defines which environment to use if environment not specified.
- path.migrations - path to migrations directory, where migration scripts placed.
- environments - section of environment configs.

Every environment has this parameters:

- environments.\*.dsn - DSN which used to connect to mongo server
- environments.\*.connectOptions - options of MongoClient, described in [\\MongoClient PHP manual](http://php.net/manual/ru/mongoclient.construct.php)
- environments.\*.default\_database - databse, used if no database specified id migration script
- environments.\*.log\_database - database, used to store migration log
- environments.*.log\_collection - collection of database environments.*.log\_database used to store migration log

#### Environment variables in configuration

[](#environment-variables-in-configuration)

Any value may be initialised from environment variable:

```
default_environment: common

path:
    migrations:  "%env(MONGO_MIGRATIONS_PATH)%"

environments:
    common:
        dsn: "%env(MONGO_DSN)%"
        default_database: "%env(MONGO_DEFAULT_DB)%"
        log_database: "%env(MONGO_LOG_DB)%"
        log_collection: "%env(MONGO_LOG_COLLECTION)%"
```

Creating new revision
---------------------

[](#creating-new-revision)

Now you can create your migration script. Creating new revison:

```
vendor/bin/mongo-migrator create revision_name

```

Name of revision must be in camel case format. For example run `vendor/bin/mongo-migrator create RevisionName`. This creates migration script like 20151127093706\_RevisionName.php, where "20151127093706" is revision id and "RevisionName" is revision name.

```
pc:~/php-mongo-migrator$ ./bin/mongo-migrator create RevisionName
New migration created at ~/php-mongo-migrator/migrations/20151127093706_RevisionName.php

```

Class source is:

```
