Bug 36424

Summary: Database update 23.06.00.061 breaks due to syntax error
Product: Koha Reporter: Pablo AB <pablo.bianchi>
Component: Installation and upgrade (command-line installer)Assignee: Bugs List <koha-bugs>
Status: Pushed to oldstable --- QA Contact: Testopia <testopia>
Severity: critical    
Priority: P2 CC: david, emily.lamancusa, fridolin.somers, jonathan.druart, lucas, martin.renvoize, nick, pablo.bianchi, pedro.amorim, tomascohen, vivek.yadava, wainuiwitikapark
Version: Main   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
This fixes a syntax error in database update 230600061.pl (from bug 29002 which added the item booking feature to Koha 23.11.) - there was a comma that shouldn't have been there, which would cause the upgrade to fail if an old version of the following plugin was installed: https://github.com/bywatersolutions/koha-plugin-room-reservations
Version(s) released in:
24.11,24.05.02,23.11.07
Circulation function:
Bug Depends on: 34520, 35473    
Bug Blocks:    
Attachments: Schema of the DB, with mysqldump --no-data
apt upgrade output, 23.11.05-1 all [upgradable from: 22.05.21-1], schema change section
apt upgrade output after changing version to 22.0500000
Bug 36424: Remove trailing comma

Description Pablo AB 2024-03-26 00:40:04 UTC
Following Joubu advice on bug 34520#c12:

After that bug has being fixed, I tried (today 2024-03-25) once again:

Unpacking koha-common (23.11.03-1) over (22.05.19-1) ...
Setting up koha-common (23.11.03-1) ...   
[...]
Upgrading database schema for myinstance 
[...]
Upgrade to 22.06.00.078  [21:13:26]: Bug 24860 - Add ability to place item group level holds                                                                                                                                                  
ERROR - {UNKNOWN}: DBI Exception: DBD::mysql::db do failed: Can't create table `koha_myinstance`.`reserves` (errno: 150 "Foreign key constraint is incorrectly formed") at /usr/share/koha/lib/C4/Installer.pm line 741                     
                                       
dpkg: error processing package koha-common (--configure):    
```
```

DB host has a 10.3.34-MariaDB-0+deb10u1-log.
Comment 1 Katrin Fischer 2024-04-01 16:21:45 UTC
Do we have an idea what's going wrong here?
Comment 2 Pedro Amorim 2024-04-02 10:16:44 UTC
Unable to reproduce when upgrading k-t-d from 22.05.x to 23.11.x, that particular db rev runs without issue.
@Pablo are you able to run the following scripts and check their output (before upgrade):
misc/maintenance/search_for_data_inconsistencies.pl
misc/maintenance/audit_database.pl
Comment 3 Pablo AB 2024-04-02 23:50:43 UTC
@Pedro, after ~8', the output of search_for_data_inconsistencies.pl was:

```
== Items do not have itype defined ==
        * Item with itemnumber=39024 does not have a itype value, additionally no item type defined for biblionumber=22362
        [7 similar lines]
=> The system preference item-level_itypes expects item types to be defined at item level
```

Are you sure this version should have `audit_database.pl`? Is not there.

