php-bread-and-butter/codeignitermigration

Codeigniter 迁移工具

1.2.8 2022-01-25 14:28 UTC

This package is auto-updated.

Last update: 2024-09-25 20:00:33 UTC


README

自定义 CodeIgniter 空迁移包,支持命令行迁移并抑制迁移文件中运行 up 和 down 方法。增加了对 .env 文件配置的支持

要求

  • 要加载 .env 文件,“vlucas/phpdotenv” 包是必需的

安装

$ composer require php-bread-and-butter/codeignitermigration

用法

$ php vendor/php-bread-and-butter/codeignitermigration/ci migration:bare add_new_post_table

或者,您也可以切换到包目录。

$ cd vendor/php-bread-and-butter/codeignitermigration
$ php ci migration:bare add_new_post_table

上面的示例将创建一个新的迁移文件 application/migrations/{YmdHis}_add_new_post_table.php,其中 {YmdHis} 将是当前的时间戳。您也可以使用驼峰命名法为迁移命名,例如 AddNewPostTable,但工具会将它转换为 snake_case 形式,如 add_new_post_table。默认生成的内容如下

<?php defined('BASEPATH') or exit('No direct script access allowed');

class Migration_Add_New_Post_Table extends CI_Migration
{
    public function up() {
        // this up() migration is auto-generated, please modify it to your needs
        // Drop table 'table_name' if it exists
        $this->dbforge->drop_table('table_name', true);

        // Table structure for table 'table_name'
        $this->dbforge->add_field(array(
            'id' => array(
                'type' => 'MEDIUMINT',
                'constraint' => '8',
                'unsigned' => true,
                'auto_increment' => true
            ),
            'created_at' => array(
                'type' => 'DATETIME',
                'null' => false,
            )
        ));
        $this->dbforge->add_key('id', true);
        $this->dbforge->create_table('table_name');
    }

    public function down()
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->dbforge->drop_table('table_name', true);
    }
}

您可以根据需要修改文件。

命令的替代用法

$ php ci migration:bare add_new_column_to_post_table
$ php ci migration:bare modify_post_table
$ php ci migration:bare update_post_table
$ php ci migration:bare rename_post_table
$ php ci migration:bare remove_post_table

现在执行迁移

$ php vendor/php-bread-and-butter/codeignitermigration/migrate

可以使用以下方式回滚迁移

$ php vendor/php-bread-and-butter/codeignitermigration/suppress ${VERSION}

将 ${VERSION} 替换为 {YmdHis},其中 {YmdHis} 将是前一个迁移文件的时间戳,例如 20211002092605