Bug 37510 - Koha::Object->delete should throw a Koha::Exception if there's a parent row constraint
Summary: Koha::Object->delete should throw a Koha::Exception if there's a parent row c...
Status: RESOLVED FIXED
Alias: None
Product: Koha
Classification: Unclassified
Component: Architecture, internals, and plumbing (show other bugs)
Version: unspecified
Hardware: All All
: P5 - low normal
Assignee: Tomás Cohen Arazi (tcohen)
QA Contact: Pedro Amorim
URL:
Keywords:
Depends on: 37419
Blocks: 37513
  Show dependency treegraph
 
Reported: 2024-07-29 11:35 UTC by Tomás Cohen Arazi (tcohen)
Modified: 2024-10-21 17:54 UTC (History)
9 users (show)

See Also:
Change sponsored?: ---
Patch complexity: Small patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
24.11.00,24.05.04
Circulation function:


Attachments
Bug 37510: Unit tests (1.26 KB, patch)
2024-07-29 12:28 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review
Bug 37510: Make Koha::Object->delete throw Koha::Exception (1.90 KB, patch)
2024-07-29 12:29 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review
Bug 37510: Unit tests (1.48 KB, patch)
2024-07-29 14:34 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review
Bug 37510: Make Koha::Object->delete throw Koha::Exception (2.83 KB, patch)
2024-07-29 14:34 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review
Bug 37510: Unit tests (1.53 KB, patch)
2024-07-29 21:52 UTC, David Nind
Details | Diff | Splinter Review
Bug 37510: Make Koha::Object->delete throw Koha::Exception (2.88 KB, patch)
2024-07-29 21:52 UTC, David Nind
Details | Diff | Splinter Review
Bug 37510: Unit tests (1.59 KB, patch)
2024-08-01 11:12 UTC, Pedro Amorim
Details | Diff | Splinter Review
Bug 37510: Make Koha::Object->delete throw Koha::Exception (2.93 KB, patch)
2024-08-01 11:12 UTC, Pedro Amorim
Details | Diff | Splinter Review
Bug 37510: Fix Objects.t and remove FIXME (1.40 KB, patch)
2024-08-19 11:56 UTC, Tomás Cohen Arazi (tcohen)
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Tomás Cohen Arazi (tcohen) 2024-07-29 11:35:48 UTC

    
Comment 1 Tomás Cohen Arazi (tcohen) 2024-07-29 12:28:58 UTC
Created attachment 169829 [details] [review]
Bug 37510: Unit tests
Comment 2 Tomás Cohen Arazi (tcohen) 2024-07-29 12:29:01 UTC
Created attachment 169830 [details] [review]
Bug 37510: Make Koha::Object->delete throw Koha::Exception

This patch makes Koha::Object->delete wrap DBIC exceptions on FK
constraints and throw a Koha::Exception::Object::FKConstraint exception
instead. This will allow us better handling it from the callers.

To test:
1. Apply the unit tests patch
2. Run:
   $ ktd --shell
  k$ prove t/db_dependent/Koha/Object.t
=> FAIL: A DBIC exception is thrown instead, tests fail
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass!
5. Sign off :-D
Comment 3 Pedro Amorim 2024-07-29 13:27:17 UTC
Tests fail even after 2nd patch:

t/db_dependent/Koha/Object.t .. 22/23 
    #   Failed test 'threw Koha::Exception'
    #   at t/db_dependent/Koha/Object.t line 1253.
    # expecting: Koha::Exception
    # found: normal exit
DBIx::Class::Row::update(): Can't update Koha::Schema::Result::BiblioMetadata=HASH(0x63aab4788218): row not found at /kohadevbox/koha/Koha/Object.pm line 172
    # Looks like you planned 2 tests but ran 1.
    # Looks like you failed 1 test of 1 run.

#   Failed test 'delete() tests'
#   at t/db_dependent/Koha/Object.t line 1261.
DBIx::Class::Row::update(): Can't update Koha::Schema::Result::BiblioMetadata=HASH(0x63aab4788218): row not found at /kohadevbox/koha/Koha/Object.pm line 172
# Looks like your test exited with 255 just after 23.
t/db_dependent/Koha/Object.t .. Dubious, test returned 255 (wstat 65280, 0xff00)
Failed 1/23 subtests 

