It would be nice to use the aliases.drushrc.php file alias definition to work for sql-dump, not just sql-sync.

Instead of having to add a drush @myalias sql-dump --results-file=somename.sql --gzip

We might have

'%dump-dir' => 'sqldumps',
'%dump' => 'somename.sql',

work for the sql-dump command too, so

drush @myalias sql-dump would read the aliases file and create the dump named 'somename.sql' in the "root" 'sqldumps' directory.

This could be overridden by actually using --results-file= instead of requiring its use.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

Category: feature » support
Status: Active » Fixed

You can add command specific options to an alias. Search for command-specific' at http://api.drush.org/api/drush/examples%21example.aliases.drushrc.php/5.x (click View source)

JSCSJSCS’s picture

I was not able to find anything with the search for 'command-specific' at the link you provided, but I did find some info in the examples.alias.drushrc.php file that got me headed in the right direction. I added the follwing to my aliases.drushrc.php file:

  'command-specific' => array (
    'sql-dump' => array (
    'result-file' => 'sqldumps\test.sql',
    'gzip' => TRUE,
    ),
  ),

(Keep in mind I am using Drush 5.1 but have edited my sql.drush.inc file to change the ; to && for Windows to use multi-commands correctly.

But I could use just a bit more help. How to automatically answer "y" when writing over the test.sql.gz file on subsequent executions?

$ drush @mdjdev sql-dump
Database dump saved to sqldumps\test.sql.gz [success]

$ drush @mdjdev sql-dump
gzip: sqldumps\test.sql.gz already exists; do you wish to overwrite (y or n)? y (****need this to auto answer y)
Database dump saved to sqldumps\test.sql.gz [success]

greg.1.anderson’s picture

Add 'yes' => TRUE to your command-specific entry for sql-dump above.

JSCSJSCS’s picture

Thanks for the quick respones!

I changed the alias and reloaded GIT Bash

  'command-specific' => array (
    'sql-dump' => array (
    'result-file' => 'sqldumps\nfsgunexsqldumps.sql',
    'gzip' => TRUE,
    'yes' => TRUE,
    ),
  ),

But not getting any love (Still asking for confirmation):

$ drush @nfsdev sql-dump -v
Load alias @nfsdev [notice]

Initialized Drupal 7.12 root directory at c:\wamp\www\local.example.com [notice]

Initialized Drupal site local.example.com at sites/default [notice]

Calling system(mysqldump --result-file sqldumps\example.sql --no-autocommit --single-transaction --opt -Q example --host=localhost --user=root --password= && gzip sqldumps\example.sql);
gzip: sqldumps\example.sql.gz already exists; do you wish to overwrite (y or n)?

Interesting that "drush sa does NOT list the command-specific options, even with --full --with-optional

$ drush sa @nfsdev --full --with-optional
$aliases['nfsdev'] = array (
'uri' => 'local.example.com',
'root' => 'c:\\wamp\\www\\local.example.com',
'db_url' => 'mysql://root@localhost/example',
'path-aliases' =>
array (
'%dump-dir' => 'sqldumps',
'%dump' => 'example.sql',
'%files' => 'sites/default/files',
'%drush' => 'c:/ProgramData/Propeople/Drush',
'%site' => 'sites/local.example.com/',
),
'#file' => 'c:/Users/USERNAME/.drush/aliases.drushrc.php',
);

It makes since that it is not working though, unless the code accounts for the fact that the -y is not --y as it does got --gzip and others.

greg.1.anderson’s picture

Whoops, didn't notice that it is gzip, not Drush, that is asking for the confirmation here. We could add code to Drush to remove the destination file (with or without confirmation); that would best be handled as a separate feature request ("patches welcome").

As a workaround, you could manually remove the destination file before calling Drush. I presume you need the autoconfirm because you want to call this function from a script; if that's the case, then it should be easy enough to preflight with a file delete.

JSCSJSCS’s picture

Forgive me my PHP, for I am only 5 weeks into a 6 week "Beginning PHP" college extension course.

I found that Gzip has a -f flag to halt confirmation. This patch seems to work if 'yes' => TRUE. 'yes' => FALSE or not set at all. I did not do any other testing.

This is probably the second time I have ever made a patch file, so no guarantees!

greg.1.anderson’s picture

Title: Let drush sql-dump make use of alias path-alias key=>value pairs » Pass --yes through to gzip in sql-dump
Category: support » feature
Status: Fixed » Needs work

That looks pretty good. The first thing I see is pre-existing, but should be fixed while we're here. $file should be wrapped in drush_shell_escape_arg when passed to gzip: $exec .= "&& gzip " . drush_escape_shell_arg($file);

Also, drush_get_option('yes') should be drush_get_context('DRUSH_AFFIRMATIVE').

Beware of typos in my off-the-cuff comments. ;) Thanks for working on this.

JSCSJSCS’s picture

I don't have a clue what the suggested changes do, but I entered them and tested for 'yes' => TRUE. 'yes' => FALSE or not set at all and everything still worked!

And as a bonus, I am honing my Git Diff patch skills.

JSCSJSCS’s picture

Oops hold everythign. I made a rookie mistake and was testing on my original drush not the git clone. Need to test cuz am getting errors when using new changes.

greg.1.anderson’s picture

#8 is missing a closing quote prior to drush_escape_shell_arg in both occurances.

JSCSJSCS’s picture

I found that, but actually, even though you warned me about typos, your "drush_escape_shell_arg($file);" should have been "drush_escapeshellarg($file);"

Once I found that, Gzip did not like it.

$ drush @nfsdev sql-dump
gzip: illegal option -- s
usage: gzip [-acdfhlLnNrtvV19] [-S suffix] [file ...]
Database dump failed [error]

So I created a variable to hold the results of the function and that seemed to work

$escfile = drush_escapeshellarg($file);

$exec .= " && gzip -f $escfile";

Again I did all the same tests as before, and this time on the right file!

greg.1.anderson’s picture

Version: 7.x-5.1 »
Assigned: Unassigned » greg.1.anderson
Status: Needs work » Reviewed & tested by the community

You misread #7; drush_escapeshellarg($file) is not quoted. The way you did it is fine, though, and all tests pass. If there are no objections, I will commit this shortly.

JSCSJSCS’s picture

It wasn't quotes that got me, it was your extra Underscores
Thanks again!

greg.1.anderson’s picture

Status: Reviewed & tested by the community » Fixed

Committed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.