Moodle

During upgrade group IDs aren't maintained - breaks activities/modules

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Blocker Blocker
  • Resolution: Fixed
  • Affects Version/s: 1.8
  • Fix Version/s: 1.8
  • Component/s: Groups
  • Labels:
    None
  • Database:
    PostgreSQL
  • Affected Branches:
    MOODLE_18_STABLE
  • Fixed Branches:
    MOODLE_18_STABLE

Description

During upgrade groups and members are transferred from old tables 'mdl_groups_temp', 'mdl_groups_members_temp'. In the former case the records aren't sorted on ID and the group ID may be lost in the function 'groups_create_group' - the ID is auto-incremented, so a new one is used.

Note, may be database dependent.

Activity

Hide
Nick Freear added a comment -

Help! The patch 'upgrade_2' does NOT work completely, but its as far as I've got. The only way I can see to maintain group IDs during incremental upgrade of 1.8 dev is to 'get rid' of the new 1.8 groups tables, recreate them, then re-transfer the data from the 'groups_temp', 'groups_members_temp' tables, using a raw INSERT to add the records with existing IDs.

So this patch fixes the upgrade from 1.6/1.7, and the incremental upgrade of 1.8 dev by,
1. drops the foreign keys/indexes created by the previous 2007012200 upgrade (sorry Eloy!),
2. renames all the new 1.8 tables to 'groups_temp_18', etc.
3. recreates them using XMLDB.
4. transfers data, calling 'groups_db_upgrade_group'.

(3) goes wrong as the rename does not rename the primary keys - it complains that they already exist. I could drop the tables in (2), but admins won't like that!

Tested on PostgreSQL.

Show
Nick Freear added a comment - Help! The patch 'upgrade_2' does NOT work completely, but its as far as I've got. The only way I can see to maintain group IDs during incremental upgrade of 1.8 dev is to 'get rid' of the new 1.8 groups tables, recreate them, then re-transfer the data from the 'groups_temp', 'groups_members_temp' tables, using a raw INSERT to add the records with existing IDs. So this patch fixes the upgrade from 1.6/1.7, and the incremental upgrade of 1.8 dev by, 1. drops the foreign keys/indexes created by the previous 2007012200 upgrade (sorry Eloy!), 2. renames all the new 1.8 tables to 'groups_temp_18', etc. 3. recreates them using XMLDB. 4. transfers data, calling 'groups_db_upgrade_group'. (3) goes wrong as the rename does not rename the primary keys - it complains that they already exist. I could drop the tables in (2), but admins won't like that! Tested on PostgreSQL.
Hide
Nick Freear added a comment -

To clarify, the upgrade from 1.6/1.7 etc. works. The problem is the upgrade from 1.8 dev!

Show
Nick Freear added a comment - To clarify, the upgrade from 1.6/1.7 etc. works. The problem is the upgrade from 1.8 dev!
Hide
Nick Freear added a comment -

I still need help!!
I am now trying to drop the primary key before renaming each table (is this possible?), with code like:

"" $tables = array('', '_members', '_groupings', '_courses_groups', '_courses_groupings', '_groupings_groups');
$suffix = '_temp_18';

