Bug 30449: (QA follow-up) Report back on problematic situations
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 25 Apr 2022 13:09:41 +0000 (10:09 -0300)
committerFridolin Somers <fridolin.somers@biblibre.com>
Mon, 2 May 2022 21:22:57 +0000 (11:22 -1000)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
installer/data/mysql/atomicupdate/bug_30449.pl

index 316f401..b9c2f73 100755 (executable)
@@ -13,11 +13,45 @@ return {
                 $dbh->do( q|ALTER TABLE borrower_attribute_types DROP INDEX category_code_fk| );
             }
         }
-        if( !foreign_key_exists('borrower_attribute_types', 'borrower_attribute_types_ibfk_1') ) {
-            if( !index_exists('borrower_attribute_types', 'category_code') ) {
-                $dbh->do( q|ALTER TABLE borrower_attribute_types ADD INDEX category_code (category_code)| );
+
+        if (
+            !foreign_key_exists(
+                'borrower_attribute_types', 'borrower_attribute_types_ibfk_1'
+            )
+          )
+        {
+
+            my $sth = $dbh->prepare(
+                q{
+                SELECT category_code
+                FROM borrower_attribute_types
+                WHERE category_code NOT IN (SELECT categorycode FROM categories);
+            }
+            );
+
+            $sth->execute;
+
+            my @invalid_categories;
+            while ( my $row = $sth->fetchrow_arrayref() ) {
+                push( @invalid_categories, $row->[0] );
+            }
+
+            if (@invalid_categories) {
+                die "The 'borrower_attribute_types' table contains "
+                  . "references to invalid category codes: "
+                  . join( ', ', @invalid_categories );
+            }
+
+            if ( !index_exists( 'borrower_attribute_types', 'category_code' ) )
+            {
+                $dbh->do(q|
+                    ALTER TABLE borrower_attribute_types ADD INDEX category_code (category_code)
+                |);
             }
-            $dbh->do( q|ALTER TABLE borrower_attribute_types ADD CONSTRAINT borrower_attribute_types_ibfk_1 FOREIGN KEY (`category_code`) REFERENCES `categories` (`categorycode`)| );
+            $dbh->do(q|
+                ALTER TABLE borrower_attribute_types
+                    ADD CONSTRAINT borrower_attribute_types_ibfk_1 FOREIGN KEY (`category_code`) REFERENCES `categories` (`categorycode`)
+            |);
         }
     },
 };