定制 grofftech/wp-migrator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

grofftech/wp-migrator

Composer 安装命令:

composer require grofftech/wp-migrator

包简介

A WordPress plugin for facilitating database schema and data updates to be done with code

README 文档

README

WPMigrator is a WordPress plugin for allowing database migrations to be done with code.

Requirements

Installation

Installing this plugin assumes you are managing plugins for a WordPress site with composer or wp-packagist. It is currently not part of the WordPress plugin repository so it can't be installed with wp-packagist.

Add the following configuration to your composer.json. If you are managing plugins with wp-packagist, this configuration may already be present.

    "extra": {
        "installer-paths": {
            "plugins/{$name}/": ["type:wordpress-plugin"]
        }
    }

Install with composer

composer require grofftech\wp-migrator

Usage

This plugin is designed to be used in conjunction with another plugin, which hooks into the file paths to look for classes to perform the migrations. Ideally, the migrations can be performed with WordPress API methods. You can definitely use SQL to perform the migrations, with the recommendation usage of the global $wpdb object https://developer.wordpress.org/reference/classes/wpdb/.

Hooking into File Paths

Object orientated approach

\add_filter('wp_migrator_paths', array( $this, 'add_migrations_folder' ) );
public function add_migrations_folder() {
    $paths[] = 'directory/for/migration/files'; # Could use a constant here

    return $paths;
}

Functional programming approach

\add_filter('wp_migrator_paths', 'add_migrations_folder' );
public function add_migrations_folder() {
    $paths[] = 'directory/for/migration/files'; # Could use a constant here

    return $paths;
}

Adding a Migration

Create a migration class (any class name is fine) that defines a run method. A namespace is required. The migration class(es) must be autoloaded (with composer or something similar) or included/required before the migration is run.

<?php
namespace Grofftech\Migrations

class TestMigration {
    public function run() {
        // Code goes here
    }
}

Using WP-CLI, either run all pending migrations or run the migration by class name. Each time a migration is run, a record is saved to the database indicating it has been run.

wp migrate run # will run all pending migration classes not run before
wp migrate run --name=TestMigration

Rolling Back a Migration

In order to rollback a migration, define the rollback method in the migration class.

namespace Grofftech\Migrations;

class TestMigration {
    public function run() {
        // Code goes here
    }

    public function rollback() {
        // Rollback code here
    }
}

Using WP-CLI, rollback the migration using the class name. Rollbacks can only be performed on a previously run migration. If there is no rollback method the rollback will not run.

wp migrate rollback --name=TestMigration

Contributing

Pull requests welcome. Please open an issue so we can discuss.

License

GNU General Public License 2.0+

统计信息

  • 总下载量: 281
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2022-04-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固