From b96badae2bcaabe21492cbda8319d6dc180154ba Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 3 Jun 2021 13:37:18 +0100 Subject: [PATCH 1/9] Add ability to manually provide connection-info and action properties rather than rely on a record being available in the database --- class/command.php | 17 ++++++++++++++++- class/wpsdb-cli.php | 8 ++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/class/command.php b/class/command.php index cee217e..b54f428 100644 --- a/class/command.php +++ b/class/command.php @@ -23,8 +23,23 @@ class WPSDBCLI extends WP_CLI_Command { */ public function migrate( $args, $assoc_args ) { $profile = $args[0]; + $manual_profile = []; - $result = wpsdb_migrate( $profile ); + // Target manually (maybe no database available yet) + if ($assoc_args['connection-info'] && $assoc_args['action']) { + $manual_profile = array( + 'connection_info' => $assoc_args['connection-info'], + 'action' => $assoc_args['action'], + 'create_backup' => $assoc_args['create_backup'], + 'backup_option' => null, + 'prefixed_tables' => null, + 'select_backup' => null, + 'table_migrate_option' => null, + 'select_tables' => null, + ); + } + + $result = wpsdb_migrate( $profile, $manual_profile ); if ( true === $result ) { WP_CLI::success( __( 'Migration successful.', 'wp-sync-db-cli' ) ); diff --git a/class/wpsdb-cli.php b/class/wpsdb-cli.php index ab59116..9bc81d8 100644 --- a/class/wpsdb-cli.php +++ b/class/wpsdb-cli.php @@ -10,18 +10,18 @@ function __construct( $plugin_file_path ) { if( ! $this->meets_version_requirements( '1.4b1' ) ) return; } - function cli_migration( $profile ) { + function cli_migration( $profile, $manual_profile ) { global $wpsdb; $wpsdb_settings = get_option( 'wpsdb_settings' ); --$profile; if( ! $this->meets_version_requirements( '1.4b1' ) ) return $this->cli_error( __( 'Please update WP Sync DB.', 'wp-sync-db-cli' ) ); - if( ! isset( $profile ) ) return $this->cli_error( __( 'Profile ID missing.', 'wp-sync-db-cli' ) ); - if( ! isset( $wpsdb_settings['profiles'][$profile] ) ) return $this->cli_error( __( 'Profile ID not found.', 'wp-sync-db-cli' ) ); + if( ! isset( $profile ) && ! isset( $manual_profile ) ) return $this->cli_error( __( 'Profile ID missing.', 'wp-sync-db-cli' ) ); + if( ! isset( $manual_profile ) && ! isset( $wpsdb_settings['profiles'][$profile] ) ) return $this->cli_error( __( 'Profile ID not found.', 'wp-sync-db-cli' ) ); $this->set_time_limit(); $wpsdb->set_cli_migration(); - $profile = apply_filters( 'wpsdb_cli_profile_before_migration', $wpsdb_settings['profiles'][$profile] ); + $profile = apply_filters( 'wpsdb_cli_profile_before_migration', $manual_profile ?? $wpsdb_settings['profiles'][$profile] ); $connection_info = explode( "\n", $profile['connection_info'] ); $form_data = http_build_query( $profile ); From 2c627cd338dfa4f6f9c8be29d95a31407dba6149 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 8 Jul 2021 16:51:45 +0100 Subject: [PATCH 2/9] Correct signature to include new manual_profile option --- wp-sync-db-cli.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wp-sync-db-cli.php b/wp-sync-db-cli.php index e356bd0..c43b2e7 100644 --- a/wp-sync-db-cli.php +++ b/wp-sync-db-cli.php @@ -29,10 +29,10 @@ function wp_sync_db_cli_loaded() { } add_action( 'plugins_loaded', 'wp_sync_db_cli_loaded', 20 ); -function wpsdb_migrate( $profile ) { +function wpsdb_migrate( $profile, $manual_profile ) { global $wpsdb_cli; if( empty( $wpsdb_cli ) ) { return new WP_Error( 'wpsdb_cli_error', __( 'WP Sync DB CLI class not available', 'wp-sync-db-cli' ) ); } - return $wpsdb_cli->cli_migration( $profile ); + return $wpsdb_cli->cli_migration( $profile, $manual_profile ); } From b3dc3acd18aa0ae21372f0aaa2c8289364f52f93 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 8 Jul 2021 16:54:08 +0100 Subject: [PATCH 3/9] If we have a profile by the supplied ID, then use it. Otherwise, attempt to use the provided manual_profile --- class/wpsdb-cli.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/class/wpsdb-cli.php b/class/wpsdb-cli.php index 9bc81d8..67130a2 100644 --- a/class/wpsdb-cli.php +++ b/class/wpsdb-cli.php @@ -21,7 +21,8 @@ function cli_migration( $profile, $manual_profile ) { $this->set_time_limit(); $wpsdb->set_cli_migration(); - $profile = apply_filters( 'wpsdb_cli_profile_before_migration', $manual_profile ?? $wpsdb_settings['profiles'][$profile] ); + // If we have a profile by the supplied ID, then use it. Otherwise, attempt to use the provided manual_profile + $profile = apply_filters( 'wpsdb_cli_profile_before_migration', $wpsdb_settings['profiles'][$profile] ? $wpsdb_settings['profiles'][$profile] : $manual_profile ); $connection_info = explode( "\n", $profile['connection_info'] ); $form_data = http_build_query( $profile ); From 77633fe7586f35157ae60892e7a74dca2c69467b Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 8 Jul 2021 16:55:00 +0100 Subject: [PATCH 4/9] Update the command signature to use optional parameters. Update the manual_profile defaults to only need connection info and action in order to work. --- class/command.php | 86 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 18 deletions(-) diff --git a/class/command.php b/class/command.php index b54f428..4d2bcab 100644 --- a/class/command.php +++ b/class/command.php @@ -3,53 +3,103 @@ /** * Migrate your DB using WP Sync DB. */ -class WPSDBCLI extends WP_CLI_Command { +class WPSDBCLI extends WP_CLI_Command +{ /** - * Run a migration. + * Run a migration. Either profile id or connection-info + action are required. * * ## OPTIONS * - * + * [--profile=] * : ID of the profile to use for the migration. + * + * [--connection-info=] + * : Manual connection info for when a profile by the ID is not found. The above ID will be used to save a copy + * + * [--action=] + * : The type of action to perform against the target connection + * --- + * default: pull + * options: + * - pull + * - push + * --- * + * [--create-backup=] + * : Whether to take a backup before running the action. + * --- + * default: 0 + * options: + * - 0 + * - 1 + * --- + * * ## EXAMPLES * - * wp wpsdb migrate 1 + * wp wpsdb migrate --profile=1 + * wp wpsdb migrate --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1 * - * @synopsis + * @synopsis [--profile=] [--connection-info=] [--action=] [--create-backup=] * * @since 1.0 */ - public function migrate( $args, $assoc_args ) { - $profile = $args[0]; + public function migrate($args, $assoc_args) + { + $profile = null; $manual_profile = []; // Target manually (maybe no database available yet) + + if ($assoc_args['profile']) { + $profile = $assoc_args['profile']; + } if ($assoc_args['connection-info'] && $assoc_args['action']) { + // Preprocess some variables + $connection_info = stripcslashes($assoc_args['connection-info']); + $connection_info_segments = explode("\n", $connection_info); + $friendly_name = preg_replace("(^https?://)", "", $connection_info_segments[0]); + + // Create a default profile, that will save afterwards $manual_profile = array( - 'connection_info' => $assoc_args['connection-info'], + 'connection_info' => $connection_info, 'action' => $assoc_args['action'], - 'create_backup' => $assoc_args['create_backup'], - 'backup_option' => null, - 'prefixed_tables' => null, + 'create_backup' => $assoc_args['create-backup'], + 'backup_option' => "backup_only_with_prefix", 'select_backup' => null, - 'table_migrate_option' => null, 'select_tables' => null, + 'table_migrate_option' => "migrate_only_with_prefix", + 'exclude_transients' => 1, + 'media_files' => 1, + 'remove_local_media' => 1, + 'save_migration_profile_option' => 0, + 'create_new_profile' => $friendly_name, + 'name' => $friendly_name, + 'save_computer' => 0, + 'gzip_file' => 1, + 'replace_guids' => 1, + 'exclude_spam' => 0, + 'keep_active_plugins' => 1, + 'exclude_post_types' => 0 ); } - $result = wpsdb_migrate( $profile, $manual_profile ); + if ($profile == null && empty($manual_profile)) { + WP_CLI::warning(__('Either profile id or connection-info + action are required.', 'wp-sync-db-cli')); + WP_CLI::log('Usage: wpsdb migrate [--profile=] [--connection-info=] [--action=] [--create-backup=]'); + return; + } + + $result = wpsdb_migrate($profile, $manual_profile); - if ( true === $result ) { - WP_CLI::success( __( 'Migration successful.', 'wp-sync-db-cli' ) ); + if (true === $result) { + WP_CLI::success(__('Migration successful.', 'wp-sync-db-cli')); return; } - WP_CLI::warning( $result->get_error_message() ); + WP_CLI::warning($result->get_error_message()); return; } - } -WP_CLI::add_command( 'wpsdb', 'WPSDBCLI' ); +WP_CLI::add_command('wpsdb', 'WPSDBCLI'); From 3af0fd182c8384dd668c3af381c4b519c9ba7ef4 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 8 Jul 2021 17:03:15 +0100 Subject: [PATCH 5/9] Default the backup to 1 as it's the safer option --- class/command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class/command.php b/class/command.php index 4d2bcab..bfa9ecb 100644 --- a/class/command.php +++ b/class/command.php @@ -29,7 +29,7 @@ class WPSDBCLI extends WP_CLI_Command * [--create-backup=] * : Whether to take a backup before running the action. * --- - * default: 0 + * default: 1 * options: * - 0 * - 1 From 8f4bf9d4221f53c7a36aaa708e4576087da91ee0 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 8 Jul 2021 17:07:25 +0100 Subject: [PATCH 6/9] Update README.md Explain the new and updated usages --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 6618001..aa57a00 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ # WP Sync DB CLI An addon for [WP Sync DB](https://github.com/slang800/wp-sync-db) that allows you to execute migrations using a function call or via WP-CLI + +## Example usage +The following two variations of the `wpsdb migrate` command are possible by supplying either a profile id or connection info string. + +#### Target a user created profile already stored in the system/database +`wp wpsdb migrate --profile=1` + +**profile** represents the profile number as seen in the Migrate tab's "saved migration profiles" list in WP. + +#### Manually target a connection string (no profile) +`wp wpsdb migrate --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1` + +**connection-info** is the string found on the target site's Settings tab. The line break is replaced with the `\n` equivalent character in order to pass the whole string on a single line. + +**action** can be set to either `pull` or `push` depending on the direction of the DB sync + +**create-backup** is a bit field that indicates whether the DB should be backed up prior to a transfer. This defaults to 1. From aee137d25f7851010546f48804fb8252b8e61a03 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 8 Jul 2021 17:18:01 +0100 Subject: [PATCH 7/9] Update README.md Include third example of usage --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index aa57a00..4cb67e0 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,8 @@ The following two variations of the `wpsdb migrate` command are possible by supp **action** can be set to either `pull` or `push` depending on the direction of the DB sync **create-backup** is a bit field that indicates whether the DB should be backed up prior to a transfer. This defaults to 1. + +#### Target a profile and fallback to a connection string +`wp wpsdb migrate --profile=1 --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1` + +In this example we can attempt to target a profile that doesn't yet exist before the migration and instead fallback to the manual profile arguments provided. Once the migration is complete if we re-run this command, then the profile will have been migrated over and this will be used. This is useful from an automation point of view. From 8cc547382920c7b8c02b04f3906eeb370d60e8c6 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 8 Jul 2021 17:18:28 +0100 Subject: [PATCH 8/9] Include 3rd example --- class/command.php | 1 + 1 file changed, 1 insertion(+) diff --git a/class/command.php b/class/command.php index bfa9ecb..c03a598 100644 --- a/class/command.php +++ b/class/command.php @@ -39,6 +39,7 @@ class WPSDBCLI extends WP_CLI_Command * * wp wpsdb migrate --profile=1 * wp wpsdb migrate --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1 + * wp wpsdb migrate --profile=1 --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1 * * @synopsis [--profile=] [--connection-info=] [--action=] [--create-backup=] * From 13b4b74b22bfcd3549e4f682299290be7538b848 Mon Sep 17 00:00:00 2001 From: Joseph D'Souza Date: Thu, 3 Mar 2022 16:30:10 +0000 Subject: [PATCH 9/9] Update references --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 7232579..aeb3c55 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "wp-sync-db/wp-sync-db-cli", + "name": "josephdsouza86/wp-sync-db-cli", "type": "wordpress-plugin", - "homepage": "https://github.com/wp-sync-db/wp-sync-db-cli", + "homepage": "https://github.com/josephdsouza86/wp-sync-db-cli", "license": "GPL-2.0", "description": "WP Sync DB CLI Addon", "keywords": ["plugin","wordpress","wp-sync-db","cli"],