Lines 8570-8575
if ( CheckVersion($DBversion) ) {
Link Here
|
8570 |
SetVersion($DBversion); |
8570 |
SetVersion($DBversion); |
8571 |
} |
8571 |
} |
8572 |
|
8572 |
|
|
|
8573 |
$DBversion = "3.17.00.XXX"; |
8574 |
if ( CheckVersion($DBversion) ) { |
8575 |
my $number_of_orders_not_linked = $dbh->selectcol_arrayref(q| |
8576 |
SELECT COUNT(*) |
8577 |
FROM aqorders o |
8578 |
WHERE NOT EXISTS ( |
8579 |
SELECT NULL |
8580 |
FROM aqbudgets b |
8581 |
WHERE b.budget_id = o.budget_id |
8582 |
); |
8583 |
|); |
8584 |
|
8585 |
if ( $number_of_orders_not_linked->[0] > 0 ) { |
8586 |
$dbh->do(q| |
8587 |
INSERT INTO aqbudgetperiods(budget_period_startdate, budget_period_enddate, budget_period_active, budget_period_description, budget_period_total) VALUES ( CAST(NOW() AS date), CAST(NOW() AS date), 0, "WARNING: This budget has been automatically created by the updatedatabase script, please see bug 12601 for more information", 0) |
8588 |
|); |
8589 |
my $budget_period_id = $dbh->last_insert_id( undef, undef, 'aqbudgetperiods', undef ); |
8590 |
$dbh->do(qq| |
8591 |
INSERT INTO aqbudgets(budget_code, budget_name, budget_amount, budget_period_id) VALUES ( "BACKUP_TMP", "WARNING: fund created by the updatedatabase script, please see bug 12601", 0, $budget_period_id ); |
8592 |
|); |
8593 |
my $budget_id = $dbh->last_insert_id( undef, undef, 'aqbudgets', undef ); |
8594 |
$dbh->do(qq| |
8595 |
UPDATE aqorders o |
8596 |
SET budget_id = $budget_id |
8597 |
WHERE NOT EXISTS ( |
8598 |
SELECT NULL |
8599 |
FROM aqbudgets b |
8600 |
WHERE b.budget_id = o.budget_id |
8601 |
) |
8602 |
|); |
8603 |
} |
8604 |
|
8605 |
$dbh->do(q| |
8606 |
ALTER TABLE aqorders |
8607 |
ADD CONSTRAINT aqorders_budget_id_fk FOREIGN KEY (budget_id) REFERENCES aqbudgets(budget_id) ON DELETE CASCADE ON UPDATE CASCADE |
8608 |
|); |
8609 |
|
8610 |
print "Upgrade to $DBversion done (Bug 12601 - Add new foreign key aqorders.budget_id" . ( ( $number_of_orders_not_linked->[0] > 0 ) ? ' WARNING: temporary budget and fund have been created (search for "BACKUP_TMP"). At least one of your order was not linked to a budget' : '' ) . ")\n"; |
8611 |
SetVersion($DBversion); |
8612 |
} |
8613 |
|
8573 |
=head1 FUNCTIONS |
8614 |
=head1 FUNCTIONS |
8574 |
|
8615 |
|
8575 |
=head2 TableExists($table) |
8616 |
=head2 TableExists($table) |