enshtein/wp-migrations

WordPress数据库表架构升级和数据初始化的库。

安装: 3

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

类型:项目

1.0.3 2022-10-18 16:48 UTC

This package is auto-updated.

Last update: 2024-09-18 21:03:12 UTC


README

WordPress数据库表架构升级和数据初始化的库。

安装

  • composer require enshtein/wp-migrations
  • 通过在mu-plugin中添加\Enshtein\WpMigrations\Migrate::instance();来启动该包。

迁移

默认情况下,命令将在与vendor文件夹并列的migrations目录中查找迁移文件。

使用方法

使用WP CLI在命令行上运行wp migrate,将执行任何尚未运行的迁移。

创建迁移文件

wp migrate create add_price_table

<?php

use Enshtein\WpMigrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        global $wpdb;

        // up migration code
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        global $wpdb;

        // down migration code
    }
};

wp migrate create add_price_table --table=price

<?php

use Enshtein\WpMigrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        global $wpdb;

        $_sql = "CREATE TABLE `{$wpdb->prefix}price` (
           `id` int(10) NOT NULL auto_increment,
           PRIMARY KEY (id)
        ) {$this->collation()}";
				
        dbDelta($_sql);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        global $wpdb;

        $wpdb->query("DROP TABLE IF EXISTS `{$wpdb->prefix}price`");
    }
};

其他迁移命令

  • wp migrate reset - 命令将回滚所有应用程序的迁移

  • wp migrate refresh - 命令将回滚所有迁移,然后执行迁移命令

  • wp migrate rollback - 命令回滚最后一个“批次”的迁移,可能包括多个迁移文件

  • wp migrate rollback --step=3 - 您可以通过提供步骤选项给回滚命令来回滚有限数量的迁移

  • wp mimgrate status - 如果您想查看已运行的迁移

配置

\Enshtein\WpMigrations\Migrate::instance([
    'command' => 'migrate',
    'table_name' => 'migrations',
    'folder' => 'migrations',
    'path' => '',
    'filename' => 'Y_m_d_His_',
]);
  • command - 存储迁移的数据库表名
  • table_name - 运行WP-CLI的主要命令
  • folder - 迁移文件所在的文件夹名
  • path - 迁移文件所在目录的绝对路径
  • filename - 迁移文件名模板