PHPackages                             openbuildings/timestamped-migrations - 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. openbuildings/timestamped-migrations

AbandonedArchivedKohana-module[Database &amp; ORM](/categories/database)

openbuildings/timestamped-migrations
====================================

Migrations are a convenient way for you to alter your database in a structured and organized manner

0.3.1(8y ago)2133.7k10[3 issues](https://github.com/OpenBuildings/timestamped-migrations/issues)BSD-3-ClausePHPPHP ^7

Since Aug 14Pushed 8y ago19 watchersCompare

[ Source](https://github.com/OpenBuildings/timestamped-migrations)[ Packagist](https://packagist.org/packages/openbuildings/timestamped-migrations)[ Docs](https://github.com/OpenBuildings/timestamped-migrations)[ RSS](/packages/openbuildings-timestamped-migrations/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (9)Dependencies (6)Versions (12)Used By (0)

Timestamped Migrations
======================

[](#timestamped-migrations)

[![Build Status](https://camo.githubusercontent.com/26c8b8917869da771e82b10e1bb15d956b87d8094efac3f4fb3236ad8cbfd548/68747470733a2f2f7472617669732d63692e6f72672f4f70656e4275696c64696e67732f74696d657374616d7065642d6d6967726174696f6e732e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/OpenBuildings/timestamped-migrations)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/d9d52a59c83451f353be00bede93a3e0b61f2700e768a30062a63ca00ee2723c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4f70656e4275696c64696e67732f74696d657374616d7065642d6d6967726174696f6e732f6261646765732f7175616c6974792d73636f72652e706e673f733d34346262613038626339396464363837633765303738306335623366373662626135616537306631)](https://scrutinizer-ci.com/g/OpenBuildings/timestamped-migrations/)[![Code Coverage](https://camo.githubusercontent.com/b1570f33ff7c031a672bc4fa1b632ca45e759a651c1d5c1341470091f4774281/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4f70656e4275696c64696e67732f74696d657374616d7065642d6d6967726174696f6e732f6261646765732f636f7665726167652e706e673f733d61346635303032383431623939633866643462626462623037383233356438376664346233313234)](https://scrutinizer-ci.com/g/OpenBuildings/timestamped-migrations/)[![Latest Stable Version](https://camo.githubusercontent.com/158f79368357bd00f0866a445072ae0757845dd7159c4b7cf994d183e0178f23/68747470733a2f2f706f7365722e707567782e6f72672f6f70656e6275696c64696e67732f74696d657374616d7065642d6d6967726174696f6e732f762f737461626c652e706e67)](https://packagist.org/packages/openbuildings/timestamped-migrations)

Migrations are a convenient way for you to alter your database in a structured and organized manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run them. You'd also have to keep track of which changes need to be run against the production machines next time you deploy.

Migrations module tracks which migrations have already been run so all you have to do is update your source and run ./minion db:migrate. Migrations module will work out which migrations should be run.

Migrations also allow you to describe these transformations using PHP. The great thing about this is that it is database independent: you don't need to worry about the precise syntax of CREATE TABLE any more than you worry about variations on SELECT \* (you can drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production.

Dependencies
------------

[](#dependencies)

This module utilizes the built-in [Kohana-minion](https://github.com/kohana/minion) for it's command line interface. The system is fairly decoupled from it though so you can easily implement this with other cli tools if you use something different.

Options
-------

[](#options)

- log - this is the logging function to be used, to integrate into whatever controller/backend you are using
- path - the path to where the migrations will be stored, defaults to APPPATH/migrations
- type - the driver for the backend for which migrations have been already executed as well as the migrations themselves, defaults to mysql

Migration command line tools
----------------------------

[](#migration-command-line-tools)

This module provides a set of kohana-minion tasks to work with migrations giving you the ability to easily create, run and rollback them. All of them have extensive documentation which you can easily read with kohana-minion's built in commands, e. g.

```
./minion db:migrate --help

```

Migrate all new migrations
--------------------------

[](#migrate-all-new-migrations)

The most common migration related task you use will probably be `db:migrate`. In its most basic form it just runs the up method for all new migration versions, that have not been executed yet. If there are no such migrations it exits.

```
./minion db:migrate

```

If you specify a target version, Active Record will run the required migrations (up or down) until it has reached the specified version. The version is the numerical prefix on the migration’s filename. For example to migrate to version 1322837510 run

```
./minion db:migrate --version=1322837510

```

If this is greater than the current version (i.e. it is migrating upwards) this will run the up method on all migrations up to and including 1322837510, if migrating downwards this will run the down method on all the migrations down to, but not including, 1322837510.

Migrate a migration
-------------------

[](#migrate-a-migration)

The `db:migrate:up` will migrate the first migration version that has not been migrated yet.

```
./minion db:migrate:up

```

Rolling back a migration
------------------------

[](#rolling-back-a-migration)

The `db:migration:down` is used to rollback the last migrated migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run the step below without specifying any version number.

```
./minion db:migrate:down

```

Redo a migration
----------------

[](#redo-a-migration)

The `db:migrate:redo` task is a shortcut for doing a rollback and then migrating back up again. It will execute *db:migrate:down* and after that *db:migrate:up* again.

```
./minion db:migrate:redo

```

Migrate specific version
------------------------

[](#migrate-specific-version)

If you need to run a specific migration up or down the `db:migrate:up` and `db:migrate:down` commands will do that. Just specify the appropriate version and the corresponding migration will have its up or down method invoked, for example

```
./minion db:migrate:up --version=1321025460

```

will run the up method from the 1321025460 migration. These commands check whether the migration has already run, so for example db:migrate:up --version=1321025460 will do nothing if Migrations module believes that --version=1321025460 has already been run.

Migrate last few steps
----------------------

[](#migrate-last-few-steps)

The commands `db:migrate:up`, `db:migrate:down` and `db:migrate:redo` by default are executed for just one migration. However they support `--step` option. It allows you migrate several migration steps at once. For example:

```
	./minion db:migrate:down --step=3

```

will roll back last 3 migrations that has been migrated.

Neither of these commands do anything you could not do with *db:migrate*, they are simply more convenient since you do not need to explicitly specify the version to migrate to.

Dry Run
-------

[](#dry-run)

You can add a `--dry-run` option and it will only show you the migrations that will be executed, without accually executing anything.

```
./minion db:migrate:up --step=3 --dry-run

```

Generating a migration
----------------------

[](#generating-a-migration)

You can generate a migration with the `db:generate` command which will create a file inside the path you've specified in the config of the module. It will prefix the filename with the timestamp and return the created filename

```
./minion db:generate --name=my_migration

```

will display `1495194020_my_migration_user.php Migration File Generated`. It creates scaffold with `up()` and `down()` methods. The migration file will looklike this:

```
class My_Migration extends Migration
{
    public function up()
    {

    }

    public function down()
    {

    }
}
```

---

There are several patterns in the filename that will be recognized and converted to actual helper methods in the up/down methods of the migration.

- create\_table\_{table}
- drop\_table\_{table}
- add\_{columns}\_to\_{table} where {columns} is a list of column names, delimited by `_and_` so you can write `add_name_and_title_to_users` - which will add both columns.
- remove\_{columns}\_from\_{table}
- change\_{columns}\_in\_{table}
- rename\_table\_{old\_name}\_to\_{new\_name}
- rename\_{old\_column\_name}\_to\_{new\_column\_name}\_in\_{table\_name}

To create a users table you could use following command

```
    ./minion db:generate --name=create_table_users

```

```
