1

Currently on drush v12.5.2.0 and I am using drush sql:sync to sync my local with the remote db. I want to ensure I do not enter @prod for the target.

I realized I could use drush policy to validate [https://www.drush.org/12.x/examples/PolicyCommands.php/][1]

I have the below file in my project/drush/Commands

<?php

namespace Drush\Commands;

use Consolidation\AnnotatedCommand\CommandData;
use Drush\Commands\DrushCommands;


class PolicyCommands extends DrushCommands {
  /**
   * Prevent catastrophic braino.
   *
   * @throws \Exception
   */
  #[CLI\Hook(type: HookManager::ARGUMENT_VALIDATOR, target: SqlSyncCommands::SYNC)]
  public function sqlSyncValidate(CommandData $commandData) {
    if ($commandData->input()->getArgument('target') == '@prod') {
      throw new \Exception(dt('Per !file, you may never overwrite the production database.', ['!file' => __FILE__]));
    }
  }

}

I cleared the drush cache using drush cc drush as well.

#Load this commandfile using the --include option - e.g. `drush --include=/path/to/drush/examples .

I ran drush --include=~/project/drush/Commands but still no luck.

Kind of a newbie so am I missing something? If I still give drush sql:sync @self @prod it does not throw any Exception..

5
  • 1
    Is there a reason you went with the Drush\Commands\PolicyCommands namespace rather than Drush\Commands? I don't know if that's the only problem here, but it looks wrong. In addition have you execute Drush in debug/verbose mode for more information?
    – mona lisa
    Commented Jun 21 at 19:36
  • Another sample to compare with: github.com/drupal-composer/drupal-project/blob/10.x/drush/…
    – leymannx
    Commented Jun 21 at 22:17
  • I don't know if they've changed it, but in the past I was not able to use sql:sync to remote aliases.
    – Jaypan
    Commented Jun 21 at 23:55
  • @monalisa thank you for pointing that out. I actually tried with Drush\Commands at first but still couldn't make it work. I have edited the question's code. Commented Jun 22 at 4:28
  • @monalisa I checked verbose output it seems like the function is not getting executed. How do I add this file to the drush configuration? I have added this path to drush.yml under drush:paths:include Commented Jun 22 at 4:36

1 Answer 1

1

This works just fine. drush sql:sync @self @prod will first ask a question if we want to destroy the live database and replace it with our local and our validation function is executed only if we enter YES. I was under the impression that it runs before the interactive question. Just learned this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.