PHPackages                             rougin/refinery - 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. rougin/refinery

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

rougin/refinery
===============

"Ready-to-eat" Codeigniter 3 migrations.

v0.4.0(1y ago)183.8k5MITPHPPHP &gt;=5.4.0CI failing

Since Jun 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/rougin/refinery)[ Packagist](https://packagist.org/packages/rougin/refinery)[ Docs](https://roug.in/refinery/)[ RSS](/packages/rougin-refinery/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (12)Used By (0)

Refinery
========

[](#refinery)

[![Latest Version on Packagist](https://camo.githubusercontent.com/bbd96f4619b02aed7554511cef0dd60f30af6d503f11f84e3b382d19bcd33745/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f7567696e2f726566696e6572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/refinery)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/rougin/refinery/blob/master/LICENSE.md)[![Build Status](https://camo.githubusercontent.com/e5be7cd3ff33bee5fd89b0391a464780a4e7d73fe5bce72eb6e897ee12a06824/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f726f7567696e2f726566696e6572792f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/rougin/refinery/actions)[![Coverage Status](https://camo.githubusercontent.com/2a6abc346ab4bca879a883c0f90520d1844ab93bb8acbc732faf83c2208d6d87/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f726f7567696e2f726566696e6572793f7374796c653d666c61742d737175617265)](https://app.codecov.io/gh/rougin/refinery)[![Total Downloads](https://camo.githubusercontent.com/3d5b80f96d4c0296816786aabb37fdcd9b02dbcec897d9e63020f26ec10975ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f7567696e2f726566696e6572792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rougin/refinery)

`Refinery` is a console-based package of [Migrations Class](https://www.codeigniter.com/userguide3/libraries/migration.html) for the [Codeigniter 3](https://codeigniter.com/userguide3). It uses the [Describe](https://roug.in/describe/) package for retrieving the database tables for creating database migrations.

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

[](#installation)

From an existing `Codeigniter 3` project, the `Refinery` package can be installed through [Composer](https://getcomposer.org/):

```
$ composer require rougin/refinery --dev
```

```
// ciacme/composer.json

{
  // ...

  "require-dev":
  {
    "mikey179/vfsstream": "1.6.*",
    "phpunit/phpunit": "4.* || 5.* || 9.*",
    "rougin/refinery": "~0.4"
  }
}
```

Then configure the project's database connectivity settings:

```
// ciacme/application/config/database.php

// ...

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'mysqli',

    // ...
);
```

Note

Although using database connection is not required in using `Refinery`, this is only applicable when creating migration files based on an existing database. Please see [Creating from database](https://github.com/rougin/refinery?tab=readme-ov-file#creating-from-database) below for its usage.

Basic Usage
-----------

[](#basic-usage)

### Creating migration files

[](#creating-migration-files)

To create a new database migration, kindly run the `create` command:

```
$ vendor/bin/refinery create create_users_table
[PASS] "20241019044009_create_users_table.php" successfully created!
```

```
// ciacme/application/migrations/20241019044009_create_users_table.php

use Rougin\Refinery\Migration;

class Migration_create_users_table extends Migration
{
    /**
     * @return void
     */
    public function up()
    {
        $data = array('id' => array());
        $data['id']['type'] = 'integer';
        $data['id']['auto_increment'] = true;
        $data['id']['constraint'] = 10;
        $this->dbforge->add_field($data);
        $this->dbforge->add_key('id', true);

        $this->dbforge->create_table('users');
    }

    /**
     * @return void
     */
    public function down()
    {
        $this->dbforge->drop_table('users');
    }
}
```

Note

- The `Migration` class under `Refinery` is directly extended on `CI_Migration`.
- The created file will be in `migrations` directory under `application`. If it does not exists, `Refinery` will automatically create the specified directory.

When creating database migrations, `Refinery` will try to guess the expected output of `up` and `down` methods of a migration file based on its name (e.g., `add_name_in_users_table`):

```
$ vendor/bin/refinery create add_name_in_users_table
"20241019044035_add_name_in_users_table.php" has been created.
```

```
// ciacme/application/migrations/20241019044035_add_name_in_users_table.php

use Rougin\Refinery\Migration;

class Migration_add_name_in_users_table extends Migration
{
    /**
     * @return void
     */
    public function up()
    {
        $data = array('name' => array());
        $data['name']['type'] = 'varchar';
        $data['name']['auto_increment'] = false;
        $data['name']['constraint'] = 100;
        $data['name']['default'] = null;
        $data['name']['null'] = true;
        $data['name']['unsigned'] = false;
        $this->dbforge->add_column('users', $data);
    }

    /**
     * @return void
     */
    public function down()
    {
        $this->dbforge->drop_column('users', 'name');
    }
}
```

Please see the accepted keywords below when creating database migration files:

KeywordDescriptionExample`add`Adds new column to a table`add_name_in_users_table``create`Creates new table with `id` as the primary key`create_users_table``delete`Deletes a column from a table`delete_name_in_users_table``modify`Updates a column of a table`modify_name_in_users_table`### Running the migrations

[](#running-the-migrations)

Kindly use the `migrate` command to use the files for database migrations:

```
$ vendor/bin/refinery migrate
[INFO] Migrating "create_users_table"...
[PASS] "create_users_table" migrated!
[INFO] Migrating "add_name_in_users_table"...
[PASS] "add_name_in_users_table" migrated!
```

When running this command, the target timestamp (`--target`) will always be the latest file in the `migrations` directory if not specified (e.g., `add_name_in_users_table`). Use the `--target` option to migrate to a specific version:

```
$ vendor/bin/refinery migrate --target=20241019044009
[INFO] Migrating "create_users_table"...
[PASS] "create_users_table" migrated!
```

To rollback a database, kindly use the `rollback` command:

```
$ vendor/bin/refinery rollback
[INFO] Rolling back "add_name_in_users_table"...
[PASS] "add_name_in_users_table" rolled back!
```

Note

Without a `--target` option, the `rollback` will only revert to its previous version (e.g., `create_users_table`).

To reset back the database schema to version `0`, the `reset` command can be used:

```
$ vendor/bin/refinery migrate
[INFO] Migrating "add_name_in_users_table"...
[PASS] "add_name_in_users_table" migrated!

$ vendor/bin/refinery reset
[INFO] Rolling back "add_name_in_users_table"...
[PASS] "add_name_in_users_table" rolled back!
[INFO] Rolling back "create_users_table"...
[PASS] "create_users_table" rolled back!
```

Creating from database
----------------------

[](#creating-from-database)

`Refinery` also allows to create a database migration based on the existing database table. Prior in creating its database migration, kindly ensure that the specified table already exists in the database schema:

```
CREATE TABLE IF NOT EXISTS `users` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
```

After checking the database table exists, run the same `create` command with the `--from-database` option:

```
$ vendor/bin/refinery create create_users_table --from-database
"20241019044729_create_users_table.php" has been created.
```

```
// ciacme/application/migrations/20241019044729_create_users_table.php

use Rougin\Refinery\Migration;

class Migration_create_users_table extends Migration
{
    /**
     * @return void
     */
    public function up()
    {
        $data = array('id' => array());
        $data['id']['type'] = 'integer';
        $data['id']['auto_increment'] = true;
        $data['id']['constraint'] = 10;
        $this->dbforge->add_field($data);
        $this->dbforge->add_key('id', true);

        $data = array('name' => array());
        $data['name']['type'] = 'varchar';
        $data['name']['auto_increment'] = false;
        $data['name']['constraint'] = 100;
        $data['name']['default'] = null;
        $data['name']['null'] = true;
        $data['name']['unsigned'] = false;
        $this->dbforge->add_field($data);

        $this->dbforge->create_table('users');
    }

    /**
     * @return void
     */
    public function down()
    {
        $this->dbforge->drop_table('users');
    }
}
```

Note

The `--from-database` option only exists when creating files under the `create_*_table` prefix.

Creating sequential migrations
------------------------------

[](#creating-sequential-migrations)

By default, `Refinery` uses a timestamp prefix as its numbering style when creating migration files. To change it to a sequential numbering, kindly update the value of `migration_type` in the `config/migration.php` to `sequential`:

```
// ciacme/application/config/migration.php

/*
|--------------------------------------------------------------------------
| Migration Type
|--------------------------------------------------------------------------
|
| Migration file names may be based on a sequential identifier or on
| a timestamp. Options are:
|
|   'sequential' = Sequential migration naming (001_add_blog.php)
|   'timestamp'  = Timestamp migration naming (20121031104401_add_blog.php)
|                  Use timestamp format YYYYMMDDHHIISS.
|
| Note: If this configuration value is missing the Migration library
|       defaults to 'sequential' for backward compatibility with CI2.
|
*/
$config['migration_type'] = 'sequential';
```

Then run the `create` command to generate a migration file in sequential migration:

```
$ vendor/bin/refinery create create_users_table
[PASS] "001_create_users_table.php" successfully created!
```

Alternatively, the `--sequential` option can also be added in the `create` command to update the said configuration:

```
$ vendor/bin/refinery create add_name_in_users_table --sequential
[PASS] "002_add_name_in_users_table.php" successfully created!
```

Note

When using the `--sequential` option, the `migration_type` in the `config/migration.php` is also set as `sequential`.

Using `refinery.yml`
--------------------

[](#using-refineryyml)

`Refinery` currently works out of the box after the configuration based on `Installation`. However, using a `refinery.yml` can be used for complex setups like specifying the new application path:

```
# refinery.yml

app_path: %%CURRENT_DIRECTORY%%
```

To create a `refinery.yml`, simply run the `initialize` command:

```
$ vendor/bin/refinery initialize
[PASS] "refinery.yml" added successfully!
```

Note

`%%CURRENT_DIRECTORY%%` is a placeholder variable which is the current directory of `refinery.yml`.

### `app_path`

[](#app_path)

This property specifies the `application` directory. It may updated to any directory (e.g., `ciacme/application`, `ciacme/config`, etc.) as long it can detect the `config/config.php` file from the defined directory:

```
# refinery.yml

app_path: %%CURRENT_DIRECTORY%%/Sample

# ...
```

Note

`Refinery` will try to check the path specified in `app_path` if it is a valid `Codeigniter 3` project. Then it will perform another check if the `application` directory exists or if the `config` directory can be accessed directly from the directory defined in `app_path`.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](https://github.com/rougin/refinery/blob/master/CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Credits
-------

[](#credits)

- [All contributors](https://github.com/rougin/refinery/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](https://github.com/rougin/refinery/blob/master/LICENSE.md) for more information.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity55

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.

###  Release Activity

Cadence

Every ~342 days

Recently: every ~788 days

Total

11

Last Release

574d ago

PHP version history (2 changes)v0.1.0PHP &gt;=5.3.0

v0.2.0PHP &gt;=5.4.0

### Community

Maintainers

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

---

Top Contributors

[![rougin](https://avatars.githubusercontent.com/u/6078637?v=4)](https://github.com/rougin "rougin (128 commits)")

---

Tags

codeigniter-librarydatabase-migrationsgeneratorphp-libraryrefineryphpcodeigniterdatabase migrations

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rougin-refinery/health.svg)

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

###  Alternatives

[popphp/pop-db

Pop Db Component for Pop PHP Framework

1814.6k11](/packages/popphp-pop-db)[eftec/pdoone

Minimaist procedural PDO wrapper library

1105.9k9](/packages/eftec-pdoone)[simple-swoole/db

A db component for Simps.

216.3k3](/packages/simple-swoole-db)

PHPackages © 2026

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