Lines 7-18
return {
Link Here
|
7 |
my ($args) = @_; |
7 |
my ($args) = @_; |
8 |
my ($dbh, $out) = @$args{qw(dbh out)}; |
8 |
my ($dbh, $out) = @$args{qw(dbh out)}; |
9 |
|
9 |
|
10 |
# Delete 'empty' guarantors. No longer possible to add them via interface. Have no use. |
10 |
# Remove constraint at start |
11 |
$dbh->do(q{ |
11 |
if ( foreign_key_exists('borrower_relationships', 'r_guarantor') ) { |
12 |
DELETE FROM borrower_relationships WHERE guarantor_id IS NULL |
12 |
$dbh->do(q{ |
|
|
13 |
ALTER TABLE borrower_relationships DROP FOREIGN KEY `r_guarantor` |
14 |
}); |
15 |
say $out "Temporarily removing FK constraint on guarantor_id"; |
16 |
} |
17 |
|
18 |
# Delete bad/empty guarantors. No longer possible to add them via interface. Have no use. |
19 |
my $cnt = $dbh->do(q{ |
20 |
DELETE borrower_relationships FROM borrower_relationships LEFT JOIN borrowers bo ON bo.borrowernumber=guarantor_id WHERE guarantor_id IS NULL OR bo.borrowernumber IS NULL; |
13 |
}); |
21 |
}); |
|
|
22 |
say $out "Removed $cnt bad guarantor relationship records" if $cnt && $cnt =~ /^\d+$/; |
23 |
|
24 |
# Make column NOT NULL and add constraint back |
14 |
$dbh->do(q{ |
25 |
$dbh->do(q{ |
15 |
ALTER TABLE borrower_relationships CHANGE COLUMN guarantor_id guarantor_id int(11) NOT NULL |
26 |
ALTER TABLE borrower_relationships CHANGE COLUMN guarantor_id guarantor_id int(11) NOT NULL |
16 |
}); |
27 |
}); |
|
|
28 |
$dbh->do(q{ |
29 |
ALTER TABLE borrower_relationships ADD FOREIGN KEY `r_guarantor` (guarantor_id) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE |
30 |
}); |
31 |
say $out "Changed column guarantor_id and added FK constraint on guarantor_id back"; |
17 |
}, |
32 |
}, |
18 |
}; |
33 |
}; |
19 |
- |
|
|