Problem/Motivation
The documentation in default.settings.php for Drupal 8 shows the following example for a database configuration:
$databases['default']['default'] = array(
'driver' => 'mysql',
'database' => 'databasename',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'prefix' => 'main_',
'collation' => 'utf8_general_ci',
);
But the actual configuration is created as:
$databases['default']['default'] = array (
'database' => 'databasename',
'username' => 'username',
'password' => 'password',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
The current issue is that the order of the options doesn't match the one in the documentation.
Beta phase evaluation
| Issue category | Task since it's not really a bug./td> |
|---|---|
| Issue priority | Minor since it is not really cosmetic, but supposed to reduce confusion. |
| Unfrozen changes | Unfrozen since only changes comments. |
| Prioritized changes | The main goal of this issue is usability of the settings.php file |
| Disruption | NOT Disruptive for core/contributed and custom modules/themes because it will not require a BC break/deprecation/internal refactoring/widespread changes... |
Allowed in the beta since it is restricted to just documenation change to make it easier to use the setting.php file.
Proposed resolution
Update the documentation in default.settings.php to match the order of the options.
$databases['default']['default'] = array (
'database' => 'databasename',
'username' => 'username',
'password' => 'password',
'prefix' => '',
'host' => 'localhost',
'port' => '3306',
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
);
Remaining tasks
| Task | Novice task? | Contributor instructions | Complete? |
|---|---|---|---|
| Double-check that the format was fixed in Drupal 8 | Yes | This was mentioned in #698236-53: Settings.php $databases example improvement and #698236-54: Settings.php $databases example improvement | Yes |
| Update the documentation to match the order. | Yes | The order is: database, username, password, prefix, host, port, namespace and driver | Yes |
| Replace var_export as suggested in #698236-36: Settings.php $databases example improvement | #698236-48: Settings.php $databases example improvement asserts that drupal_var_export in not available at install time and according to #698236-71: Settings.php $databases example improvement the suggested function is now Variable::export(). | ||
| Complete the rest of #698236-64: Settings.php $databases example improvement part 3 |
User interface changes
API changes
Original report by @iantresman
Fortunately, Drupal 7 set-up my settings settings.php file correctly. But I would not have been able to work it out from the examples given, which is more complicated than D6. The example is given as:
* Database configuration format:
* $databases['default']['default'] = array(
* 'driver' => 'mysql',
* 'database' => 'databasename',
* 'username' => 'username',
* 'password' => 'password',
* 'host' => 'localhost',
* );But the actual configuration shows as follows, which I think is sufficiently different for the non-programmer (ie, shows "array" three times!):
$databases = array (
'default' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'mydatabase',
'username' => 'myname',
'password' => 'mypassword',
'host' => 'msql.mydomain.com',
'port' => '',
),
),
);I think the example should be very close to the follow, with an explanation of the arrays for programmers:
$databases = array ('default' => array ('default' => array (
'driver' => 'mysql',
'database' => 'mydatabase',
'username' => 'myname',
'password' => 'mypassword',
'host' => 'msql.mydomain.com',
'port' => '',
),
),
);| Comment | File | Size | Author |
|---|---|---|---|
| #95 | 698236-nr-bot.txt | 192 bytes | needs-review-queue-bot |
| #85 | 698236-85.drupal.Settingsphp-databases-example-improvement.patch | 572 bytes | joachim |
| #74 | 698236-database-74.patch | 1.59 KB | mistermarco |
| #57 | 698236-57.diff | 1.48 KB | steeloctopus |
| #53 | withoutapplyanypatch.png | 137.08 KB | brahmjeet789 |
Comments
Comment #1
Anonymous (not verified) commentedI agree. Since most people (even non-developers) will have to muddle about in a settings.php file at one time or another, and will most likely have to do it when they are new to Drupal, we should make it as easy as possible to figure out. Specifying the nested arrays in different ways between the comment and the actual implementation is unnecessarily confusing. We should make it consistent.
Comment #2
stixes commentedI think the generated code overcomplicates the issue, since all the parenthesis and added syntax reduces readability.
It would make better sense to let the implementation follow the example.
Comment #3
derjochenmeyer commentedI aggree with #2 but the output is generated in includes/install.inc:584
drupal_rewrite_settings()using the PHP functionvar_export(). The output of this function can't be formated differently.The Drupal function
drupal_var_export()returns similar output.Without writing a custom var_export function we can only rewrite the documentation according to the output of
var_export()to make it consistent. But that would dramatically reduce the readability of the documentation.Comment #4
derjochenmeyer commentedAttached patch rewrites output for
$databasessetting in includes/install.inc:584drupal_rewrite_settings().Comment #5
derjochenmeyer commentedAdded a comment.
Comment #6
derjochenmeyer commentedAnd removed a trailing space.
Comment #7
stixes commentedValid patch that fixes the issue.
Comment #8
xjmThanks for your work on this patch. Couple questions/remarks:
These are sort of hard to read... I'd suggest using a full if/else rather than the ternary, since the lines are long.
Also, is it possible to add an automated test for this behavior? Not sure if we have test coverage for the creation of settings.php or not.
Finally, note that the Drupal 8.x patch will need to be rerolled, because the core directory structure for Drupal 8 has now changed. (For more information, see #22336: Move all core Drupal files under a /core folder to improve usability and upgrades).
It would also help to have somewhat more detailed reviews (rather than simply "fixes the issue."). E.g., some before/after, what you did to test it, etc.
Comment #9
derjochenmeyer commentedI agree, but basically the second one is just re-adding the existing code, removed before:
Maybe we don't even need the comments following the
$databasesarray?Creating tests for this is over my head, I guess. I'd need some help with that.
I'll be happy to reroll the patch, but setting this to "needs review" to get some opinions first.
Comment #10
hosef commentedI updated the patch so that it will apply against the current state of Drupal.
While I was doing that I also decided to brake out the two big long lines into full if/else statements. After doing that I realized that I had never seen the output from $setting['comment'], so I did a quick grep to find where else it is used and found that $setting['comment'] is not used in core and it has no purpose, hence I completely removed the part where it adds $setting['comment'] to $buffer.
I would also have written a test to test against this functionality, however I can't figure out how one would actually run the test if this code didn't work in the first place.
Comment #11
hosef commentedComment #12
hosef commentedI just want to see how the automated test site handles this. Don't use this on a real install.
Comment #14
hosef commentedOk, it appears that qa.drupal.org already tests to make sure that Drupal can install. Therefore, writing a test against this will duplicate existing functionality(I think?).
Comment #15
xjmThanks @hosef (and thanks also for fixing that ternary). The patch looks clean to me codewise.
@hosef and I discussed this in IRC (regarding what the bot actually tests, etc.) Since we can't test the installer at present, let's do some manual testing for this patch (maybe with each database type?). Include some before/after code snippets of the settings.php from your testing in your review.
Leaving this tagged as "novice" for that task.
Comment #16
hosef commentedIn the course of trying to figure out what i was supposed to assert in a test, I installed D8 ~30 times, so I already have the code snippets. But, I still need someone else to test it before it should be marked RTBC.
Here is the $databases variable before applying the patch on each of my VM's and after applying the patch on each of my VM's.
MySQL before patch:
MySQL after patch:
SQLite before patch:
SQLite after patch:
PostgreSQL before patch:
PostgreSQL after patch:
Comment #17
xjmExcellent, thanks @hosef! That will make it much easier for reviewers to know what to check for.
Comment #18
Zgear commentedI'll test this in a bit
Comment #19
Zgear commentedConfirmed, the patch does as hosef said. I installed a fresh version of D8 with the patch in, and the settings.php file did include all of the changes.
Comment #20
hosef commentedThanks for the positive review Zgear. There are a few things I would like to point out though.
First, it appears from your comment that you only tested my patch on one of the three supported databases. When there is a patch that could potentially modify how database stuff works, one is supposed to test it on at least MySQL, PostgreSQL, and SQLite. If you have access to another database such as Microsoft SQL server or Oracle server, then it would also be nice for the community to know how well something works with them and where we could make improvements.
Second, the 'Assigned:' drop down box is supposed to be the person who is fixing the problem not the person reviewing the fix. So, in this case even though you were helping by reviewing the patch it was still assigned to me. Assigning an issue to yourself is kind of like saying 'I have an interest in the issue and want to write a patch that fixes it, and/or re-roll existing patches until the patch is committed to core or I un-assign it from myself.'
Comment #21
aimeilian commentedAssigning to self for manual review
Comment #22
aimeilian commentedIf I'm reading the comment thread correctly, this issue requires before/after patch testing against the three supported database schemata. I had taken on this task thinking it was simply a documentation review; regrettably I only have MySQL available right now, so I can't actually test the patch in the remaining databases.
That said, being new to Drupal but familiar with PHP, great job on getting documentation and implementation to match.
Comment #23
aimeilian commentedDe-assigning; kindly forgive if this comment is superfluous!
Comment #24
disasm commentedreroll
Comment #25
disasm commentedMySQL
SQLite
Note on SQLite, the settings.php file was written correctly, however; I did run into some weird errors about watchdog table not existing. I think this is unrelated though, because it gave the same errors when I tried installing pre-patch.
And finally postgresql:
Comment #26
hosef commentedAlright, I just tested the rerolled patch, and everything worked. Marking RTBC. I can backport this if we decide that would be good idea.
Comment #27
catchCan we find better variable names than $db_first_key and $db_second_key? There's also code style issues here i.e. missing a space after the foreach. The array formatting does look friendlier though.
Comment #28
disasm commentedreroll and rename db_first_key to db_name and db_second_key to db_role.
Comment #29
derjochenmeyer commentedFixing foreach code style issues.
Comment #30
cferthorneyPatch changed file permissions from 644 to 755. Patch rerolled and resubmitted in following commit.
Comment #31
cferthorneyAdjust patch for correct file permissions
Comment #32
star-szr#31: drupal-698236-improve_database_settings_documentation-31.patch queued for re-testing.
Comment #33
star-szrThis has always confused me, happy to find this issue!
This inline comment should wrap at 80 characters per http://drupal.org/node/1354#drupal.
Comment #34
hazem.tamimi commented#31: drupal-698236-improve_database_settings_documentation-31.patch queued for re-testing.
Comment #35
hazem.tamimi commented#12: improve-databases-settings-documentation-698236-5.patch queued for re-testing.
Comment #36
damien tournoud commentedThis is a cute patch :)
We also need to kill about 90% of the unnecessary text of
default.settings.php, but that's another issue.There are a couple of issues in there:
drupal_var_export()instead ofvar_export()here (and in the other branch below) to match our coding standards (especially the spacing afterarray);$databases = array();), before outputting the sub-keys;drupal_var_export()too, as in:Comment #37
petercook commentedI applied patch at #31 and ran install first with mysql. The relevant part of settings.php is as follows:
Also ran the install with sqlite which gave the following:
It seems we now have a value 'namespace' showing up in the settings that isn't in the example.
Comment #38
petercook commentedAdded code to patch #31 so that the 'namespace' values exist in the example code as they do in the settings. Works for mysql, sqlite and postgres. Postgres settings output below:
Comment #39
yesct commented#38: improve_database_settings_documentation-698236-38.patch queued for re-testing.
Comment #41
sanguis commentedrerolled patch from #38 against 711dd53
Comment #43
star-szr#41: improve_database_settings_documentation-698236-41.patch queued for re-testing.
Comment #45
star-szrThanks @sanguis! I diffed the two patches, I don't think the reroll was quite right. Were there many conflicts while rebasing?
Comment #46
sanguis commentedthere was one but I thought I sorted it. will retry
Comment #47
derjochenmeyer commenteddrupal_rewrite_settings() has been completely rewritten.
In the corresponding issue #1921818: Modify drupal_rewrite_settings() to allow writing $settings values heyrocker also suggests rewriting the dumps of whole arrays in favor of a simplified output as proposed here.
Here's an updated patch.
Comment #48
derjochenmeyer commenteddrupal_var_export() as suggested in #37 is not available during install.
Comment #49
derjochenmeyer commentedremove whitespace.
Comment #50
star-szr#49: 698236-49.patch queued for re-testing.
Comment #51
flefle commented#49: 698236-49.patch queued for re-testing.
Comment #52
lucastockmann commentedApplied patch #49 and tested with MySQL. It worked.
Comment #53
brahmjeet789 commentedwithout apply any patch the file all the changes are reflected in setting.php file in drupal-8.x
Comment #54
steeloctopus commentedI tested this issue on both drupal-8.x and drupal-7.x. Here are my findings:
drupal-7.x
drupal-8.x
drupal-8x branch is working as expected but this will need to be resolved on the drupal-7x branch
Comment #55
steeloctopus commentedComment #56
steeloctopus commentedI'm currently working on a back port
Comment #57
steeloctopus commentedThe patch has been added to make the database string in an easily read format.
I have tested this locally and it works as expected.
Comment #59
steeloctopus commented57: 698236-57.diff queued for re-testing.
Comment #60
lucastockmann commentedUnassigning..
Comment #61
gaetanpru commentedHello,
I'm working with a drupal site on mysql database but i would like to use my other database oracle to display data on my site. But when i configure a new database in settings.php, i have an error : "PDOException: could not find driver in eval() (line 4 of E:\drupal\modules\php\php.module(80) : eval()'d code)."
I have to declare my driver in "Database configuration format" of settings.php too or juste add a new database?
If you have some example how to use a second database (oracle) could you say me?
Thanks
Comment #62
yesct commentedThere seem to be a few things going on at once here.
Fixes need to be done in 8.x first and then backported to 7.x.
This issue was moved to 8.x in comment #1,
and then moved back to 7.x in #55....
BUT it was never committed to 8.x.
Let's get this ready for 8.x and *then* do the backport.
----
Note on #57, the patch there is not following the coding standards.
https://drupal.org/coding-standards
For example,
The { should not be on it's own line.
@steeloctopus what editor are you using? there maybe be a drupal coding standards file you can import, or you might have to configure your editor settings manually.
-----
Now, back to 8.x.
Comment #63
iantresman commentedI still don't think we should be using a programming construct to define the database settings, which forces a novice to content with the correct placement of quotes, assignments, and commas etc. Looking at the option, YAML looks clear, unambiguous, well defined, and easy to use:
This is horrendous:
YAML is clear:
Comment #64
yesct commented#54 was a manual test of the last 8.x patch from #49.
A review of #49:
1.
there are several reorderings in arrays in this patch, why?
Is this because when the settings are saved, that is the place the driver is? And so this is making the order in the comments match what might be saved?
--
2.
@catch's concern in #27 was addressed. good.
--
3.
in #36, @Damien Tournoud had concerns:
I think this is still needs work to address that.
Comment #65
yesct commented#63 is a good point. but I think that would be a separate issue. I searched and didn't find an existing issue to use yml for settings. So someone could create that issue and link it back to here.
Comment #66
iantresman commented@#65 YesCT
Done.
Comment #67
yesct commentedadding #2232301: D8: settings.php to settings.yml as related.
Comment #68
hosef commented@#64
I rearranged the database settings example to match what Drupal actually output, because they were quite different from each other. I thought that would make it less confusing for a novice coder who needed to change the settings manually.
Comment #69
yesct commentedComment #70
mistermarco commentedComment #71
dandaman commentedI spent a while looking at this and I'm not sure what need to be changed anymore.
The majority of the code in #49 is not actually run at this time because the
$prefixvalue when_drupal_rewrite_settings_dump_one()is run is$databases, not$databases =. Also, even if I change it to$databases, that code is not run because_drupal_rewrite_settings_dump_one()is only run on objects, I believe, and that is an array.Without any patches, though, the database structure in 8.x core is what the documentation shows:
I'm not sure what the issue is, except for maybe a separate issue that this is written at the bottom of the
settings.phpfile and not replacing$databases = array();towards the top of the file. Also, I think the point of settings$databases = array();right above this is a good idea if it is at the bottom because then you don't have things less clearly overwritten.And finally, the API Docs for
drupal_var_export()says that this function is deprecated and should be changed toVariable::export(). I think that means if we do use that that the statementuse Drupal\Component\Utility\Variable;should be added to the top of the file to make sure the dependency of the Variable class is loaded.Comment #72
mistermarco commentedComment #73
mistermarco commentedComment #74
mistermarco commentedComment #75
mistermarco commentedComment #76
iantresman commentedI would still much prefer settings.php changed to settings.yml as yaml is much easier to read.
Comment #77
sunComment #78
yesct commenteddoing https://www.drupal.org/contributor-tasks/update-allowed-beta
to evaluate https://www.drupal.org/contributor-tasks/update-allowed-beta
Comment #79
yesct commentedI'm not sure that @sun's feedback is actionable here.
This is just about making the comment match what the setting get's saved as.
If the suggestion is to change the order of the saved things, I think that is a different issue.
--
but does need work to reroll/remake to apply to head since it does not apply anymore.
Comment #85
joachim commentedThis has changed quite a bit since the earlier patches.
Here's an update based on the difference between default.settings.php and what's been written in my settings.php by the installer.
Comment #95
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.