Class: Wordmove::Actions::AdaptRemoteDb
- Inherits:
-
Object
- Object
- Wordmove::Actions::AdaptRemoteDb
- Extended by:
- LightService::Action
- Includes:
- Helpers, WpcliHelpers
- Defined in:
- lib/wordmove/actions/adapt_remote_db.rb
Overview
This action is not responsible to download the remote DB nor to backup any DB at all. It expects to find a dump of the remote DB into context.db_paths.local.gzipped_path (SSH) or context.db_paths.local.path (FTP), otherwise it will fail and stop the procedure.
Adapt the remote DB for the local destination. “To adapt” in Wordmove jargon means to transform URLs strings into the database. This action will substitute remote URLs with local ones, in order to make the DB to work correctly once pulled to the local wordpress installation.
Class Method Summary collapse
-
.execute ⇒ LightService::Context
Action’s context.
- .import_db_command(context) ⇒ Object
-
.search_replace_command(context, config_key) ⇒ String
Compose and returns the search-replace command.
-
.uncompress_command(context) ⇒ String
Construct the command to deflate a compressed file as a string.
-
.wpcli_search_replace_patterns(config_key, from, to) ⇒ Object
Builds the regex search pattern and replacement string for wp search-replace.
Methods included from WpcliHelpers
Class Method Details
.execute ⇒ LightService::Context
Returns Action’s context.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/wordmove/actions/adapt_remote_db.rb', line 32 executed do |context| # rubocop:disable Metrics/BlockLength context.logger.task 'Adapt remote DB' unless wp_in_path? raise UnmetPeerDependencyError, 'WP-CLI is not installed or not in your $PATH' end next context if simulate?(cli_options: context.) if File.exist?(context.db_paths.local.gzipped_path) context.logger.task_step true, uncompress_command(context) begin system(uncompress_command(context), exception: true) rescue RuntimeError, SystemExit => e context.fail_and_return!("Local command status reports an error: #{e.}") end end unless File.exist?(context.db_paths.local.path) context.fail_and_return!( "Cannot find the dump file to adapt in #{context.db_paths.local.path}" ) end context.logger.task_step true, import_db_command(context) begin system(import_db_command(context), exception: true) rescue RuntimeError, SystemExit => e context.fail_and_return!("Local command status reports an error: #{e.}") end if context.[:no_adapt] context.logger.warn 'Skipping DB adapt' next context end %i[vhost wordpress_path].each do |key| command = search_replace_command(context, key) context.logger.task_step true, command begin system(command, exception: true) rescue RuntimeError, SystemExit => e context.fail_and_return!("Local command status reports an error: #{e.}") end end context.logger.success 'Local DB adapted' end |
.import_db_command(context) ⇒ Object
94 95 96 97 |
# File 'lib/wordmove/actions/adapt_remote_db.rb', line 94 def self.import_db_command(context) "wp db import #{context.db_paths.local.path} --allow-root --quiet " \ "--path=#{wpcli_config_path(context)}" end |
.search_replace_command(context, config_key) ⇒ String
Compose and returns the search-replace command. It’s intended to be used from a LightService::Action
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/wordmove/actions/adapt_remote_db.rb', line 106 def self.search_replace_command(context, config_key) unless %i[vhost wordpress_path].include?(config_key) raise ArgumentError, "Unexpected `config_key` #{config_key}.:vhost" \ 'or :wordpress_path expected' end from = context.dig(:remote_options, config_key) to = context.dig(:local_options, config_key) search, replace = wpcli_search_replace_patterns(config_key, from, to) [ 'wp search-replace', "--path=#{wpcli_config_path(context)}", search, replace, '--regex-delimiter="|"', '--regex', '--precise', '--quiet', '--skip-columns=guid', '--all-tables', '--allow-root' ].join(' ') end |
.uncompress_command(context) ⇒ String
Construct the command to deflate a compressed file as a string.
86 87 88 89 90 91 92 |
# File 'lib/wordmove/actions/adapt_remote_db.rb', line 86 def self.uncompress_command(context) command = ['gzip'] command << '-d' command << '-f' command << "\"#{context.db_paths.local.gzipped_path}\"" command.join(' ') end |
.wpcli_search_replace_patterns(config_key, from, to) ⇒ Object
Builds the regex search pattern and replacement string for wp search-replace.
For vhost: no anchors so the URL is replaced everywhere it appears (post content, meta, serialized data). Regexp.escape handles special chars in the URL. For wordpress_path: exact anchors (A…Z) prevent replacing substrings (e.g. /html appearing inside text/html — see issue #616).
137 138 139 140 141 142 143 |
# File 'lib/wordmove/actions/adapt_remote_db.rb', line 137 def self.wpcli_search_replace_patterns(config_key, from, to) if config_key == :vhost [Regexp.escape(from).shellescape, to.shellescape] else ["\\A#{Regexp.escape(from)}\\Z".shellescape, to.shellescape] end end |