Issue Details (XML | Word | Printable)

Key: MDL-11062
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Yu Zhang
Reporter: Eloy Lafuente (stronk7)
Votes: 2
Watchers: 4
Operations

Add/Edit UI Mockup to this issue
If you were logged in you would be able to see more operations.
Moodle

Groups 1.7 => 1.8 upgrade problems

Created: 30/Aug/07 07:02 AM   Updated: 25/Sep/07 10:16 AM
Component/s: General, Groups, Installation
Affects Version/s: 1.8.2, 1.9
Fix Version/s: 1.8.3

File Attachments: 1. Text File 11062.patch (25 kB)
2. File groups.diff (19 kB)
3. File groups17_groups18.diff (6 kB)
4. Text File groups17_groups18.txt (0.8 kB)
5. File groups17_groups18_upgrade_code.php (10 kB)

Environment: practically any
Issue Links:
Dependency
 
Relates
 

Database: Any
Participants: Eloy Lafuente (stronk7), Martin Dougiamas, Nick Freear and Yu Zhang
Security Level: None
Resolved date: 25/Sep/07
Affected Branches: MOODLE_18_STABLE, MOODLE_19_STABLE
Fixed Branches: MOODLE_18_STABLE


 Description  « Hide
Hi,

I was tracing the upgrade process in order to fix one bug in the 1.7 => 1.8 migration under Oracle (MDL-10575) when I've seen some code in the upgrade of groups that, or I'm completely wrong, or is breaking any non-mysql installation:

1) As part of the process, the "groups" table is renamed to "groups_temp", and then records are copied as "raw" inserts (i.e. including explicitly the id in the insert from one table to other).

2) The upgrade process, instead of declaring all the XMLDB tables/fields/indexes, uses directly the install.xml file to perform the creation of the schema.

With those 2 points I think that all these disasters are true:

D1) Sequences are lost. Absolutely. At least under PostgreSQL and Oracle. When one table is renamed (to temp_groups) its sequence is moved together and when the new table (groups) is created it comes with its own sequence initialy set to 1). So, if any record has been deleted from the 1.7 groups table along its life, then the copy of raw registers won't coincide with the number of the sequence in the new table. Problems for sure!

D2) Some databases (MSSQL AFAIK) doesn't allow insertion in fields that are governed by sequences. Broken for sure.

D3) We must think that each block of code in upgrade.php is one photograph of the DB, while the install.xml file changes along the time. So it's 1000% incorrect to use in the upgrade.php something that potentially, has changed!!! FORBIDDEN or posterior steps in the upgrade process can fail completely.

With all these Disasters... I think that the upgrade is broken in near each Oracle, PostgreSQL and MSSQL intallations (with more posibilities depending of the changes performed to the install.xml file along the life on the 1.8 group/db code).

And should be fixed. Completely.

To fix it I would propose to:

1) Apply all the needed modifications to the schema, from the 1.7 version to the 1.8 one, using the upgrade.php file as usual (creating all the needed XMLDB objects and calling ddllib functions - The editor can help here.

2) Once everything has been created. Populate tables as needed. I don't know too much about this.

This new 1 & 2 code should REPLACE the current one. 100%

I've been talking with Petr and HEAD seems to be free of this problems, because the upgrade code has been completely replaced.

Please, I think it's important. Each day one server becomes upgraded... if they were using groups... problems!



 All   Comments   Change History   Version Control      Sort Order: Ascending order - Click to sort in descending order
Martin Dougiamas added a comment - 30/Aug/07 05:11 PM
It's very odd that this wasn't picked up before ... I'd have thought Nick @ OU would have noticed it for sure on their PG site.

OK, I'm assigning to Yu for tomorrow. If someone else wants to grab it sooner (while we sleep) please do!


Eloy Lafuente (stronk7) added a comment - 31/Aug/07 02:56 AM
I've attached one diff file (groups17_groups18.diff) with all the differences between Moodle 1.7 groups and Moodle 1.8.2+ groups.

Going to summarize all the changes in a text file now...


Eloy Lafuente (stronk7) added a comment - 31/Aug/07 03:37 AM
Attached (groups17_groups18.txt) is a list with all the changes required to transform the Moodle 1.7 groups schema to the 1.8.2+ version.

Note that there is one point (point 11) that is where all the movement of data must happen (before deleting some DB objets later).

I'm going to generate one file with all the XMLDB steps above (skipping the point 11 for now).


Eloy Lafuente (stronk7) added a comment - 31/Aug/07 07:35 AM
Attached one new file (groups17_groups18_upgrade_code.php) that contains all the upgrade code needed to modify the SCHEMA (not the logic!) from any Moodle 1.7 version to the Moodle 1.8.2+ one (it follows the steps detailed in the previous attachment).

IMPORTANT NOTE: Point 11 in the script must be edited to add the the logic in the upgrade (creation of new records in new tables and so on).


Eloy Lafuente (stronk7) added a comment - 31/Aug/07 07:46 AM - edited
So, at this point these tasks must be performed:

