PHPackages                             cbaykam/php-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. cbaykam/php-migrations

ActiveLibrary

cbaykam/php-migrations
======================

Database migrations for php 5.x

110PHP

Since Mar 13Pushed 11y ago1 watchersCompare

[ Source](https://github.com/cbaykam/php-migrations)[ Packagist](https://packagist.org/packages/cbaykam/php-migrations)[ RSS](/packages/cbaykam-php-migrations/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Php Standalone Migrations Plugin v0.0.1
=======================================

[](#php-standalone-migrations-plugin-v001)

This plugin enables to migrate your db tables as in Ruby on Rails.

Installation :
--------------

[](#installation-)

- With Composer

    - Add cbaykam/php-migrations To your composer.json file composer.phar install
- With Git clone or download To install the plugin download it and put it in a sub directory in your project
- Copy the config.php.sample to config.php and fill it with you configuration variables
- If you are using composer please make sure that your versions dir is outside the vendor directory

Make sure you have the rights to execute the migrate.php Move the config.php.sample to config.php and update the contents with your db credentials.

```
    ./migrate.php install

```

Generating Migrations :
-----------------------

[](#generating-migrations-)

To generate a migration you have to run the command below. With specifying the class name with underscores.

```
	./migrate.php generate the_name_of_the_migration

```

This will generate a php file under versions directory with the unix timestamp.

Coding migration files :
------------------------

[](#coding-migration-files-)

Open the migration file you generated and in the up method add the change you want to add. In the down method add the reverse functions for rolling back the change if something goes wrong.

- Adding a table

    ```
      class CreateUsersTable extends Migrations{
      	public function up(){
      		$this->create_table('users', array(
      			array('primaryKey', array('name_of_key_1')), // This is for adding a primary key, Supports multiple primary keys.

      			array('username', 'string'),
      			array('password', array('type' => 'string', 'length' => 252)),
      			array('sign_in_count', 'integer')
      		));
      	}

      	// We have to code the reverse of it for being able to rollback
      	public function down(){
      		$this->drop_table('users');
      	}
      }

    ```

    you can either choose to specify the field type with string by choosing defaults or an array if you want to customize.
- Adding a field

    ```
      class AddAStrangeFieldToUsers extends Migrations{
      	public function up(){
      		$this->add_field('users', 'strange_field', array('type' => 'string', 'length' => 12));
      		$this->add_field('users', 'strange_notnull', array('type' => 'integer', 'null' => false));
      		$this->add_field('users', 'strange_two', 'integer');
      		$this->add_field('users', 'strange_three', 'string');
      	}

      	public function down(){
      		$this->remove_field('users', 'strange_field');
      		$this->remove_field('users', 'strange_notnull');
      		$this->remove_field('users', 'strange_two');
      		$this->remove_field('users', 'strange_three');
      	}
      }

    ```
- Removing a field

    ```
      class RemoveVeryImportantField extends Migrations{
          public function up(){
              $this->remove_field('users', 'strange_two');
          }

          public function down(){
          	$this->add_field('users', 'strange_two', 'string');
      	}
      }

    ```
- Adding a primary key

    ```
      class AddPrimaryKey extends Migrations{
          public function up(){
          	$this-> add_primary_key('users', array('id')); // For existing fields. Also can be array of multiple fields
          }
              $this->remove_primary_key('users');
      	}
      }

    ```
- Removing a primary key

    ```
      class RemovePrimaryKey extends Migrations{
          public function up(){
              $this->remove_primary_key('users');
          }

          public function down(){
          	$this-> add_primary_key('users', array('id')); // For existing fields. Also can be array of multiple fields
      	// For adding a new id key:
      	// $this->add_field('users', 'id', array('type' => 'integer', 'null' => false, 'autoincrement' => true, 'primarykey' => true, 'first' => true));
      	}
      }

    ```
- Removing a table

    ```
      class CreateUsersTable extends Migrations{
      	public function up(){
      		$this->drop_table('users');
      	}

      	public function down(){
      		$this->create_table('users', array(
      			array('username', 'string'),
      			array('password', array('type' => 'string', 'length' => 252)),
      			array('sign_in_count', 'integer')
      		));
      	}
      }

    ```
- Executing a raw sql query

    ```
      class AddSpecialColumn extends Migrations{
      	public function up(){
      		$this->runquery('ALTER TABLE `users` ADD `is_advertiser` BOOLEAN NOT NULL DEFAULT FALSE AFTER `is_presenter`;');
      	}

      	public function down(){
      		$this->remove_field('users','is_advertiser');
      	}
      }

    ```
- Running migrations

    ```
      ./migrate.php run

    ```

Field types and options
-----------------------

[](#field-types-and-options)

- String length
- Integer length null autoincrement primarykey first
- Enum options
- Datetime null options
- ForeignKey options

For existing projects
---------------------

[](#for-existing-projects)

Install the plugin, export the latest version of your database and generate a new migration ie: database.sql

```
	./migrate.php generate initial_version

```

Then open the migration file and in the up method please insert

```
	$this->runsql('database.sql');

```

The database sql file path is relative to the path you installed the plugin.

Todos
-----

[](#todos)

- Add more datatypes
- Make output messages smarter
- Add schema dump.
- Add availability to handle migrations with the same class name.
- Support Postgresql

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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/c42dd817cabb7f2b2609eb3c5075b60ac8211199c2b830b3be86086421ad5e43?d=identicon)[cbaykam](/maintainers/cbaykam)

### Embed Badge

![Health badge](/badges/cbaykam-php-migrations/health.svg)

```
[![Health](https://phpackages.com/badges/cbaykam-php-migrations/health.svg)](https://phpackages.com/packages/cbaykam-php-migrations)
```

PHPackages © 2026

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