PHPackages                             coenjacobs/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. coenjacobs/migrator

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

coenjacobs/migrator
===================

Library to run database migrations in WordPress plugins

0.2.0(7y ago)1488[2 issues](https://github.com/coenjacobs/migrator/issues)MITPHPPHP &gt;=5.6CI failing

Since Feb 1Pushed 5y ago1 watchersCompare

[ Source](https://github.com/coenjacobs/migrator)[ Packagist](https://packagist.org/packages/coenjacobs/migrator)[ RSS](/packages/coenjacobs-migrator/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

Migrator [![Latest Stable Version](https://camo.githubusercontent.com/03ab80f23af76c1bba114d3be3a8b1aa2db3468dfe7ec294ca4f1b956fcef09e/68747470733a2f2f706f7365722e707567782e6f72672f636f656e6a61636f62732f6d69677261746f722f762f737461626c652e737667)](https://packagist.org/packages/coenjacobs/migrator) [![License](https://camo.githubusercontent.com/b217271af618da77c641c617829c793710995572c93b22cbdf490fc6f6c000c9/68747470733a2f2f706f7365722e707567782e6f72672f636f656e6a61636f62732f6d69677261746f722f6c6963656e73652e737667)](https://packagist.org/packages/coenjacobs/migrator)
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#migrator--)

Migrator allows you to run migrations on your database, for your WordPress plugin specific needs. This can be to either add or remove tables, change the structure of the tables, or change the data being contained in your tables.

This package requires PHP 7.2 or higher in order to run the tool.

**Warning:** This package is very experimental and breaking changes are very likely until version 1.0.0 is tagged. Use with caution, always wear a helmet when using this in production environments.

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

[](#installation)

This package can be best installed inside your plugin, by using Composer:

`composer require coenjacobs/migrator`

Best results are achieved when installing this library in combination with the [Mozart package](https://github.com/coenjacobs/mozart), so Migrator will be installed in your own namespace to prevent conflicts.

Workers
-------

[](#workers)

Worker classes are the classes responsible for actually performing the queries your migrations want to run. By default, a `$wpdb` based Worker is available, in the `CoenJacobs\Migrator\Workers\WpdbWorker` class. This utilises the default `$wpdb` global variable in WordPress installations, to run your queries. You can provide your own implementation of the Worker class, as long as they implement the `CoenJacobs\Migrator\Contracts\Worker` interface.

Loggers
-------

[](#loggers)

Loggers take care of registering the migrations that have been run already. This is to ensure that no migrations are being run more than once. By default, there is a database based Logger available, in the `CoenJacobs\Migrator\Loggers\DatabaseLogger` class. This class actually uses the aforementioned `$wpdb` based Worker, in order to log the migration data into a specific database table. This database table is being created, using the table name you've provided as the first contructor argument.

You can provide your own implementation of the Logger class, as long as they implement the `CoenJacobs\Migrator\Contracts\Logger` interface.

Migration structure
-------------------

[](#migration-structure)

All of the migrations are required to follow a specific format, being enforced by the `CoenJacobs\Migrator\Contracts\Migration.php` interface. This interface will force you to provide a static `id()` method, which will need to provide the unique identifier of the migration. The interface also enforces your migration class to contain the following two methods: `up()` and `down()`. These two methods are used to run and rollback your migration.

There is a helper `BaseMigration` available, to help with setting up the right variables for the migration, such as the Worker.

A basic migration example looks like this:

```
