Vagari
Vagari

Reputation: 458

How to pass a database prefix to DDEV?

One of the DDEV sites I manage uses a database that includes a prefix. The default behavior for DDEV is to recreate the settings.ddev.php on every start. But that obviously overwrites anything added, purging any manual addition of the prefix.

Is the assumed solution to stop DDEV from overwriting the file? Or to create another settings file (like settings.local.php) to override what's been overridden? Or am I missing something?

This just seems like something that would exist as a simple variable in the config to generate a more accurate settings.ddev.php file. Thanks!

Upvotes: 1

Views: 1033

Answers (3)

Raj
Raj

Reputation: 591

When it comes to installing a new Drupal site from scratch with DDEV, I'd leave config.yaml alone unless you want to set your upload_dirs there (but that's a different topic). I've found that changing the type to php when using Drupal causes some havoc as the installer won't appreciate having files in your project directory already. In any case, I would suggest leaving the type as drupal. If you're using WordPress, that's a different story.

Assuming you're using DDEV with Composer and Drush, once you've run:

ddev composer require --dev drush/drush

Set the db prefix (and the Private File Path if needed) in web/sites/default/settings.php, found around line 93 for Drupal 10:

$databases = [];

Replace that line with:

$databases['default']['default'] = [
  'prefix' => 'myprefix_',
];
// Set the private file path 
$settings['file_private_path'] = '../private';

Then finish the installation with

ddev drush site:install --account-name=myaccount --account-pass=mypass -y

For complete instructions, see this article by PureWebMedia.biz

Upvotes: 0

benjifisher
benjifisher

Reputation: 5112

I decided to use a version of the second suggestion:

// Automatically generated include for settings managed by ddev.
$ddev_settings = dirname(__FILE__) . '/settings.ddev.php';
if (getenv('IS_DDEV_PROJECT') == 'true' && is_readable($ddev_settings)) {
  require $ddev_settings;
  $databases['default']['default']['prefix'] = "drupal_";
}

I just added the $databases line. The rest was already there.

Upvotes: 0

rfay
rfay

Reputation: 12815

There are a few straightforward answers:

  • Don't let ddev fiddle with settings at all. Change the project type to 'php' and ddev won't mess with it.
  • Make the changes you want to db settings in settings.php after the inclusion of settings.ddev.php. That should work no matter what. And it should work on your prod site as well.
  • Do the work in settings.local.php, but include it after settings.ddev.php in your settings.php file
  • Take over settings.ddev.php and do whatever you want with it. This just means deleting the line that contains #ddev-generated in settings.ddev.php. After that, ddev won't muck with it at all.

Upvotes: 1

Related Questions