Databases: I think I have broken my database!!!

Databases: I think I have broken my database!!!

Lindsay Magnus-mit -
Antal besvarelser: 1
Hi,

In an attempt to try and create a new field in the user profile I though a short cut would be just to rename an existing one .... I did this using the database admin module now I get what I think are errors when I browse that table i.e. mdl_user

Are these messages critical and how can I remove them?

Thanx
Lindsay
Bilag moodle_error.jpg
Gennemsnitsbedømmelse: -
I svar til Lindsay Magnus

Re: Databases: I think I have broken my database!!!

A K-mit -

Whoops!

Table definitions, including fields names, are /not/ to be played with lightly in a functioning relational database system. Within any non-trivial system there will be many very carefully designed 'relations' (hence the name: relational database) between tables and the slightest change to any of these, which certainly includes changing a field name, is very likely to affect the functioning of the whole system, usually breaking it. Such changes should only be done with an in-depth knowledge of the database design and substantial understanding of how relational databases work and of programming (in this case in PHP).

A database administrator tool (such as the module you mention) is a very powerful tool for a database designer /developer / programmer and I'd go as far as to suggest that you should /never/ use it to change data unless you know /exactly/ what the consequences will be, probably after having studied and understood substantial parts of the PHP code that runs Moodle.

The best you can do now is probably to rename the field back straight away and keep your fingers crossed that no widespread damage has been done meanwhile by other PHP code responding to the change you made. If you're very lucky, everything will work again. This will be very much influenced by whether anyone (including yourself) has tried to update any data while the field was renamed. 

I would then have a good look around the system, especially parts connected with what you were trying to change (user information) to look for signs of whether anything appears not to be working as expected. This will be a bit hit-or-miss (unless you are going to invest a /lot/ of time in understanding the relational model used in Moodle) but will hopefully identify any major problems.

In general, it is relatively 'safe' to add a new field to a table (since that will not break any existing relations / structure) but highly dangerous to change an existing field definition in any way without a lot of meticulous checking of the underlying relations and code. The smallest change to the structure of a relational database is a highly technical process in which it is easy to do great damage unless you have considerable experience and have done a lot of preparatory work, including thorough familiarisation with all the ways any changed fields are used throughout the system. In fact adding a new field is probably pointless unless you are prepared to get your hands dirty with a /lot/ of checking and cross-checking of the developer documentation and are going to be writing significant PHP code.


Sorry if some of this is familiar ground or sounds a bit heavy, but it's hard to over-emphasise the damage that can be done to a database by even the tiniest of 'inappropriate' changes - it's very different from something like Word or Excel in this respect... you can't count on just experimenting with a little change and keeping your fingers crossed in the knowledge that you can undo it... by the time you try to undo the change its effects could have propogated widely within the database if there has been any other access to it by any process meanwhile.

If after reverting the change of field name things don't appear to be working properly your only option will probably be to restore the database itself. In fact, from a technical point of view, unless you can guarantee that no other data change might have taken place, restoring the database is the only really safe way to get back to a working system. 

So, try to change it back and good luck. If you really want to make such a change spend a /lot/ of time studying the developer documentation and existing PHP code first.

Andrew.