3

I am attempting to sanitize my database as described in this Acquia guide.

For my site, I need to sanitize all the usernames as well; I would like to sanitize the usernames exactly the same as the e-mail addresses.

The guide suggests using a hook such as this:

<?php
/**
* Implementation of hook_drush_sql_sync_sanitize().
*/
function modulename_drush_sql_sync_sanitize($source) {
    $query = "UPDATE content_type_profile SET field_primary_email_email = CONCAT(SUBSTRING_INDEX(field_primary_email_email,'@', 1), '@example.com');";

    $query .= "UPDATE content_type_profile SET field_work_phone_value = SUBSTRING(FLOOR(RAND() * 1000000000000) WHERE uid NOT IN  SELECT uid FROM users_roles WHERE rid=3) AND uid > 0 FROM 1 FOR 12)  WHERE field_work_phone_value IS NOT NULL;";

   drush_sql_register_post_sync_op('my-sanitize-id', dt('Sanitize email addresses and phone numbers'), $query);
}
?>

But since this is for Drush, I don't know where to put the hook. I tried modifying the code and adding it to an enabled module on my site, but when I ran sql-sanitize, it didn't pick up the hook.

This is what I did. In MYMODULE/MYMODULE.module, I added the following code:

/**
* Implementation of hook_drush_sql_sync_sanitize().
*/
function MYMODULE_drush_sql_sync_sanitize($source) {
    $query = "UPDATE user SET name = CONCAT(SUBSTRING_INDEX(name,'@', 1), '@example.com');";


   drush_sql_register_post_sync_op('my-sanitize-id', dt('Sanitize user names'), $query);
}
?>
1
  • 1
    Is your module enabled? If it is, try adding it in a MYMODULE.drush.inc file, that's where custom commands usually go but I'm not sure if the same applies to hooks
    – Clive
    Commented Jul 2, 2014 at 13:59

2 Answers 2

4

Looking at the drush code this is the function that looks for the "hooks"

drush_command_invoke_all('drush_sql_sync_sanitize', 'default');

following the path at the end is calling to:

drush_commandfile_list()

which is going to look for hooks implementations in the next folders:

  • The "/path/to/drush/commands" folder.
  • Folders listed in the 'include' option (see example.drushrc.php).
  • The system-wide drush commands folder, e.g. /usr/share/drush/commands
  • The ".drush" folder in the user's HOME folder.
  • /drush and sites/all/drush in current Drupal site.
  • Folders belonging to enabled modules in the current Drupal site.

So as @Clive suggested probably MYMODULE.drush.inc and MYMODULE_drush_sql_sync_sanitize should work.

1

You can run drush -d status (or any other command, -d for Debug) to see where drush actually looking for the files and what files it's trying to load, e.g.:

$ drush -d status
Using the Drush script found at /usr/local/share/drush/drush.launcher using pcntl_exec
Drush preflight prepare loaded autoloader at                         [preflight]
/usr/local/share/drush/vendor/autoload.php [0.01 sec, 2.99 MB]
Starting Drush preflight. [0.01 sec, 2.99 MB]                                                                                                                                              [preflight]
Cache HIT cid: 8.1-dev-commandfiles-0-80c764c89cea212b4c95e9b4349fa821 [0.02 sec, 3.06 MB]                                                                                                     [debug]
Scanning into /etc/drush for /.*aliases\.drush(8|)rc\.php$/ [0.06 sec, 5.67 MB]                                                                                                                [debug]
Scanning into /etc/drush for /self\.alias\.drush(8|)rc\.php$/ [0.06 sec, 5.67 MB]                                                                                                              [debug]
Scanning into /usr/local/share/drush/includes/.. for /.*aliases\.drush(8|)rc\.php$/ [0.06 sec, 5.67 MB]                                                                                        [debug]
Scanning into /usr/local/share/drush/includes/.. for /self\.alias\.drush(8|)rc\.php$/ [0.1 sec, 5.68 MB]                                                                                       [debug]
Scanning into /home/vagrant/.drush for /.*aliases\.drush(8|)rc\.php$/ [0.14 sec, 5.68 MB]                                                                                                      [debug]
Scanning into /home/vagrant/.drush for /self\.alias\.drush(8|)rc\.php$/ [0.14 sec, 5.68 MB]                                                                                                    [debug]
Scanning into /var/www/../drush for /.*aliases\.drush(8|)rc\.php$/ [0.14 sec, 5.68 MB]                                                                                           [debug]
Scanning into /var/www/../drush for /self\.alias\.drush(8|)rc\.php$/ [0.14 sec, 5.68 MB]                                                                                         [debug]
Scanning into /var/www/drush for /.*aliases\.drush(8|)rc\.php$/ [0.14 sec, 5.68 MB]                                                                                              [debug]
Scanning into /var/www/drush for /self\.alias\.drush(8|)rc\.php$/ [0.14 sec, 5.68 MB]                                                                                            [debug]
Scanning into /var/www/sites/all/drush for /.*aliases\.drush(8|)rc\.php$/ [0.14 sec, 5.68 MB]                                                                                    [debug]
Scanning into /var/www/sites/all/drush for /self\.alias\.drush(8|)rc\.php$/ [0.15 sec, 5.68 MB]                                                                                  [debug]
Scanning into /var/www/sites/default for /.*aliases\.drush(8|)rc\.php$/ [0.15 sec, 5.69 MB]                                                                                      [debug]
Scanning into /var/www/sites/default for /self\.alias\.drush(8|)rc\.php$/ [0.15 sec, 5.69 MB]                                                                                    [debug]
Bootstrap to phase 0. [0.61 sec, 7.9 MB]

Basically you can copy drush files (such as drushrc.php, *.drushrc.php, *.drush.inc) into any of the following locations:

  1. Drupal's drush, ../drush or sites/all/drush folder relative to your document root (however sql-sync validation could not work here).
  2. A .drush folder in your HOME folder.
  3. Anywhere in a folder tree below an active module on your site.
  4. /usr/share/drush/commands (this is configurable).
  5. In an arbitrary folder specified with the --include option.

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.