Bug 26596

Summary: updatedatabase.pl uses too much CPU when it has 0 updates to run
Product: Koha Reporter: David Cook <dcook>
Component: Architecture, internals, and plumbingAssignee: Bugs List <koha-bugs>
Status: RESOLVED DUPLICATE QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low    
Version: Main   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:

Description David Cook 2020-10-02 01:04:49 UTC
At the moment, koha-upgrade-schema indiscriminately runs updatedatabase.pl, regardless of whether or not the Koha instance actually requires a database upgrade.

I've noted that running koha-upgrade-schema against a large number of instances (e.g. 50 instances) takes a couple of minutes. Jonathan has noted doing an upgrade will take 1.5 seconds, so 1.5 x 50 = would be 1.25 minutes. 

Jonathan and I agreed via email that koha-upgrade-schema should do a check similar to C4::Auth::_version_check but without using the ORM via C4::Context->preference() to avoid the overhead of loading the DBIC schema. 

In theory, koha-upgrade-schema should be able to spin through 50 instances in a fraction of a second, if none of them need database upgrades.
Comment 1 David Cook 2020-10-02 01:05:23 UTC
This isn't an urgent matter for me, so I'm not going to mark myself as the Assignee, but I may get to this at some point.
Comment 2 David Cook 2021-03-25 01:34:03 UTC
I'm noticing that updatedatabase.pl uses 100% CPU and runs for a while, so this is just annoying me too much now.

Rather than make the change to koha-upgrade-schema though, I'm going to change updatedatabase.pl.
Comment 3 David Cook 2021-03-25 01:37:31 UTC
All we need to do is the following at the top of updatedatabase.pl:

my $original_version = C4::Context->preference("Version");
my $code_version = $Koha::VERSION;

if ( $original_version >= TransformToNum($code_version){
    exit 0;
}

--

If the code version is the same or higher than the current database version, then there cannot possibly be any database updates to run, and we should exit immediately, rather than consuming 100% CPU for way too long by doing 23,000 lines of updates.

This is a short-term solution as overhauling the database update system is the long-term solution.
Comment 4 David Cook 2023-09-12 23:37:29 UTC

*** This bug has been marked as a duplicate of bug 34088 ***