Bug 17292 - Use of DBIx in updatedatabase.pl broke upgrade
authorKyle M Hall <kyle@bywatersolutions.com>
Mon, 12 Sep 2016 14:20:19 +0000 (14:20 +0000)
committerBrendan Gallagher <brendan@bywatersolutions.com>
Mon, 10 Oct 2016 10:20:56 +0000 (10:20 +0000)
A recent change in the 'subscription' table structure highlighted a
problem in a DBRev upgrade (3.23.00.006). As it adds a new column, when
upgrading from (say) 3.20.00.000 the code/schema (correctly) expects the
subscription.itemtype column to exist. But it is not created until DBRev
16.06.00.025.

To reproduce:
- Have a clean 3.20.00 DB loaded into kohadevbox
- Checkout current master
- Run:
  $ perl installer/data/mysql/updatedatabase.pl
  => FAIL: The upgrade procedure fails due to missing colum.

  The solution: rewrite the updatedatabase.pl entry using plain DBI

Signed-off-by: Mark Tompsett <mtompset@hotmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Brendan Gallagher <brendan@bywatersolutions.com>
installer/data/mysql/updatedatabase.pl

index 0062bf5..f638fa7 100755 (executable)
@@ -11469,13 +11469,19 @@ if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
         ADD serialseq_z VARCHAR( 100 ) NULL DEFAULT NULL AFTER serialseq_y
    ");
 
-    my $schema        = Koha::Database->new()->schema();
-    my @subscriptions = $schema->resultset('Subscription')->all();
+    my $sth = $dbh->prepare("SELECT * FROM subscription");
+    $sth->execute();
+
+    my $sth2 = $dbh->prepare("SELECT * FROM subscription_numberpatterns WHERE id = ?");
+
+    my $sth3 = $dbh->prepare("UPDATE serials SET serialseq_x = ?, serialseq_y = ?, serialseq_z = ? WHERE serialid = ?");
 
-    foreach my $subscription (@subscriptions) {
+    foreach my $subscription ( $sth->fetchrow_hashref() ) {
         my $number_pattern = $subscription->numberpattern();
+        $sth2->execute( $subscription->{numberpattern} );
+        my $number_pattern = $sth2->fetchrow_hashref();
 
-        my $numbering_method = $number_pattern->numberingmethod();
+        my $numbering_method = $number_pattern->{numberingmethod};
         # Get all the data between the enumeration values, we need
         # to split each enumeration string based on these values.
         my @splits = split( /\{[XYZ]\}/, $numbering_method );
@@ -11487,12 +11493,15 @@ if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
         }
         my @indexes = sort { $indexes{$a} <=> $indexes{$b} } keys(%indexes);
 
-        my @serials =
-          $schema->resultset('Serial')
-          ->search( { subscriptionid => $subscription->subscriptionid() } );
+        my @serials = @{
+            $dbh->selectall_arrayref(
+                "SELECT * FROM serial WHERE subscriptionid = $subscription->{subscriptionid}",
+                { Slice => {} }
+            )
+        };
 
         foreach my $serial (@serials) {
-            my $serialseq = $serial->serialseq();
+            my $serialseq = $serial->{serialseq};
             my %enumeration_data;
 
             ## We cannot split on multiple values at once,
@@ -11514,12 +11523,11 @@ if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) {
                 $enumeration_data{ $indexes[0] } = $serialseq;
             }
 
-            $serial->update(
-                {
-                    serialseq_x => $enumeration_data{'X'},
-                    serialseq_y => $enumeration_data{'Y'},
-                    serialseq_z => $enumeration_data{'Z'},
-                }
+            $sth3->execute(
+                    $enumeration_data{'X'},
+                    $enumeration_data{'Y'},
+                    $enumeration_data{'Z'},
+                    $serial->{serialid},
             );
         }
     }