ignitekit/wp-migrations

Laravel 启发下的 WordPress 数据库迁移

1.0.0 2020-09-26 22:30 UTC

This package is auto-updated.

Last update: 2024-09-27 07:28:12 UTC


README

由 Laravel 启发创建的 WordPress 迁移。

如果您在各个插件中创建了自定义的 BaseMigration 类,则可以使用此包在不同的插件中。

在正确设置此包之前,请按照以下步骤操作。

说明

1.) 创建名为 BaseMigration 的基类

namespace MyPlugin\Database\Migrations;

use IgniteKit\WP\Migrations\Engine\Migration;
use IgniteKit\WP\Migrations\Contracts\Migration as MigrationContract;

class BaseMigration extends Migration implements MigrationContract {

    /**
     * Specify custom migrations table
     * @var string
     */
    protected $migrationsTableName = 'your_migrations_table';

}

2.) 创建迁移表设置迁移。例如 SetupMigrations

namespace MyPlugin\Database\Migrations;

use IgniteKit\WP\Migrations\Engine\Setup;

class CreateMigrationsTable extends BaseMigration {
	use Setup;
}

3.) 现在,您可以创建自己的自定义迁移。在这个例子中,创建一个 fruits 表。

namespace MyPlugin\Database\Migrations;

use IgniteKit\WP\Migrations\Database\Blueprint;
use IgniteKit\WP\Migrations\Wrappers\Schema;

class CreateFruitsTable extends BaseMigration {

	/**
	 * Put the table up
	 *
	 * @return void
	 */
	public function up() {
		Schema::create( 'fruits', function ( Blueprint $table ) {
			$table->bigIncrements( 'id' );
			$table->string( 'name' );
		} );
	}

	/**
	 * Default down function
	 *
	 * @return void
	 */
	public function down() {
		Schema::drop( 'fruits' );
	}

}

4.) 最后,注册这些迁移

/**
 * Registers the plugin migrations
 * @return void
 */
function my_plugin_register_migrations() {
    
    // Boot the CLI interface
    if(!class_exists('\IgniteKit\WP\Migrations\CLI\Boot')) {
        return;
    }
    \IgniteKit\WP\Migrations\CLI\Boot::start();
    
    // Register the migrations table migration
    CreateMigrationsTable::register();

    // Register the other migrations
    // NOTE: The squence is important!
    CreateFruitsTable::register();
    // ... other

}
add_action('plugins_loaded', 'my_plugin_register_migrations');

创建列

您可以使用表迁移创建许多不同的列类型。以下是一个列表

贡献

如果您有兴趣为该项目做出贡献,请随时提交您的 issue 或 pull request。

许可

Copyright (C) 2020 Darko Gjorgjijoski (https://darkog.com)

This file is part of wp-migrations

WP Migrations is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

WP Migrations  is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with WP Migrations. If not, see <https://gnu.ac.cn/licenses/>.