Class: Wordmove::Actions::AdaptLocalDb
- Inherits:
-
Object
- Object
- Wordmove::Actions::AdaptLocalDb
- Extended by:
- LightService::Action
- Includes:
- Helpers, WpcliHelpers
- Defined in:
- lib/wordmove/actions/adapt_local_db.rb
Overview
Adapt the local DB for the remote destination. “To adapt” in Wordmove jargon means to transform URLs strings into the database. This action will substitute local URLs with remote ones, in order to make the DB to work correctly once pushed to the remote wordpress installation.
Class Method Summary collapse
- .compress_command(context) ⇒ Object
- .dump_adapted_command(context) ⇒ Object
- .dump_command(context) ⇒ Object
-
.execute ⇒ LightService::Context
Action’s context.
- .import_original_db_command(context) ⇒ Object
-
.search_replace_command(context, config_key) ⇒ String
Compose and returns the search-replace command.
-
.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
.compress_command(context) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/wordmove/actions/adapt_local_db.rb', line 102 def self.compress_command(context) command = ['nice'] command << '-n' command << '0' command << 'gzip' command << '-9' command << '-f' command << "\"#{context.db_paths.local.adapted_path}\"" command.join(' ') end |
.dump_adapted_command(context) ⇒ Object
92 93 94 95 |
# File 'lib/wordmove/actions/adapt_local_db.rb', line 92 def self.dump_adapted_command(context) "wp db export #{context.db_paths.local.adapted_path} --allow-root --quiet " \ "--path=#{wpcli_config_path(context)}" end |
.dump_command(context) ⇒ Object
87 88 89 90 |
# File 'lib/wordmove/actions/adapt_local_db.rb', line 87 def self.dump_command(context) "wp db export #{context.db_paths.local.path} --allow-root --quiet " \ "--path=#{wpcli_config_path(context)}" end |
.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 80 81 82 83 84 85 |
# File 'lib/wordmove/actions/adapt_local_db.rb', line 32 executed do |context| # rubocop:disable Metrics/BlockLength context.logger.task 'Adapt local 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.) context.logger.task_step true, dump_command(context) begin system(dump_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' else %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 end context.logger.task_step true, dump_adapted_command(context) begin system(dump_adapted_command(context), exception: true) rescue RuntimeError, SystemExit => e context.fail_and_return!("Local command status reports an error: #{e.}") end if context.photocopier.is_a? Photocopier::SSH context.logger.task_step true, compress_command(context) begin system(compress_command(context), exception: true) rescue RuntimeError, SystemExit => e context.fail_and_return!("Local command status reports an error: #{e.}") end end context.logger.task_step true, import_original_db_command(context) begin system(import_original_db_command(context), exception: true) rescue RuntimeError, SystemExit => e context.fail_and_return!("Local command status reports an error: #{e.}") end end |
.import_original_db_command(context) ⇒ Object
97 98 99 100 |
# File 'lib/wordmove/actions/adapt_local_db.rb', line 97 def self.import_original_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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/wordmove/actions/adapt_local_db.rb', line 120 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(:local_options, config_key) to = context.dig(:remote_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 |
.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).
151 152 153 154 155 156 157 |
# File 'lib/wordmove/actions/adapt_local_db.rb', line 151 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 |