PHPackages                             mvrhov/migratum - 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. mvrhov/migratum

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

mvrhov/migratum
===============

Templated database migrations

16PHP

Since Feb 1Pushed 8y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Migratum: Templated Database Migrations
=======================================

[](#migratum-templated-database-migrations)

### Intro

[](#intro)

Most of the PHP migration tools force you to write the migrations in PHP with classes and function, You are database agnostic then, but on the other hand you are usually limited to the lowest common denominator of what's supported by databases in features you can use. With Migratum you write migrations directly in database specific DDL. On top of that you can pass some context into each migration.

Most people think that having the migrations in DB specific DDL doesn't allow you to change the database but let's be honest here. Almost nobody changes the database after it has been decided on the platform.

### Features

[](#features)

- utilize any DDL &amp; DML functionality your database provides
- pass parameters/context to your migrations
- create migrations for multiple database providers e.g MySQL and PostgreSQL
- migrate up and down
- migrate pending up migrations
- migrations in multiple directories and/or namespaces
- multiple environments
- uses twig to

### Supported Adapters

[](#supported-adapters)

Migratum natively supports following database platforms:

- PostgreSQL

### Install &amp; Run

[](#install--run)

1. Install Composer:

    ```
    curl -sS https://getcomposer.org/installer | php

    ```
2. Require Migratum as a dependency using Composer:

    ```
    php composer.phar require mvrhov/migratum

    ```
3. Execute Migratum:

    ```
    php composer.phar install

    ```

### Commands

[](#commands)

All commands have a dry-run mode where you can se what's going to be run. If you also provide `-v` you can also see the queries that are going to be executed.

#### migratum:init

[](#migratuminit)

Initializes migratum, by creating default configuration file

#### migratum:create

[](#migratumcreate)

Create empty migration in some namespace

`bin/migratum migratum:create "create table account"` creates an empty migration in default namespace `_create_table_account`

`bin/migratum migratum:create "create table foo" -s foo.1` creates an empty migration in namespace `foo` named `_create_table_foo`

#### migratum:migrate

[](#migratummigrate)

`bin/migratum migratum:migrate` apply all pending migrations in order regardless the namespace they come from

`bin/migratum migratum:migrate -t ` apply all pending migrations up to `` in order regardless the namespace they come from

#### migratum:pending

[](#migratumpending)

Sometimes when multiple people work on a project it happens that there are some migrations that other people have created and those migrations sit in between yours. When this happens you should review those migrations and if they don't conflict with yours, then you should run the pending command. If they do conflict, then unfortunately the only way to do proper migrations is to rollback back up to that migration, fix your migrations and apply them again.

#### migratum:rollback

[](#migratumrollback)

Revert some of the applied migrations

`bin/migratum migratum:rollback -b ` rollback applied migrations up to `` migrations regardless of version

`bin/migratum migratum:rollback -t ` rollback applied migrations up to `` migrations

#### migratum:status

[](#migratumstatus)

The status command will report the current state of the database.

### Migration example

[](#migration-example)

- timestamps.sql.twig

```
{% macro timestamp_triggers(tableName) %}
DROP TRIGGER IF EXISTS set_updated_at_{{ tableName }} ON {{ tableName }};

CREATE TRIGGER set_updated_at_{{ tableName }}
BEFORE UPDATE ON {{ tableName }} FOR EACH ROW
EXECUTE PROCEDURE set_updated_at_column();

{% endmacro timestamp_triggers %}

{% macro drop_timestamp_triggers(tableName) %}
DROP TRIGGER IF EXISTS set_updated_at_{{ tableName }} ON {{ tableName }};
{% endmacro drop_timestamp_triggers %}
```

- 20180110851900\_create\_default\_triggers.sql.twig

```
{% block up %}
-- @Migratum\QueryBlockStart
CREATE OR REPLACE FUNCTION set_updated_at_column() RETURNS TRIGGER AS $$
BEGIN
   NEW.updated_at = now();
   RETURN NEW;
END;
$$ language 'plpgsql';
{% endblock up %}

{% block down %}
DROP FUNCTION set_updated_at_column;
{% endblock down %}
```

- 20180111115900\_create\_account.sql.twig

```
{% block up %}
{% import 'timestamps.sql.twig' as t %}

{% set table = 'account' %}

CREATE TABLE {{ table }} (
    id_{{ table }} INTEGER GENERATED BY DEFAULT AS IDENTITY,
    name varchar(200) NOT NULL,
    email varchar(200) NOT NULL,
    enabled boolean NOT NULL,
    created_at timestamp DEFAULT now() NOT NULL,
    updated_at timestamp,

    PRIMARY KEY(id_{{ table }})
);

{{ t.timestamp_triggers(table) }}
{% endblock up %}

{% block down %}
{% set table = 'account' %}

{{ t.drop_timestamp_triggers(table) }}

DROP TABLE {{ table }};
{% endblock down %}
```

### Gotchas

[](#gotchas)

If you look closely, the migrations above you can see that there is a specific SQL comment present in create triggers migration This comment is needed because the Migratum breaks the migration per SQL statement based on `;` character. This becomes a problem when one has stored procedures/triggers... that are basically a group of multiple SQL sentences or said stored procedure/trigger... utilizes the language where the ; is also end of statement. As there is too many different dialects of this you must mark the unbreakable parts with `-- @Migratum\QueryBlockStart`. You can repeat the `-- @Migratum\QueryBlockStart` as may times as you need inside a migration and each of them
starts new non breakable statement. If you want to exit non breakable statement mode you should add the following SQL comment `-- @Migratum\QueryBlockEnd`.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/c03ca79334f04d59814ba1ff794c6582bffa24c5c8b733748fb3d0faec18965b?d=identicon)[mvrhov](/maintainers/mvrhov)

---

Top Contributors

[![mvrhov](https://avatars.githubusercontent.com/u/450008?v=4)](https://github.com/mvrhov "mvrhov (12 commits)")

---

Tags

databaseddlmigrationspostgrespostgresql

### Embed Badge

![Health badge](/badges/mvrhov-migratum/health.svg)

```
[![Health](https://phpackages.com/badges/mvrhov-migratum/health.svg)](https://phpackages.com/packages/mvrhov-migratum)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
