View | Details | Raw Unified | Return to bug 7793
Collapse All | Expand All

(-)a/Koha/Schema/Result/MessageQueue.pm (-11 / +8 lines)
Lines 1-21 Link Here
1
use utf8;
2
package Koha::Schema::Result::MessageQueue;
1
package Koha::Schema::Result::MessageQueue;
3
2
4
# Created by DBIx::Class::Schema::Loader
3
# Created by DBIx::Class::Schema::Loader
5
# DO NOT MODIFY THE FIRST PART OF THIS FILE
4
# DO NOT MODIFY THE FIRST PART OF THIS FILE
6
5
7
=head1 NAME
8
9
Koha::Schema::Result::MessageQueue
10
11
=cut
12
13
use strict;
6
use strict;
14
use warnings;
7
use warnings;
15
8
16
use base 'DBIx::Class::Core';
9
use base 'DBIx::Class::Core';
17
10
18
=head1 TABLE: C<message_queue>
11
12
=head1 NAME
13
14
Koha::Schema::Result::MessageQueue
19
15
20
=cut
16
=cut
21
17
Lines 130-135 __PACKAGE__->add_columns( Link Here
130
  "content_type",
126
  "content_type",
131
  { data_type => "text", is_nullable => 1 },
127
  { data_type => "text", is_nullable => 1 },
132
);
128
);
129
__PACKAGE__->set_primary_key("message_id");
133
130
134
=head1 RELATIONS
131
=head1 RELATIONS
135
132
Lines 165-176 __PACKAGE__->belongs_to( Link Here
165
  "message_transport_type",
162
  "message_transport_type",
166
  "Koha::Schema::Result::MessageTransportType",
163
  "Koha::Schema::Result::MessageTransportType",
167
  { message_transport_type => "message_transport_type" },
164
  { message_transport_type => "message_transport_type" },
168
  { is_deferrable => 1, on_delete => "RESTRICT", on_update => "CASCADE" },
165
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
169
);
166
);
170
167
171
168
172
# Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-07-11 09:26:55
169
# Created by DBIx::Class::Schema::Loader v0.07010 @ 2015-02-24 18:59:44
173
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:YWxM2O2W/h34qqIOIpzOtw
170
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:v+Th3wvct4LBCEp+fRlMBw
174
171
175
172
176
# You can replace this text with custom content, and it will be preserved on regeneration
173
# You can replace this text with custom content, and it will be preserved on regeneration
(-)a/installer/data/mysql/kohastructure.sql (-1 / +1 lines)
Lines 2567-2573 CREATE TABLE `message_queue` ( Link Here
2567
  `to_address` mediumtext,
2567
  `to_address` mediumtext,
2568
  `from_address` mediumtext,
2568
  `from_address` mediumtext,
2569
  `content_type` text,
2569
  `content_type` text,
2570
  KEY `message_id` (`message_id`),
2570
  PRIMARY KEY `message_id` (`message_id`),
2571
  KEY `borrowernumber` (`borrowernumber`),
2571
  KEY `borrowernumber` (`borrowernumber`),
2572
  KEY `message_transport_type` (`message_transport_type`),
2572
  KEY `message_transport_type` (`message_transport_type`),
2573
  CONSTRAINT `messageq_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
2573
  CONSTRAINT `messageq_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
(-)a/installer/data/mysql/updatedatabase.pl (-1 / +37 lines)
Lines 10462-10467 if ( CheckVersion($DBversion) ) { Link Here
10462
    SetVersion ($DBversion);
10462
    SetVersion ($DBversion);
10463
}
10463
}
10464
10464
10465
$DBversion = "3.21.00.XXX";
10466
if ( CheckVersion($DBversion) ) {
10467
10468
    #First make sure that there are no duplicate message_id's which might be quite impossible thanks to the AUTO_INCREMENT.
10469
    #If duplicates exists, find the next free message_id and UPDATE the duplicate message_queues with the new biggest id's.
10470
    my $sthGetDuplicatePKs = $dbh->prepare("SELECT count(message_id) as duplicate_pks, message_id ".
10471
                            "FROM message_queue GROUP BY message_id HAVING duplicate_pks > 1;");
10472
#USE THIS TO TEST           "FROM message_queue GROUP BY message_id HAVING duplicate_pks > 0;");
10473
    my $sthGetMessage_queues = $dbh->prepare("SELECT * FROM message_queue WHERE message_id = ?");
10474
    my $sthGetBiggestMessageId = $dbh->prepare("SELECT max(message_id) as message_id FROM message_queue;");
10475
    my $sthUpdateMessageQueue = $dbh->prepare("UPDATE message_queue SET message_id = ? WHERE message_id = ? AND borrowernumber = ? AND time_queued = ? AND content = ?;");
10476
10477
    #Get duplicate message_ids
10478
    $sthGetDuplicatePKs->execute;
10479
    my $message_ids = $sthGetDuplicatePKs->fetchall_arrayref({});
10480
    foreach my $mi (@$message_ids) {
10481
        if ($mi->{duplicate_pks} > 1) { #Making sure there are multiple usages of this id
10482
#USE TO TEST  if ($mi->{duplicate_pks} > 0) { #Making sure we trigger this repair routine for messages with no duplicates, just to test.
10483
            $sthGetMessage_queues->execute($mi->{message_id}); #Get all the message_queues sharing the same id.
10484
            my $duplicates = $sthGetMessage_queues->fetchall_arrayref({});
10485
            my $firstRun = 1; #On the first replace run for this set of duplicates, preserve the original id.
10486
            foreach my $duplicate (@$duplicates) {
10487
                next if $firstRun-- == 1; #Preserve the original id for the first conflicting message_queue-row
10488
                $sthGetBiggestMessageId->execute();
10489
                my $max_message_id = $sthGetBiggestMessageId->fetchrow();
10490
                $max_message_id++;
10491
                $sthUpdateMessageQueue->execute( $max_message_id, $duplicate->{message_id}, $duplicate->{borrowernumber}, $duplicate->{time_queued}, $duplicate->{content} );
10492
            }
10493
        }
10494
    }
10495
10496
    #Finally add the PRIMARY KEY
10497
    $dbh->do( "ALTER TABLE message_queue DROP KEY message_id, ADD CONSTRAINT PRIMARY KEY (message_id);" );
10498
    print "Upgrade to $DBversion done (Bug 7793 - redefine the field message_id as PRIMARY KEY of message_queue)\n";
10499
    SetVersion ($DBversion);
10500
}
10501
10465
# DEVELOPER PROCESS, search for anything to execute in the db_update directory
10502
# DEVELOPER PROCESS, search for anything to execute in the db_update directory
10466
# SEE bug 13068
10503
# SEE bug 13068
10467
# if there is anything in the atomicupdate, read and execute it.
10504
# if there is anything in the atomicupdate, read and execute it.
10468
- 

Return to bug 7793