After trying unsuccessfully to install Drupal 6 with a custom database prefix...
drush site-install --db-url='mysqli://username:password@domain/db' --db-prefix='drupal_' --account-name='username' --account-pass='password' --account-mail='username@domain.com' --site-name='generic' --site-mail='username@domain.com'
...Drupal 6 installed and runs, but without the prefix. Turns out that while drush_core_site_install_db_spec() parses $db_spec['db_prefix'] correctly, drush_core_pre_site_install() doesn't actually use it for Drupal 6 configurations; it just appends the $db_url to the end of the file instead of parsing / finding and replacing.
// On D6, we have to write $db_url ourselves. On D7+, the installer does it.
file_put_contents($settingsfile, "\n" . '$db_url = \'' . drush_get_option('db-url') . "';\n", FILE_APPEND);
I understand that's a design decision and easier to maintain, but the end result is that db-prefix doesn't get written.
I've attached a patch, which includes a more verbose comment about how the configuration is being written and why.
Comments
Comment #1
fluxsauce commentedComment #2
moshe weitzman commentedSeems reasonable. However, db_prefix can be an array. We should var_export it, no?
Comment #3
fluxsauce commentedI've been thinking about this over the holiday weekend...
var_export would be fine in this instance.
The question, which makes this a little trickier: how to get the pairs?
runserver in Drush 5 doesn't parse arrays from the command line, but I think that in this instance we can get away with it due to the simplicity of database table naming conventions. (Ref: http://dev.mysql.com/doc/refman/5.5/en/identifiers.html and function db_prefix_tables - http://api.drupal.org/api/drupal/includes--database.inc/function/db_pref... )
Here's a $db_prefix definition for a multi-site configuration:
This can be serialized and be included on the command line, but it's a little messy and needs to be generated.
Human readable proposal - If $db_spec['db_prefix'] contains comma (an illegal character for a table name), explode on comma, explode each on colon and build the $db_prefix that way.
A different separator, like | could be used instead of a comma.
Syntax: No spaces, comma separated TABLE:PREFIX
Code that parses human readable pairs:
Thoughts? Should it include error checking? If it sounds good, I'll write the patch.
Comment #4
moshe weitzman commentedI don't mind the human readable option that much, but IMO we should do what runserver does and ask users to use drushrc.php is they need to pass an array.
Comment #5
fluxsauce commentedMakes sense, thanks for your feedback. I've attached the patch for the 7.x-4.x branch, includes documentation and example.
Comment #6
moshe weitzman commentedWhy is D6 special here? Is D7 different with respect to prefixes?
must use two lines here per coding standards
Lets do this as $command_specific for site-install. sql-sync does not support db_prefix.
Comment #7
moshe weitzman commenteddoes not apply cleanly against master branch.
Comment #8
greg.1.anderson commentedD7 puts the database prefix into the $databases array, so this patch only works with D6.
Since drush itself does not support database prefixes, this feature is slightly dubious. First step on the road, I guess, but it should support D7 as well.
Comment #9
fluxsauce commentedSorry about the confusion regarding branches; this patch is against the master. I adjusted the patch to use
$command_specific, fixed the coding standard issue, and adjusted the help to reflect that it works for both Drupal 6 and 7 (tested, verified in both configuration and in database).No modifications were necessary for it to work in Drupal 7, but there is a minor warning from drupal_strlen from the form validation.
Drupal 7 result:
Comment #10
moshe weitzman commentedCommitted. Thanks.
I decided not to clutter the example.drushrc file with this niche example. The command docs describe what to do.
Comment #11
msonnabaum commentedBackported to 4.x