PHPackages                             kevinkl3/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. [Database &amp; ORM](/categories/database)
4. /
5. kevinkl3/php-mongo-migrator

ActiveLibrary[Database &amp; ORM](/categories/database)

kevinkl3/php-mongo-migrator
===========================

Migrations for MongoDB

1.1.0(4mo ago)02MITPHPPHP &gt;=8.1

Since Jun 15Pushed 4mo agoCompare

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

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

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

[](#phpmongo-migrator)

> **Note:** This is a fork of the original [sokil/php-mongo-migrator](https://github.com/sokil/php-mongo-migrator) project, updated for PHP 8.1+ compatibility. This fork replaces the deprecated `sokil/php-mongo` ODM with the official [MongoDB PHP Library](https://github.com/mongodb/mongo-php-library).

Migrations for MongoDB using the official [MongoDB PHP Library](https://github.com/mongodb/mongo-php-library).

[![Daily Downloads](https://camo.githubusercontent.com/edf8fe0bf1c364d49923c1ba973e2f823a33973b370116bd97a0017d987aad81/68747470733a2f2f706f7365722e707567782e6f72672f6b6576696e6b6c332f7068702d6d6f6e676f2d6d69677261746f722f642f6461696c79)](https://packagist.org/packages/sokil/php-mongo-migrator)[![Latest Stable Version](https://camo.githubusercontent.com/f8fb097f5943126fedb56d1a3ea1411599fc7091453453c4940a4206ca596292/68747470733a2f2f706f7365722e707567782e6f72672f6b6576696e6b6c332f7068702d6d6f6e676f2d6d69677261746f722f762f737461626c652e706e67)](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 8.1 or above
- [PHP MongoDB Extension](https://pecl.php.net/package/mongodb) 1.0 or above
- [MongoDB PHP Library](https://github.com/mongodb/mongo-php-library) (installed automatically via Composer)

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

[](#installation)

#### Install locally through composer

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

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

```

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

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:

```
