2017 © Pedro Peláez
 

library refinery

"Ready-to-eat" migrations for the Codeigniter framework.

image

rougin/refinery

"Ready-to-eat" migrations for the Codeigniter framework.

  • Saturday, July 28, 2018
  • by rougin
  • Repository
  • 1 Watchers
  • 13 Stars
  • 2,484 Installations
  • PHP
  • 0 Dependents
  • 5 Suggesters
  • 4 Forks
  • 0 Open issues
  • 12 Versions
  • 7 % Grown

The README.md

Refinery

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-coverage] Total Downloads, (*1)

Refinery is a console-based package of Migrations Class for the Codeigniter 3. It uses the Describe package for retrieving the database tables for creating database migrations., (*2)

Installation

From an existing Codeigniter 3 project, the Refinery package can be installed through Composer:, (*3)

``` bash $ composer require rougin/refinery --dev, (*4)


``` json // 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:, (*5)

``` php // ciacme/application/config/database.php, (*6)

// ..., (*7)

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

// ...

);, (*9)


> [!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 ### Creating migration files To create a new database migration, kindly run the `create` command: ``` bash $ vendor/bin/refinery create create_users_table [PASS] "20241019044009_create_users_table.php" successfully created!

``` php // ciacme/application/migrations/20241019044009_create_users_table.php, (*10)

use Rougin\Refinery\Migration;, (*11)

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);, (*12)

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

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

}, (*13)


> [!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`): ```bash $ vendor/bin/refinery create add_name_in_users_table "20241019044035_add_name_in_users_table.php" has been created.

``` php // ciacme/application/migrations/20241019044035_add_name_in_users_table.php, (*14)

use Rougin\Refinery\Migration;, (*15)

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); }, (*16)

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

}, (*17)


Please see the accepted keywords below when creating database migration files: | Keyword | Description | Example | |----------|------------------------------------------------|------------------------------| | `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 Kindly use the `migrate` command to use the files for database migrations: ``` bash $ 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:, (*18)

``` bash $ vendor/bin/refinery migrate --target=20241019044009 [INFO] Migrating "create_users_table"... [PASS] "create_users_table" migrated!, (*19)


To rollback a database, kindly use the `rollback` command: ``` bash $ 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)., (*20)

To reset back the database schema to version 0, the reset command can be used:, (*21)

``` bash $ vendor/bin/refinery migrate [INFO] Migrating "add_name_in_users_table"... [PASS] "add_name_in_users_table" migrated!, (*22)

$ 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!, (*23)


## 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: ``` sql 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:, (*24)

``` bash $ vendor/bin/refinery create create_users_table --from-database "20241019044729_create_users_table.php" has been created., (*25)


``` php // 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., (*26)

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:, (*27)

``` php // ciacme/application/config/migration.php, (*28)

/* |-------------------------------------------------------------------------- | 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';, (*29)


Then run the `create` command to generate a migration file in sequential migration: ``` bash $ 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:, (*30)

``` bash $ vendor/bin/refinery create add_name_in_users_table --sequential [PASS] "002_add_name_in_users_table.php" successfully created!, (*31)


> [!NOTE] > When using the `--sequential` option, the `migration_type` in the `config/migration.php` is also set as `sequential`. ## Using `refinery.yml` `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: ``` yaml # refinery.yml app_path: %%CURRENT_DIRECTORY%%

To create a refinery.yml, simply run the initialize command:, (*32)

``` bash $ vendor/bin/refinery initialize [PASS] "refinery.yml" added successfully!, (*33)


> [!NOTE] > `%%CURRENT_DIRECTORY%%` is a placeholder variable which is the current directory of `refinery.yml`. ### `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: ``` yaml # 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., (*34)

Changelog

Please see CHANGELOG for more information what has changed recently., (*35)

Testing

bash $ composer test, (*36)

Credits

License

The MIT License (MIT). Please see LICENSE for more information., (*37)

The Versions

28/07 2018

dev-master

9999999-dev https://rougin.github.io/refinery

"Ready-to-eat" migrations for the Codeigniter framework.

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php codeigniter refinery

06/01 2017

v0.3.0

0.3.0.0 https://github.com/rougin/refinery

'Ready-to-eat' migrations for Codeigniter

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php codeigniter refinery

29/12 2016

0.3.0.x-dev

0.3.0.9999999-dev https://github.com/rougin/refinery

'Ready-to-eat' migrations for Codeigniter

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php refinery

14/09 2016

v0.2.1

0.2.1.0 https://github.com/rougin/refinery

'Ready-to-eat' migrations for Codeigniter

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php refinery

22/05 2016

v0.2.0

0.2.0.0 https://github.com/rougin/refinery

'Ready-to-eat' migrations for Codeigniter

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php refinery

04/03 2016

v0.1.6

0.1.6.0 https://github.com/rougin/refinery

'Ready-to-eat' migrations for Codeigniter

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php refinery

03/03 2016

v0.1.5

0.1.5.0 https://github.com/rougin/refinery

'Ready-to-eat' migrations for Codeigniter

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php refinery

23/10 2015

v0.1.4

0.1.4.0 https://github.com/rougin/spark-plug

'Ready-to-eat' migrations for Codeigniter

  Sources   Download

MIT

The Requires

 

The Development Requires

database migrations php refinery

19/09 2015

v0.1.3

0.1.3.0

A command line interface for Migrations Class in CodeIgniter

  Sources   Download

MIT

The Requires

 

by Rougin Gutib

database migrations php refinery

26/06 2015

v0.1.2

0.1.2.0

A command line interface for Migrations Class in CodeIgniter

  Sources   Download

MIT

The Requires

 

by Rougin Gutib

database migrations php refinery

07/06 2015

v0.1.1

0.1.1.0

A command line interface for Migrations Class in CodeIgniter

  Sources   Download

MIT

The Requires

 

by Rougin Gutib

database migrations php refinery

07/06 2015

v0.1.0

0.1.0.0

A command line interface for Migrations Class in CodeIgniter

  Sources   Download

MIT

The Requires

 

by Rougin Gutib

database migrations php refinery