1) Edit the point 11 in the upgrade script above to add all the logic of the upgrade.
2) The resulting script should be copied and pasted somewhere in the upgrade function (I'm not really sure of where exactly), but these conditions should be satisfied:

a) Users installing 1.8.2+ from scratch, should use, exclusively, the install.xml file
b) Users coming from 1.7.x to 1.8.2+ must execute our new script exclusively. Nothing else.
c) Users coming from 1.8.x to 1.8.2+ must execute current code exclusively. Skipping our new script. While this is a mistake, I'm not really sure of what intermediate status are in that process so, perhaps it will be risky to modify it.

3) Tomorrow I'll play with sequences, trying to create some ddllib function able to reset sequence numbers to the desired one. We'll add then that step to the upgrade file trying to repair potentially wrong sequences (it's risky to do that directly in 1.8 - new function, but I really cannot imagine a better situation).

4) Confirm that both the 1.7.x => 1.8.2+ and the 1.8.x => 1.8.2+ upgrades are working and that the final schema is the same (the one currently represented in install.xml). This will help to be sure than the upgrade to 1.9 will work perfectly.

Hope this helps. Going to bed now. I leave 2) for you. Ciao


Yu Zhang added a comment - 31/Aug/07 03:01 PM - edited
Groups upgrade code is called at 3 places from admin/index.php install_group_db() x 2, upgrade_group_db() x 1

1) For new installs, this is ok, because groups table will be empty thus there is nothing to move (we can leave this alone, or fix it)
2) For under 1.7, where rolesactive flag is not set. This needs fixing, because usage of groups is expected (call to new function)
3) For 1.7+ -> 1.8. In this case upgrade_group_db() is called, this needs fixing because usage of groups is expected (call to new function)

I propose we fix it like this

Leave 1) alone, except to change the function to remove code handling data migration. It should just read from the xml (group/install.xml) and install the tables. This is for new install only.

Remove call 2) and 3) (not removing this call, but remove the install code within upgrade_group_db()), and replace with the code in core with what Eloy provided, since we know the exact version

if ($result && $oldversion < XXXXXXXXXX) {
}

Note: there is very little code needed to migrate data for point 11. We only need to insert data into course_groups table, before dropping the courseid field.

/// a) get the current groups, foreach one add an entry in groups_courses_groups
if ($oldgroups = get_records('groups')) {
foreach ($oldgroups as $oldgroup) { $rec = new Object(); $rec->courseid = $oldgroup->courseid; $rec->groupid = $oldgroup->id; $rec->timeadded = $oldgroup->timemodified; // I think this is not needed since the field is gone? insert_record('groups_courses_groups', $rec); }
}

That was all I found out.


Yu Zhang added a comment - 31/Aug/07 03:11 PM - edited
I also noticed that some of the things Eloy has done is already in xmldb_group_upgrade()

Though I do not really understand when the following code gets called...

if ($result && $oldversion < 2007012400) {
if (table_exists(new XMLDBTable('groups_temp')) && file_exists($CFG->dirroot.'/group/db/install.xml')) {

The temporary table never gets dropped from 1.7->1.8, so the above code is probably run once, which means groups_transfer_db() is probably called twice during installation. I think the second call doesn't do anything since table would have been populated, but this is probably wrong.


Yu Zhang added a comment - 03/Sep/07 11:55 AM
This is a bit worse than I imagined. Since lib/upgrade.php gets run before groups code, I had to move the groups and groups_members table out from group directory and put them into main. Groups now also goes through 2 update (1st one to install the tables, 2nd one to upgrade db).

I have tested 1.7 > 1.8 (should be same for 1.6>1.8), and new install. These seem to work. However, I suspect some versions of 1.8->1.8 might break on the rename of password->enrolmentkey. So I think we can check for field existence first.

Also, I am setting groups version to 1 after the groups table installation. I think we can potentially test some groups table existence as well, instead of using this hack.

cvs diff is attached. Please review.

Cheers,

Yu


Yu Zhang added a comment - 03/Sep/07 11:59 AM
Sorry just realized I need to move all the other groups tables out as well, to main, and drop customized groups completely to rely on the upgrade code.

Yu Zhang added a comment - 03/Sep/07 01:20 PM
I think I will put the upgrade code in install function instead. Will make a new diff tomorrow.

Yu Zhang added a comment - 04/Sep/07 01:42 PM
Here is the revised patch.

1) New installations. I have copied install.xml from group/db/ into lib/db/ so new install should get all the new tables. In admin/index.php I also set the groups version manually to the latest 2007012401. This would prevent any groups upgrade code from running later on.

2) Upgrade from installtions prior to 1.8. upgrade_group_db() will be called which in turn calls install_groups_db() because there is no groups version . I have changed install_groups_db() to use xmldb to do proper upgrade and migrate. Upon completion groups version is set to the latest 2007012401.

3) Existing 1.8->1.8. upgrade_groups_db() will be called, and should use existing code xmldb_group_upgrade(). I do not have a copy I can test, but the code is unmodified for this bit. The only question is that I have taken out install.xml to main, and bits of the xmldb_group_upgrade() code looks for install.xml in group/db. Do I need to keep a copy of install.xml there as well, or can I just cvs remove install.xml? I do not understand how this code can be run, as 2007012400 is a dev version? I can keep the existing group/db/install.xml if it is needed.

