Skip to content

Commit

Permalink
Move Scaffold functionality to wp scaffold sub command
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain committed May 4, 2023
1 parent bbd40df commit 33cf17e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ You can run specific migrations using the filename as an argument, eg. `wp dbi m

To rollback all migrations you can run `wp dbi migrate --rollback`, or just a specific migration `wp dbi migrate AddCustomTable --rollback`.

To quickly scaffold a new migration you can run `wp dbi migrate <name> --scaffold`. For example, `wp dbi migration MyMigration --scaffold` will create a new class named `MyMigration` in the default migration files directory with the correct filename and all required boilerplate code.
To quickly scaffold a new migration you can run `wp scaffold migration <name>`. For example, `wp scaffold migration MyMigration` will create a new class named `MyMigration` in the default migration files directory with the correct filename and all required boilerplate code.
19 changes: 1 addition & 18 deletions src/CLI/Command.php → src/CLI/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DeliciousBrains\WPMigrations\CLI;

class Command extends \WP_CLI_Command {
class Migrate extends \WP_CLI_Command {

/**
* Data migration command
Expand All @@ -18,9 +18,6 @@ class Command extends \WP_CLI_Command {
* [--setup]
* : Set up the migrations table
*
* [--scaffold]
* : Scaffold a new migration class using the migration stub
*
* @param array $args
* @param array $assoc_args
*
Expand All @@ -44,20 +41,6 @@ public function __invoke( $args, $assoc_args ) {
$migration = $args[0];
}

if ( isset( $assoc_args['scaffold'] ) ) {
if ( ! $migration ) {
return \WP_CLI::error( 'Migration name must be specified when using --scaffold' );
}

$filename = $migrator->scaffold( $migration );

if ( is_wp_error( $filename ) ) {
return \WP_ClI::error( $filename->get_error_message() );
}

return \WP_CLI::success( "Created {$filename}!" );
}

$rollback = false;
if ( $migration && isset( $assoc_args['rollback'] ) ) {
// Can only rollback specific migration
Expand Down
50 changes: 50 additions & 0 deletions src/CLI/Scaffold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace DeliciousBrains\WPMigrations\CLI;

class Scaffold extends \WP_CLI_Command {

/**
* Data migration command
*
* ## OPTIONS
*
* [<migration>]
* : Class name for the migration
*
* [--rollback]
* : If we are reverting a migration
*
* [--setup]
* : Set up the migrations table
*
* [--scaffold]
* : Scaffold a new migration class using the migration stub
*
* @param array $args
* @param array $assoc_args
*
* @throws \WP_CLI\ExitException
*/
public function __invoke( $args, $assoc_args ) {
$migrator = \DeliciousBrains\WPMigrations\Database\Migrator::instance();

$migration = null;
if ( ! empty( $args[0] ) ) {
$migration = $args[0];
}


if ( ! $migration ) {
return \WP_CLI::error( 'Migration name must be specified when using --scaffold' );
}

$filename = $migrator->scaffold( $migration );

if ( is_wp_error( $filename ) ) {
return \WP_ClI::error( $filename->get_error_message() );
}

return \WP_CLI::success( "Created {$filename}" );
}
}
6 changes: 4 additions & 2 deletions src/Database/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace DeliciousBrains\WPMigrations\Database;

use DeliciousBrains\WPMigrations\CLI\Command;
use DeliciousBrains\WPMigrations\CLI\Migrate;
use DeliciousBrains\WPMigrations\CLI\Scaffold;

class Migrator {

Expand Down Expand Up @@ -34,7 +35,8 @@ public function init( $command_name ) {
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
\WP_CLI::add_command( $command_name . ' migrate', Command::class );
\WP_CLI::add_command( $command_name . ' migrate', Migrate::class );
\WP_CLI::add_command( 'scaffold migration', Scaffold::class );
}
}

Expand Down

0 comments on commit 33cf17e

Please sign in to comment.