rougin/refinery

为 Codeigniter 框架提供的“即食”迁移。

v0.3.0 2017-01-06 16:26 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:27 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Refinery 是 Codeigniter 框架的 迁移类 的扩展和命令行界面。它使用 Describe 库来检索数据库表,并将其作为代码生成的依据。

安装

通过 Composer 安装 Refinery

$ composer require rougin/refinery

基本用法

创建一个表

$ vendor/bin/refinery create create_users_table
"20180621090905_create_users_table.php" has been created.
// application/migrations/20180621090905_create_users_table.php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_create_users_table extends CI_Migration
{
    public function up()
    {
        $this->dbforge->add_field('id');

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

    public function down()
    {
        $this->dbforge->drop_table('users');
    }
}

使用 --from-database 选项从现有数据库表创建迁移。

CREATE TABLE IF NOT EXISTS `user` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `name` varchar(200) NOT NULL,
    `age` int(2) NOT NULL,
    `gender` varchar(10) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
$ vendor/bin/refinery create create_users_table --from-database
"20180621090905_create_users_table.php" has been created.
// application/migrations/20180621090905_create_users_table.php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_create_users_table extends CI_Migration
{
    public function up()
    {
        $this->dbforge->add_column('users', array(
            'gender' => array(
                'type' => 'string',
                'constraint' => 10,
                'auto_increment' => FALSE,
                'default' => '',
                'null' => FALSE,
                'unsigned' => FALSE
            ),
        ));

        $this->dbforge->add_column('users', array(
            'age' => array(
                'type' => 'integer',
                'constraint' => 2,
                'auto_increment' => FALSE,
                'default' => '',
                'null' => FALSE,
                'unsigned' => FALSE
            ),
        ));

        $this->dbforge->add_column('users', array(
            'name' => array(
                'type' => 'string',
                'constraint' => 200,
                'auto_increment' => FALSE,
                'default' => '',
                'null' => FALSE,
                'unsigned' => FALSE
            ),
        ));

        $this->dbforge->add_column('users', array(
            'id' => array(
                'type' => 'integer',
                'constraint' => 10,
                'auto_increment' => TRUE,
                'default' => '',
                'null' => FALSE,
                'unsigned' => FALSE
            ),
        ));

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

    public function down()
    {
        $this->dbforge->drop_table('users');
    }
}

在表中创建列

$ vendor/bin/refinery create add_name_in_users_table
"20180621090953_add_name_in_users_table.php" has been created.
// application/migrations/20180621090953_add_name_in_users_table.php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_add_name_in_users_table extends CI_Migration
{
    public function up()
    {
        $this->dbforge->add_column('users', array(
            'name' => array(
                'type' => 'VARCHAR',
                'constraint' => 50,
                'auto_increment' => FALSE,
                'default' => '',
                'null' => FALSE,
                'unsigned' => FALSE
            ),
        ));
    }

    public function down()
    {
        $this->dbforge->drop_column('users', 'name');
    }
}

可用关键词

迁移、回滚和重置

$ vendor/bin/refinery migrate
Migrating: 20180621090905_create_users_table
Migrated:  20180621090905_create_users_table
Migrating: 20180621090953_add_name_in_users_table
Migrated:  20180621090953_add_name_in_users_table
$ vendor/bin/refinery rollback
Rolling back: 20180621090953_add_name_in_users_table
Rolled back:  20180621090953_add_name_in_users_table

注意:您也可以使用 --version 选项指定要回滚的版本。例如:--version=20180621090905

$ vendor/bin/refinery rollback 20180621090905
Rolling back: 20180621090905_create_users_table
Rolled back:  20180621090905_create_users_table
$ vendor/bin/refinery reset
Rolling back: 20180621090953_add_name_in_users_table
Rolled back:  20180621090953_add_name_in_users_table
Rolling back: 20180621090905_create_users_table
Rolled back:  20180621090905_create_users_table

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

测试

$ composer test

致谢

许可证

MIT 许可证 (MIT)。请参阅 LICENSE 了解更多信息。