4

I am trying to write a Drush script locally to speed up some repetitive tasks. I am running in to issues importing a database through the script. I have tried using drush_invoke and drush_invoke_process to run the equivalent of drush sql-cli < C:/path/to/mysql.sql.

I have tried several different variations with the $commandline_args and $commandline_arguments with no luck and Google has not been helping me much.

Does anyone have the correct syntax for this? I would also be fine with another method that works. I would prefer one that allows for a user input path to the SQL file but hard coded would work for now.

EDIT: I am using drush scr to run the script. Just in case it makes a difference.

EDIT 2: The accepted answer from greg_1_anderson works great. To allow for using the path to the file as an argument I found that drupal_get_option works. Just add the argument --arg_name=arg_value like drush scr script.php --file=path/to/file. Then call the arg in the script file.

Full solution:

$file = drush_get_option('file');
$options = array(
  'file' => $file,
  'file-delete' => FALSE,
  'strict' => FALSE,
);
drush_invoke_process($self, 'sql-query', array(), $options);
2
  • So you're looking to execute a PHP script after executing a few SQL queries in Drush?
    – Shawn Conn
    Commented Nov 12, 2015 at 17:12
  • I am looking to run some Drush commands and make a db import (and updates) in a PHP script. I would prefer using Drush commands but I could be dissuaded. I was hoping that the sql-cli command would transfer over to the drush_invoke_process function.
    – sareed
    Commented Nov 12, 2015 at 17:19

1 Answer 1

5

Basically, you want to run some php code to import an SQL database dump from a file. There is code that does this in the implementation of drush sql-sync. It looks like this:

  $options = $global_options + $target_options + array(
    'file' => $destination_dump_path,
    'file-delete' => TRUE,
  );
  $return = drush_invoke_process($destination, 'sql-query', array(), $options, $backend_options);
  if ($return['error_status']) {
    // An error was already logged.
    return FALSE;
  }

You could also call drush sql-sync with appropriate options for a more full-featured solution (e.g. if you want to import a local database to a remote site). For an import to a local site, though, it is more straightforward to simply use the snippet above, which essentially runs drush sql-query --file=/path/to/dump.sql.

n.b. You may simply remove (or make empty) $global_options, $target_options and $backend_options. You should also take of the file-delete flag, unless you really would like Drush to delete your sql dump for you after it has imported it (e.g. for temporary files).

1
  • Awesome. Worked perfectly for me. Note that because of my version I also had to add 'strict' => FALSE to the options (although I could have removed the 'file-delete' as well).
    – sareed
    Commented Nov 12, 2015 at 19:40

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.