BTW, `perldoc search_for_data_inconsistencies.pl` return a malformed output.
Comment 4 Pedro Amorim 2024-04-03 08:33:18 UTC
(In reply to Pablo AB from comment #3)
> @Pedro, after ~8', the output of search_for_data_inconsistencies.pl was:
> 
> ```
> == Items do not have itype defined ==
>         * Item with itemnumber=39024 does not have a itype value,
> additionally no item type defined for biblionumber=22362
>         [7 similar lines]
> => The system preference item-level_itypes expects item types to be defined
> at item level
> ```
> 
> Are you sure this version should have `audit_database.pl`? Is not there.
> 
> BTW, `perldoc search_for_data_inconsistencies.pl` return a malformed output.

You're right, apologies. audit_database.pl was added by bug 34064 in version 23.11, although you likely would be able to backport it for this specific use case.

Looking at 220600078.pl, what you could do, before upgrading, is manually check the output of
SHOW CREATE TABLE reserves;
SHOW CREATE TABLE old_reserves;
SHOW CREATE TABLE item_groups;

and compare that with what's in kohastructure.sql. This may provide some clues.
Comment 5 Pablo AB 2024-04-03 19:17:41 UTC
COMMENTS apart, they are identical except for these differences:

reserves: These lines are swapped. Like this on show create:

​  KEY `itemtype` (`itemtype`),
  KEY `desk_id` (`desk_id`),

old_reserves: Only on show create:

  KEY `old_desk_id` (`desk_id`),

About `SHOW CREATE TABLE item_groups;`:

ERROR 1146 (42S02): Table 'koha_myinstance.item_groups' doesn't exist
Comment 6 Pedro Amorim 2024-04-04 08:32:11 UTC
Bug 24857 is adding the table item_groups. This is DBrev 220600014.
Bug 24860, the one you report failing for you, it's DBrev 220600078.
Both bugs were added for 22.11.

It appears, for some reason, your schema ended up without the item_groups table, and that should exist by the time DBrev 220600078 runs.
What's the full output of updatedatabase when you perform the update? Does bug 24857 DBrev 220600014 run? Does the message 'Added new table 'item_groups'' show? If not, any error (related to 220600014?)
Comment 7 Pablo AB 2024-04-09 23:42:00 UTC
> It appears, for some reason, your schema ended up without the item_groups table, and that should exist by the time DBrev 220600078 runs.

Not sure if there is a misunderstanding. Those tests were run on the current rolled back instance, not the broken middle-ground upgrade.

> What's the full output of updatedatabase when you perform the update? Does bug 24857 DBrev 220600014 run? Does the message 'Added new table 'item_groups'' show? If not, any error (related to 220600014?)

I tried again the upgrade. No mention of Bug 24857 or `item_groups` or 220600078 or 220600014.

In this broken state, cron:

/etc/cron.hourly/koha-common:
DBIx::Class::Storage::DBI::_dbh_execute(): DBI Exception: DBD::mysql::st execute failed: Unknown column 'me.renewals_count' in 'field list' at /usr/share/koha/lib/Koha/Objects.pm line 317
myinstance: 11 status returned by "/usr/share/koha/bin/cronjobs/automatic_checkin.pl"
run-parts: /etc/cron.hourly/koha-common exited with return code 1

I'll leave it in this state, for any further analysis I could test before the rollback.

Also, if helps, I could attach the output of the `apt upgrade` (after the update, after moving 22.05 → 23.11 on koha.list).
Comment 8 Pablo AB 2024-04-10 18:02:58 UTC
In the broken state `SHOW CREATE TABLE item_groups;` also..

ERROR 1146 (42S02): Table 'koha_myinstance.item_groups' doesn't exist
Comment 9 Pablo AB 2024-04-15 23:16:04 UTC
I set to critical, as Jonathan Druart did to Bug 34520.
Comment 10 Tomás Cohen Arazi (tcohen) 2024-05-29 17:21:47 UTC
Pablo: can you share your DB structure? There's a mysqldump option for that. Otherwise the exchange is difficult to follow.

Thanks in advance!
Comment 11 Tomás Cohen Arazi (tcohen) 2024-05-29 17:27:11 UTC
(In reply to Tomás Cohen Arazi from comment #10)
> Pablo: can you share your DB structure? There's a mysqldump option for that.
> Otherwise the exchange is difficult to follow.
> 
> Thanks in advance!

I mean the one before the attempt to upgrade.
Comment 12 Martin Renvoize (ashimema) 2024-06-11 16:31:48 UTC
Did you overcome this and come to the conclusion it was a local data issue?
Comment 13 Pablo AB 2024-06-12 16:51:53 UTC
Created attachment 167670 [details]
Schema of the DB, with mysqldump --no-data
Comment 14 Pablo AB 2024-06-12 16:56:33 UTC
> Did you overcome this and come to the conclusion it was a local data issue?

Nop, I couldn't jump to this conclusion. Was a pretty standard installation started from around 19.05, AFAIK was never touched directly.
Comment 15 Emily Lamancusa (emlam) 2024-06-14 20:18:16 UTC
Pablo, can you try the upgrade again, and copy everything in the output starting from
Upgrading database schema for myinstance 
until the error appears?
Comment 16 Pablo AB 2024-06-14 22:12:27 UTC
Created attachment 167773 [details]
apt upgrade output, 23.11.05-1 all [upgradable from: 22.05.21-1], schema change section
Comment 17 Emily Lamancusa (emlam) 2024-06-18 15:39:07 UTC
Thanks for sharing your apt upgrade output, Pablo. It seems like your system didn't roll back all the way after one of the previous upgrade attempts - your database upgrade started at 22.06.00.041, but it should have started at 22.06.00.000. That would explain why your system doesn't have the item_groups table, since it skipped that part of the upgrade.

You might need to try backing up your data and importing into a fresh install, but there might be other options - I'm not an expert on upgrading. I'd suggest asking around on the Koha community chat (https://chat.koha-community.org/), or on the mailing lists.
Comment 18 Pablo AB 2024-07-01 23:06:51 UTC
I asked on chat, https://chat.koha-community.org/koha-community/pl/drr98y7z13dqxj8mrpjbj8f6je giving more details. The issue happen on two instances on two different hosts, one of them a production server which I never tried to upgrade out of 22.05.x. I have no clue why I did get to have a .06/development and a 040. The other host/instance (test/sandbox) have the same strange version. 

Not sure also how to solve it either. Just an UPDATE to version, changing the `22.0600040` to `22.0500000`? All the schema upgrades are idempotent?

It seems that it's just me, so maybe this bug should be closed.
Comment 19 Katrin Fischer 2024-07-02 07:28:01 UTC
(In reply to Pablo AB from comment #18)
> I asked on chat,
> https://chat.koha-community.org/koha-community/pl/drr98y7z13dqxj8mrpjbj8f6je
> giving more details. The issue happen on two instances on two different
> hosts, one of them a production server which I never tried to upgrade out of
> 22.05.x. I have no clue why I did get to have a .06/development and a 040.
> The other host/instance (test/sandbox) have the same strange version. 
> 
> Not sure also how to solve it either. Just an UPDATE to version, changing
> the `22.0600040` to `22.0500000`? All the schema upgrades are idempotent?
> 
> It seems that it's just me, so maybe this bug should be closed.

You could take a backup and try that. The database updates should be idempotent.

Important bit is to really pay attention on the output of the update script to see where it gets stuck and resolve any issues found before continuing.
Comment 20 Pablo AB 2024-07-02 20:38:41 UTC
This time I hit a further step: It goes through Bug 24860 (Upgrade to 22.06.00.078) but crash later, on Bug 29002 - Add bookings table (Upgrade to 23.06.00.061).

1. apt update, upgrade, on 22.05 (22.0600040 on db)
2. koha-mysql $INSTANCE -e  'UPDATE systempreferences SET VALUE = '22.0500000' WHERE variable = "version"'
3. Set 23.11 on `koha.list`
4. apt update, upgrade; koha-common (23.11.06-2) over (22.05.22-1), stdout/err to txt (attached).
Comment 21 Pablo AB 2024-07-02 20:40:52 UTC
Created attachment 168403 [details]
apt upgrade output after changing version to 22.0500000
Comment 22 Katrin Fischer 2024-07-03 12:31:21 UTC
Can you please paste the output/error here?
Comment 23 Pedro Amorim 2024-07-03 12:35:46 UTC
Created attachment 168446 [details] [review]
Bug 36424: Remove trailing comma
Comment 24 Pedro Amorim 2024-07-03 12:37:34 UTC
(In reply to Pablo AB from comment #20)
> This time I hit a further step: It goes through Bug 24860 (Upgrade to
> 22.06.00.078) but crash later, on Bug 29002 - Add bookings table (Upgrade to
> 23.06.00.061).
> 
> 1. apt update, upgrade, on 22.05 (22.0600040 on db)
> 2. koha-mysql $INSTANCE -e  'UPDATE systempreferences SET VALUE =
> '22.0500000' WHERE variable = "version"'
> 3. Set 23.11 on `koha.list`
> 4. apt update, upgrade; koha-common (23.11.06-2) over (22.05.22-1),
> stdout/err to txt (attached).

Are you able to test my latest patch for this use case? I believe there is an extra trailing comma in the db rev.
Also, can you confirm if the Koha system this is happening on has the following plugin installed:
https://github.com/bywatersolutions/koha-plugin-room-reservations
Comment 25 Pablo AB 2024-07-03 23:50:04 UTC
> Are you able to test my latest patch for this use case? I believe there is an extra trailing comma in the db rev.

I also saw that trailing comma, since it seems to break on that query, and all variables are defined and with its contents. Removing the comma it runs the query, but breaks on the next query, `ALTER TABLE $rooms_table RENAME INDEX..`.

> Also, can you confirm if the Koha system this is happening on has the following plugin installed:
https://github.com/bywatersolutions/koha-plugin-room-reservations

Yes! It is installed:

- Name: Room Reservations Plugin ENABLED
- Description: This plugin provides a room reservation solution on both intranet and OPAC interfaces.
- Author: Lee Jamison
- Plugin version: 3.2.1
- Minimum Koha version: 3.22
- Last updated: 22/08/2022

Should be disabled/removed before the upgrade?
Comment 26 Pablo AB 2024-07-04 00:30:22 UTC
(In reply to Katrin Fischer from comment #22)
> Can you please paste the output/error here?

It's attached, also here:

Upgrading database schema for myinstance
Upgrade to 22.06.00.000  [17:13:49]: Increase DBRev for 22.06
Upgrade to 22.06.00.001  [17:13:49]: Bug 23659 - Allow hold pickup location to default to item home branch for item-level holds
[...]
Upgrade to 23.06.00.061  [17:14:59]: Bug 29002 - Add bookings table
ERROR - {UNKNOWN}: DBI Exception: DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 5 at /usr/share/koha/lib/C4/Installer.pm line 741
dpkg: error processing package koha-common (--configure):

Breaks on RENAME TABLE query on 230600061.pl, but removing the comma (line 5 on the query), then on the first ALTER TABLE. Maybe should be an IF EXIST or DROP TABLEs?
Comment 27 Pedro Amorim 2024-07-04 08:29:58 UTC
(In reply to Pablo AB from comment #26)
> Breaks on RENAME TABLE query on 230600061.pl, but removing the comma (line 5
> on the query), then on the first ALTER TABLE. Maybe should be an IF EXIST or
> DROP TABLEs?

How do we know it's then breaking on the first ALTER TABLE? What's the error?

 (In reply to Pablo AB from comment #25)
> Should be disabled/removed before the upgrade?

That plugin creates and utilizes a database table called 'bookings'.
The DB REV we're discussing here is renaming that table if it already exists, because the table added in bug 29002 is also named 'bookings'.

In theory, if you uninstall that plugin:
https://github.com/bywatersolutions/koha-plugin-room-reservations/blob/master/Koha/Plugin/Com/MarywoodUniversity/RoomReservations.pm#L248
It should remove all plugin related tables and then the DB rev will no longer consider renaming anything because 'bookings' will not exist.
Comment 28 Pedro Amorim 2024-07-04 09:38:55 UTC
I checked out branch 23.05.x (previous to bookings bug 29002)
I installed bywater's bookings plugin version 3.2.1 (when it was still using table named 'bookings'.
I ran the db rev, it errored, I applied my patch, it no longer errors and finishes successfully:

kohadev-koha@kohadevbox:koha(23.05.x)$ updatedatabase
Migrated room reservations plugin to it's own namespace
You MUST upgrade to the latest room reservation plugin to continue using it
Plugin disabled, please re-enable once you have upgraded it
DEV atomic update /kohadevbox/koha/installer/data/mysql/atomicupdate/test_29002.pl  [09:36:21]: Bug 29002 - Add bookings table
        Added new table 'bookings'
        Added column 'items.bookable'
        Added column 'deleteditems.bookable'
        Added new permission 'manage_bookings'

@Katrin the patch "Bug 36424; Remove trailing comma" is still required.
Comment 29 Katrin Fischer 2024-07-04 11:26:30 UTC
(In reply to Pedro Amorim from comment #28)
> I checked out branch 23.05.x (previous to bookings bug 29002)
> I installed bywater's bookings plugin version 3.2.1 (when it was still using
> table named 'bookings'.
> I ran the db rev, it errored, I applied my patch, it no longer errors and
> finishes successfully:
> 
> kohadev-koha@kohadevbox:koha(23.05.x)$ updatedatabase
> Migrated room reservations plugin to it's own namespace
> You MUST upgrade to the latest room reservation plugin to continue using it
> Plugin disabled, please re-enable once you have upgraded it
> DEV atomic update
> /kohadevbox/koha/installer/data/mysql/atomicupdate/test_29002.pl 
> [09:36:21]: Bug 29002 - Add bookings table
>         Added new table 'bookings'
>         Added column 'items.bookable'
>         Added column 'deleteditems.bookable'
>         Added new permission 'manage_bookings'
> 
> @Katrin the patch "Bug 36424; Remove trailing comma" is still required.

Thanks Pedro - easiest way to get my attention is to set back to PQA - it shows up in my list that way.
Comment 30 Katrin Fischer 2024-07-04 11:27:41 UTC
Pushed "Remove trailing comma" as a follow-up to main.
Comment 31 Katrin Fischer 2024-07-04 11:29:12 UTC
... not a real follow-up. Sorry, got a little confused here.
Comment 32 Pedro Amorim 2024-07-04 12:53:14 UTC
I want to update this bug's title to mention that fix specifically, but wanting on Pablo's reply to my last comments to ensure nothing else is required here and we've isolated the issue successfully.
Comment 33 Katrin Fischer 2024-07-04 15:30:12 UTC
I will wait with the actual push for confirmation from Pablo.
Comment 34 Katrin Fischer 2024-07-05 07:05:04 UTC
*** Bug 37259 has been marked as a duplicate of this bug. ***
Comment 35 Katrin Fischer 2024-07-05 13:46:23 UTC
I think as this has just been reported another time we should go ahead and fix the obvious syntax error. If there are other mistakes, we can put them on a new bug later.
Comment 36 Katrin Fischer 2024-07-05 13:50:29 UTC
Pushed for 24.11!

Well done everyone, thank you!
Comment 37 Pedro Amorim 2024-07-05 13:52:10 UTC
@Katrin I'm changing the bug title to 23.06.00.061 as that is the DB rev with the issue and the one being fixed here.
Comment 38 Katrin Fischer 2024-07-05 13:53:12 UTC
(In reply to Pedro Amorim from comment #37)
> @Katrin I'm changing the bug title to 23.06.00.061 as that is the DB rev
> with the issue and the one being fixed here.

Perfect, thank you.
Comment 39 Pablo AB 2024-07-05 14:48:27 UTC
(In reply to Pedro Amorim from comment #27)
> (In reply to Pablo AB from comment #26)
>> Breaks on RENAME TABLE query on 230600061.pl, but removing the comma (line 5
>> on the query), then on the first ALTER TABLE. Maybe should be an IF EXIST or
>> DROP TABLEs?
>
> How do we know it's then breaking on the first ALTER TABLE? What's the error?

I added some dirty print "after ALTER"
 
>  (In reply to Pablo AB from comment #25)
> That plugin creates and utilizes a database table called 'bookings'.
> ...
> It should remove all plugin related tables and then the DB rev will no
> longer consider renaming anything because 'bookings' will not exist.

I've uninstalled the plugin and now `koha-mysql $INSTANCE -e  'SHOW TABLES;' | grep book` only returns `aqbooksellers`.

---

I disabled and uninstalled the plugin and then followed the upgrade steps (as in comment 20) and finished successfully!! :)
Comment 40 Lucas Gass (lukeg) 2024-07-11 19:23:59 UTC
Backported to 24.05.x for upcoming 24.05.02
Comment 41 Fridolin Somers 2024-07-17 14:19:49 UTC
Pushed to 23.11.x for 23.11.07
Comment 42 Wainui Witika-Park 2024-07-19 03:40:45 UTC
not backporting to 23.05.x unless requested