fix for #791 & refactor and fix bugs in dbschemasetup #930
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request refactors
dbschemasetup.phpto remove code duplication and logic inconsistencies, fixes several bugs, and adds documentation. I started doing this when trying to set up emoncms master, I bumped into the issue with the user table'stagscolumn being set to NOT NULL, and I refactored to try and work out what was going on. This is the result!There are three cases when this file creates or updates a column in the database:
All these cases currently use different logic, resulting in inconsistencies. For a column that has 'null'=false, in cases 1 and 3 it is not given a NOT NULL constraint, but in case 2 it is. If 'null'=true, however, in cases 1 and 2 it doesn't get a NOT NULL constraint, but in case 3 it is. I've refactored so that all three cases use the same code to decide what a column should look like in the database.
There was also a bug in case 3, where only the changes to the column specification would be written on update. So if you had a column, say
mothers_surnameand it had a default value, if you then later updated it to be NOT NULL without respecifying the default value, the default would be lost, like so:This patch avoid this by always respecifying the full datatype when modifying a column.
Columns that had an explicit default of 0 or '' weren't getting this set in the database in case 1 or case 3. PHP's loose equality operator has probably covered this up until now.
NOT NULL wasn't being set on most columns that requested it in case 1 or case 3.
The new index creation code didn't trigger in case 2.
And finally, only case 1 reported errors. All cases now report errors.
Future suggestions
db_schema_setup()into two: maybedb_schema_get_statementsanddb_schema_apply_statementsinstead of having an $apply argument. This way, errors can be returned from the latter without mixing them up with the return of SQL statements.I'm happy to do all the above, but didn't because I presume there are compatibility issues with changing schema definitions if people have custom modules, and I didn't want to touch code outside of this file if I could help it.
I wrote some test cases while working on the code. I didn't see any unit test or integration test infrastructure but I thought I'd include them anyway in case this is something the project adopts in the future.
Any feedback welcome, I realise I'm a new contributor here :)