5 * Drush integration for media_entity.
8 use Drush\Log\LogLevel;
11 * Implements drush_hook_COMMAND_validate().
13 function drush_media_entity_updatedb_validate() {
14 // This hook exists because when running DB updates using drush,
15 // hook_requirements() is not enforced (see
16 // https://github.com/drush-ops/drush/pull/2708 for more info on that).
17 // Here we just re-evaluate all the checks from hook_requirements() and abort
18 // the "updatedb/updb" command by returning FALSE to this hook if any of them
20 drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_FULL);
22 // Normally users should remove Media Entity from the codebase after the
23 // upgrade. However, if they don't do so, this function will be called on
24 // subsequent DB upgrades, even if ME is not enabled. Don't proceed on those
26 if (!\Drupal::moduleHandler()->moduleExists('media_entity')) {
30 $checks = \Drupal::service('media_entity.cli')->validateDbUpdateRequirements();
31 if (empty($checks['errors'])) {
35 foreach ($checks['errors'] as $error_msg) {
36 // We can't use drush_log() inside this hook.
37 drush_print($error_msg);
44 * Implements hook_drush_command().
46 function media_entity_drush_command() {
48 $items['media-entity-check-upgrade'] = [
49 'description' => 'Check upgrade requirements for Media Entity into Media in core.',
50 'aliases' => ['mecu'],
53 "drush mecu" => "Checks upgrade requirements for Media Entity while upgrading to Media in core.",
61 * Callback for drush commmand "media-entity-check-upgrade" (mecu).
63 function drush_media_entity_check_upgrade() {
64 // This command is useless if the DB updates have already been run.
65 if (drupal_get_installed_schema_version('media_entity') >= 8004) {
66 drush_log(dt('Your site has already run the media_entity DB updates. If you believe this is not correct, you should consider rolling back your database to a previous backup and try again.'), LogLevel::WARNING);
70 drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_FULL);
71 $checks = \Drupal::service('media_entity.cli')->validateDbUpdateRequirements();
73 if (empty($checks['errors'])) {
74 drush_log(sprintf("\033[1;32;40m\033[1m%s\033[0m", '✓') . ' ' . dt('SUCCESS: All upgrade requirements are met and you can proceed with the DB updates.'), LogLevel::OK);
77 drush_log(sprintf("\033[31;40m\033[1m%s\033[0m", '✗') . ' ' . dt('ERROR: Your site did not pass all upgrade checks. You can find more information in the error messages below.'), LogLevel::ERROR);
79 foreach ($checks['passes'] as $pass_msg) {
80 drush_log($pass_msg, LogLevel::OK);
82 foreach ($checks['errors'] as $error_msg) {
83 drush_log($error_msg, LogLevel::ERROR);