Test Summary Report
-------------------
t/db_dependent/Koha/Object.t (Wstat: 65280 (exited 255) Tests: 23 Failed: 1)
  Failed test:  23
  Non-zero exit status: 255
Files=1, Tests=23,  7 wallclock secs ( 0.03 usr  0.02 sys +  3.63 cusr  3.08 csys =  6.76 CPU)
Result: FAIL
Comment 4 Tomás Cohen Arazi (tcohen) 2024-07-29 14:34:16 UTC
Created attachment 169842 [details] [review]
Bug 37510: Unit tests
Comment 5 Tomás Cohen Arazi (tcohen) 2024-07-29 14:34:19 UTC
Created attachment 169843 [details] [review]
Bug 37510: Make Koha::Object->delete throw Koha::Exception

This patch makes Koha::Object->delete wrap DBIC exceptions on FK
constraints and throw a Koha::Exception::Object::FKConstraint exception
instead. This will allow us better handling it from the callers.

To test:
1. Apply the unit tests patch
2. Run:
   $ ktd --shell
  k$ prove t/db_dependent/Koha/Object.t
=> FAIL: A DBIC exception is thrown instead, tests fail
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass!
5. Sign off :-D
Comment 6 Tomás Cohen Arazi (tcohen) 2024-07-29 14:44:24 UTC
(In reply to Pedro Amorim from comment #3)
> Tests fail even after 2nd patch:
> 
> t/db_dependent/Koha/Object.t .. 22/23 
>     #   Failed test 'threw Koha::Exception'
>     #   at t/db_dependent/Koha/Object.t line 1253.
>     # expecting: Koha::Exception
>     # found: normal exit
> DBIx::Class::Row::update(): Can't update
> Koha::Schema::Result::BiblioMetadata=HASH(0x63aab4788218): row not found at
> /kohadevbox/koha/Koha/Object.pm line 172

That's interesting. It seems to me that you don't have 37419 applied and/or missing an `updatedatabase` run.

Can you retry with the newer patch (just changed the exception thrown) and provide me more context on how you're running it? Also MariaDB/MySQL version would be helpful.

I've tested this with:

```shell
$ docker ps | grep db-1
150d15b3232b   arm64v8/mariadb:10.5 ...
d2812db856b1   arm64v8/mysql:8.0    ...
```
Comment 7 David Nind 2024-07-29 21:52:47 UTC
Created attachment 169848 [details] [review]
Bug 37510: Unit tests

Signed-off-by: David Nind <david@davidnind.com>
Comment 8 David Nind 2024-07-29 21:52:49 UTC
Created attachment 169849 [details] [review]
Bug 37510: Make Koha::Object->delete throw Koha::Exception

This patch makes Koha::Object->delete wrap DBIC exceptions on FK
constraints and throw a Koha::Exception::Object::FKConstraint exception
instead. This will allow us better handling it from the callers.

To test:
1. Apply the unit tests patch
2. Run:
   $ ktd --shell
  k$ prove t/db_dependent/Koha/Object.t
=> FAIL: A DBIC exception is thrown instead, tests fail
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass!
5. Sign off :-D

Signed-off-by: David Nind <david@davidnind.com>
Comment 9 David Nind 2024-07-29 21:56:09 UTC
This worked for me by running an updatedatabase after the patches were applied.

Note that these warnings were there before the patches were applied:

prove t/db_dependent/Koha/Object.t
t/db_dependent/Koha/Object.t .. 1/23 Use of uninitialized value in numeric eq (==) at /kohadevbox/koha/Koha/Patron.pm line 446.
t/db_dependent/Koha/Object.t .. 7/23 C4::Context->userenv not defined! at /kohadevbox/koha/Koha/Patron.pm line 1891.
C4::Context->userenv not defined! at /kohadevbox/koha/Koha/Patron.pm line 1891.
t/db_dependent/Koha/Object.t .. 9/23 DBIx::Class::Storage::DBI::insert(): Missing value for primary key column 'currency' on Currency - perhaps you forgot to set its 'is_auto_increment' attribute during add_columns()? Treating 'currency' implicitly as an autoinc, and attempting value retrieval at /kohadevbox/koha/Koha/Object.pm line 172
t/db_dependent/Koha/Object.t .. ok     
All tests successful.
Files=1, Tests=23,  5 wallclock secs ( 0.03 usr  0.01 sys +  3.04 cusr  1.28 csys =  4.36 CPU)
Result: PASS
Comment 10 Pedro Amorim 2024-08-01 11:12:44 UTC
Created attachment 169938 [details] [review]
Bug 37510: Unit tests

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Comment 11 Pedro Amorim 2024-08-01 11:12:46 UTC
Created attachment 169939 [details] [review]
Bug 37510: Make Koha::Object->delete throw Koha::Exception

This patch makes Koha::Object->delete wrap DBIC exceptions on FK
constraints and throw a Koha::Exception::Object::FKConstraint exception
instead. This will allow us better handling it from the callers.

To test:
1. Apply the unit tests patch
2. Run:
   $ ktd --shell
  k$ prove t/db_dependent/Koha/Object.t
=> FAIL: A DBIC exception is thrown instead, tests fail
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass!
5. Sign off :-D

Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Pedro Amorim <pedro.amorim@ptfs-europe.com>
Comment 12 Pedro Amorim 2024-08-01 11:13:22 UTC
(In reply to Tomás Cohen Arazi from comment #6)
> That's interesting. It seems to me that you don't have 37419 applied and/or
> missing an `updatedatabase` run.

Apologies, yes I was missing the dependency.
Comment 13 Katrin Fischer 2024-08-16 15:37:19 UTC
Pushed for 24.11!

Well done everyone, thank you!
Comment 14 Katrin Fischer 2024-08-17 09:30:41 UTC
I believe this is related to the failing test on Jenkins:

18:27:32 koha_1       |                 #   Failed test at t/db_dependent/Koha/Objects.t line 773.
18:27:32 koha_1       |                 #          got: 'Koha::Exceptions::Object::FKConstraintDeletion'
18:27:32 koha_1       |                 #     expected: 'DBIx::Class::Exception'
18:27:32 koha_1       |                 # Looks like you failed 1 test of 7.
18:27:32 koha_1       | 
18:27:32 koha_1       |             #   Failed test 'Koha::Objects->delete'
18:27:32 koha_1       |             #   at t/db_dependent/Koha/Objects.t line 777.
18:27:32 koha_1       |             # Looks like you failed 1 test of 2.
18:27:32 koha_1       | 
18:27:32 koha_1       |         #   Failed test 'Overwritten Koha::Objects->delete - Koha::Patrons'
18:27:32 koha_1       |         #   at t/db_dependent/Koha/Objects.t line 778.
18:27:32 koha_1       |         # Looks like you failed 1 test of 2.
18:27:32 koha_1       | 
18:27:32 koha_1       |     #   Failed test 'Delete'
18:27:32 koha_1       |     #   at t/db_dependent/Koha/Objects.t line 782.
18:27:32 koha_1       |     # Looks like you failed 1 test of 2.
18:27:32 koha_1       | 
18:27:32 koha_1       | #   Failed test 'Return same values as DBIx::Class'
18:27:32 koha_1       | #   at t/db_dependent/Koha/Objects.t line 1065.
18:27:32 koha_1       | # Looks like you failed 1 test of 23.
18:27:32 koha_1       | [16:25:57] t/db_dependent/Koha/Objects.t ............................................... 
18:27:32 koha_1       | Dubious, test returned 1 (wstat 256, 0x100)
18:27:32 koha_1       | Failed 1/23 subtests
Comment 15 Tomás Cohen Arazi (tcohen) 2024-08-19 11:56:53 UTC
Created attachment 170464 [details] [review]
Bug 37510: Fix Objects.t and remove FIXME

This patch makes the tests check for the added exception instead of the
generic DBIx::Class::Exception.

A FIXME was set in place proposing we should throw a Koha::Exception
instead, so removing the FIXME as that's what this patch actually did.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 16 Katrin Fischer 2024-08-19 15:01:04 UTC
Pushed follow-up, thanks!
Comment 17 Lucas Gass (lukeg) 2024-09-11 16:52:37 UTC
Backported to 24.05.x for upcoming 24.05.04
Comment 18 Fridolin Somers 2024-10-21 12:11:27 UTC
Depends on Bug 37419 not in 23.11.x
Comment 19 David Nind 2024-10-21 17:54:29 UTC
No changes required to the manual.