foreach ($tables as $t_name) {
$table = new XMLDBTable('groups'.$t_name);
$key = new XMLDBKey('groups'.$t_name);
$key->setAttributes(XMLDB_KEY_PRIMARY, array('id'));
$status = $status && drop_key($table, $key);
$status = $status && rename_table($table, 'groups'.$t_name.$suffix);
...""

But this doesn't seem to work - I currently get this output (Adodb warning at the end):

"" Sorry, groups tables created in Moodle 1.8 dev are being archived and renamed to 'groups_*_temp_18'!

(postgres7): SELECT co.conname AS name, co.consrc AS description FROM pg_constraint co, pg_class cl WHERE co.conrelid = cl.oid AND co.contype = 'c' AND cl.relname = 'headmdl_groups'
(postgres7): ALTER TABLE headmdl_groups RENAME TO headmdl_groups_temp_18
Success

(postgres7): ALTER TABLE headmdl_groups_id_seq RENAME TO headmdl_groups_temp_18_id_seq
Success

...
(postgres7): CREATE TABLE headmdl_groups ( id BIGSERIAL, name VARCHAR(254) NOT NULL DEFAULT '', description TEXT, enrolmentkey VARCHAR(50) NOT NULL DEFAULT '', lang VARCHAR(30) NOT NULL DEFAULT 'en', theme VARCHAR(50) NOT NULL DEFAULT '', picture BIGINT NOT NULL DEFAULT 0, hidepicture SMALLINT NOT NULL DEFAULT 0, timecreated BIGINT NOT NULL DEFAULT 0, timemodified BIGINT NOT NULL DEFAULT 0, CONSTRAINT headmdl_grou_id_pk PRIMARY KEY (id) )

Warning: pg_query() [function.pg-query]: Query failed: ERROR: relation "headmdl_grou_id_pk" already exists in /var/www/html/ndf42/head/lib/adodb/drivers/adodb-postgres7.inc.php on line 115
-1: ERROR: relation "headmdl_grou_id_pk" already exists
ADOConnection._Execute(CREATE TABLE headmdl_groups (
... ""

Show
Nick Freear added a comment - I still need help!! I am now trying to drop the primary key before renaming each table (is this possible?), with code like: "" $tables = array('', '_members', '_groupings', '_courses_groups', '_courses_groupings', '_groupings_groups'); $suffix = '_temp_18'; foreach ($tables as $t_name) { $table = new XMLDBTable('groups'.$t_name); $key = new XMLDBKey('groups'.$t_name); $key->setAttributes(XMLDB_KEY_PRIMARY, array('id')); $status = $status && drop_key($table, $key); $status = $status && rename_table($table, 'groups'.$t_name.$suffix); ..."" But this doesn't seem to work - I currently get this output (Adodb warning at the end): "" Sorry, groups tables created in Moodle 1.8 dev are being archived and renamed to 'groups_*_temp_18'! (postgres7): SELECT co.conname AS name, co.consrc AS description FROM pg_constraint co, pg_class cl WHERE co.conrelid = cl.oid AND co.contype = 'c' AND cl.relname = 'headmdl_groups' (postgres7): ALTER TABLE headmdl_groups RENAME TO headmdl_groups_temp_18 Success (postgres7): ALTER TABLE headmdl_groups_id_seq RENAME TO headmdl_groups_temp_18_id_seq Success ... (postgres7): CREATE TABLE headmdl_groups ( id BIGSERIAL, name VARCHAR(254) NOT NULL DEFAULT '', description TEXT, enrolmentkey VARCHAR(50) NOT NULL DEFAULT '', lang VARCHAR(30) NOT NULL DEFAULT 'en', theme VARCHAR(50) NOT NULL DEFAULT '', picture BIGINT NOT NULL DEFAULT 0, hidepicture SMALLINT NOT NULL DEFAULT 0, timecreated BIGINT NOT NULL DEFAULT 0, timemodified BIGINT NOT NULL DEFAULT 0, CONSTRAINT headmdl_grou_id_pk PRIMARY KEY (id) ) Warning: pg_query() [function.pg-query]: Query failed: ERROR: relation "headmdl_grou_id_pk" already exists in /var/www/html/ndf42/head/lib/adodb/drivers/adodb-postgres7.inc.php on line 115 -1: ERROR: relation "headmdl_grou_id_pk" already exists ADOConnection._Execute(CREATE TABLE headmdl_groups ( ... ""
Hide
Nick Freear added a comment -

Fixed using version 4 of the patch.

This uses a new approach - the admin is given a 'Do you want to continue?' message, with yes/ no options - if they choose yes groups tables are dropped and recreated. (This gets round the issue that primary keys can't be dropped separately, and in Postgres, MySQL, etc. keys can't be renamed.)

Show
Nick Freear added a comment - Fixed using version 4 of the patch. This uses a new approach - the admin is given a 'Do you want to continue?' message, with yes/ no options - if they choose yes groups tables are dropped and recreated. (This gets round the issue that primary keys can't be dropped separately, and in Postgres, MySQL, etc. keys can't be renamed.)
Hide
Nick Freear added a comment -

Fixed, committed. Tested on PostgreSQL - uses XMLDB so hopefully the same on other DBs.

Show
Nick Freear added a comment - Fixed, committed. Tested on PostgreSQL - uses XMLDB so hopefully the same on other DBs.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: