PHPackages                             zerifas/ladder - 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. zerifas/ladder

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

zerifas/ladder
==============

A database migration tool, written in PHP.

v3.0.0(7y ago)8311↓100%[1 issues](https://github.com/Drarok/ladder2/issues)MITPHPPHP &gt;=7.0CI failing

Since Aug 2Pushed 3y ago2 watchersCompare

[ Source](https://github.com/Drarok/ladder2)[ Packagist](https://packagist.org/packages/zerifas/ladder)[ RSS](/packages/zerifas-ladder/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependencies (8)Versions (9)Used By (0)

Ladder [![Build Status](https://github.com/Drarok/ladder2/actions/workflows/php.yml/badge.svg)](https://github.com/Drarok/ladder2/actions/workflows/php.yml) [![Coverage Status](https://camo.githubusercontent.com/598ccb80a789dff338c00efb6fa5b85f81d6a552cd73c08370e2cda55323b723/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f447261726f6b2f6c6164646572322f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/Drarok/ladder2?branch=develop)
====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#ladder--)

What is it?
-----------

[](#what-is-it)

Ladder started life many years ago as an extremely simple database migration system. It's grown over time to include a host of features. It's written for PHP &gt;= 8.0, and supports the popular MySQL database server via PDO.

What would I use this for?
--------------------------

[](#what-would-i-use-this-for)

It's used in conjunction with source control (Git, Mercurial, SVN, et al) in order to track the changes made to the database alongside application source code.

This allows multiple developers to work on a project and know whether or not they have the correct database schema for their local development environments, and enables them to bring their schema up-to-date with a single command.

Migrations have at least two methods: `apply()` and `rollback()`. The `apply()` method is run when the migration is applied, and `rollback()` when it is un-applied. Logically, a `rollback()` method should do the opposite to its counterpart `apply()`method; Dropping a column instead of adding it, etc.

What is supported?
------------------

[](#what-is-supported)

- Creating and dropping tables
- Adding, dropping, and altering columns
- Adding and dropping indexes/constraints
- Data operations: insert/update/delete
- Storing metadata when applying a migration, and using it during rollback

How do I use it?
----------------

[](#how-do-i-use-it)

You can add Ladder to your project using Composer:

```
$ composer require zerifas/ladder
$ edit ladder.json
```

Your `ladder.json` file should look something like the included `ladder.json.sample`:

```
{
    "db": {
        "host": "localhost",
        "dbname": "YOUR_DATABASE",
        "charset": "utf8",
        "username": "privileged_user",
        "password": "G00D_P@ssw0rd%"
    },
    "migrations": [
        {
            "namespace": "YourAppNamespace\\Migration",
            "path":      "src/Migration"
        }
    ]
}
```

If your database does not exist, and your user has access to create it, `init` will do so:

```
$ vendor/bin/ladder init
Created database 'test2'.
```

Now, you should be able to run the `status` command to see what's not applied, and `migrate` to apply changes.

```
$ vendor/bin/ladder status
Status
    Missing Migrations
        1 - Ladder internal tables
$ vendor/bin/ladder migrate
Migrate from 0 to 1
Applying 1 - Ladder internal tables: OK
```

You can get Ladder to create a migration template file for you:

```
$ vendor/bin/ladder create 'Create user table'
src/Migration/Migration1455898526.php
```

The template shows an example of creating and dropping a table, edit and save the file, and then:

```
$ vendor/bin/ladder migrate
Migrate from 1 to 1455898526
Applying 1455898526 - Create user table: OK
```

Additionally, you can provide a migration id to the `migrate` command in order to migrate to that specific point.

The `migrate` command can be used to migrate to an *older* migration only when given the `--rollback` option. This is to avoid accidentally dropping tables or columns.

### Commands

[](#commands)

Ladder aims to be self-documenting in use (though we need documentation for the Migration methods):

```
$ vendor/bin/ladder
Ladder version 2.1.0

Usage:
  [options] command [arguments]

…

Available commands:
  create   Create a new Migration file.
  help     Displays help for a command
  init     Attempt to create the configured database.
  list     Lists commands
  migrate  Migrate to the latest, or specified migration number.
  reapply  Rollback the given migration (if applied), then apply it.
  remove   Rollback a single migration.
  status   Show the current status of the database.
```

Example Migration
-----------------

[](#example-migration)

Here's an example that does *way* too much for a single Migration, but should cover all the use cases.

```