if ($result && $oldversion < 2007012400) {
if (table_exists(new XMLDBTable('groups_temp')) && file_exists($CFG->dirroot.'/group/db/install.xml')) { /// Need to drop foreign keys/indexes added in last upgrade, drop 'new' tables, then start again!! $result = $result && groups_drop_keys_indexes_db(); $result = $result && groups_revert_db($renametemp=false); $result = $result && install_from_xmldb_file($CFG->dirroot.'/group/db/install.xml'); $result = $result && groups_transfer_db(); }
}

Please review/test.

Cheers,

Yu


Eloy Lafuente (stronk7) added a comment - 05/Sep/07 07:50 AM
Hi Yu,

I've tested both:

  • 1.7 => 1.8.2+ with your patch
  • 1.8.2 => 1.9beta

with one site, having one course with 2 groups having 2 users each.

and everything is working fine under MySQL, PostgreSQL and MSSQL now (I had to modify slightly the 1.8 => 1.9 but that's another story).

Under Oracle I'm getting one error I've localized (I think). I'll fix it tomorrow.

So only this needs to be tested:

  • 1.8.0 => 1.8.2+ with your patch
  • 1.8.1 => 1.8.2+ with your patch

If you can do it (I think that if it works with MySQL and PostgeSQL, it will be safe). It requires to install the initial version (from download.moodle.org, perhaps?) and then, perform the upgrade to 1.8.2+ with your patch.

Later, I'll get this again to:

1) Fix the Oracle issue.
2) Try the sequences thing
3) Perform the pending tests (those that you haven't tested tomorrow).

all this to issue the final +1 ASAP. It's really is looking promising. Cool!

Ciao zzzZZZzzz


Yu Zhang added a comment - 05/Sep/07 11:00 AM
Hi Eloy,

Tested

  • 1.8.0 => 1.8.2+ with my patch (with groupings)
  • 1.8.1 => 1.8.2+ with my patch (no more groupings in this version)

both with new installations. Seems to work. However I didn't have postgres and I tested on MSSQL instead. (One calendar SQL bug under MSSQL, same as the one under HEAD, will file separate bug)

Waiting for your test result and signal to commit =-)

Cheers,

Yu


Nick Freear added a comment - 05/Sep/07 11:46 PM
Hi guys,
Sorry I haven't commented sooner! As far as I know, we didn't see this on our PostgreSQL installation(s).
I appreciate the problem, now! But it would be helpful to document some of these points regarding upgrade versus install - they weren't obvious to me (unless they're already documented & I've missed them).

I'll follow progress with interest. Many thanks
Nick


Eloy Lafuente (stronk7) added a comment - 06/Sep/07 03:45 AM
Now working under Oracle too, both 1.7 => 1.8 and 1.8 => 1.9

I'm going to test 1.8 => 1.8.2 too under PG and Oracle, but I guess they'll work.

Also I've the readjust_sequence subtask pending, I hope I'll be able to implement that (although I think is shouldn't stop this to be sent to CVS asap).

Ciao


Yu Zhang added a comment - 07/Sep/07 09:49 AM
Please let me know when you are done the postgres tests so I can commit the patch in.

Cheers,

Yu


Eloy Lafuente (stronk7) added a comment - 08/Sep/07 05:01 PM
Confirmed working under PostgreSQL:

1.8.0 ==> 1.8.2 (deleted the groupings)
1.8.1 ==> 1.8.2 (did practically nothing)

Oracle coming soon (although, due to the nature of the actions performed in that two upgrades, I guess it'll work).


Eloy Lafuente (stronk7) added a comment - 08/Sep/07 05:48 PM
Confirmed both under Oracle too.

so I cannot imagine anything against to vote +1 for this to go to 18_STABLE.

And, perhaps, it would be a good idea to release 1.8.3, now that we have tested all this upgrade paths.

Ciao


Martin Dougiamas added a comment - 10/Sep/07 09:50 AM
+1 from me! Nice job!

Yu Zhang added a comment - 10/Sep/07 09:56 AM
Code in 1.8...

Yu Zhang added a comment - 17/Sep/07 01:48 PM
No longer a blocker I think

Eloy Lafuente (stronk7) added a comment - 17/Sep/07 03:04 PM
+1 to say "Bye, bye" to this.

Eloy Lafuente (stronk7) added a comment - 22/Sep/07 01:48 AM
Just to annotate that, before clossing this, there are two MOODLE_18_MERGED tags pending to be moved (not merged!) on:

admin/index.php
lib/db/install.xml

http://docs.moodle.org/en/Development:Unmerged_files

Ciao


Yu Zhang added a comment - 25/Sep/07 10:16 AM
Tagged and closing...