From 53feed41524661bc010b436fc262f460485156ee Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 12 Jul 2022 15:55:03 +0200 Subject: [PATCH] Bug 31142: Don't generate objects if more than 1 level deep --- installer/data/mysql/kohastructure.sql | 24 ++++++++++++------------ t/lib/TestBuilder.pm | 21 ++++++++++++++++----- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 54676b54e78..13a1c49dcc1 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1421,8 +1421,8 @@ CREATE TABLE `borrowers` ( `B_email` mediumtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the patron/borrower''s alternate email address', `B_phone` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'the patron/borrower''s alternate phone number', `dateofbirth` date DEFAULT NULL COMMENT 'the patron/borrower''s date of birth (YYYY-MM-DD)', - `branchcode` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'foreign key from the branches table, includes the code of the patron/borrower''s home branch', - `categorycode` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'foreign key from the categories table, includes the code of the patron category', + `branchcode` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'foreign key from the branches table, includes the code of the patron/borrower''s home branch', + `categorycode` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'foreign key from the categories table, includes the code of the patron category', `dateenrolled` date DEFAULT NULL COMMENT 'date the patron was added to Koha (YYYY-MM-DD)', `dateexpiry` date DEFAULT NULL COMMENT 'date the patron/borrower''s card is set to expire (YYYY-MM-DD)', `password_expiration_date` date DEFAULT NULL COMMENT 'date the patron/borrower''s password is set to expire (YYYY-MM-DD)', @@ -1478,8 +1478,8 @@ CREATE TABLE `borrowers` ( KEY `firstname_idx` (`firstname`(191)), KEY `othernames_idx` (`othernames`(191)), KEY `sms_provider_id` (`sms_provider_id`), - CONSTRAINT `borrowers_ibfk_1` FOREIGN KEY (`categorycode`) REFERENCES `categories` (`categorycode`), - CONSTRAINT `borrowers_ibfk_2` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`), + CONSTRAINT `borrowers_ibfk_1` FOREIGN KEY (`categorycode`) REFERENCES `categories` (`categorycode`) ON DELETE SET NULL ON UPDATE CASCADE, + CONSTRAINT `borrowers_ibfk_2` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT `borrowers_ibfk_3` FOREIGN KEY (`sms_provider_id`) REFERENCES `sms_providers` (`id`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; @@ -1561,13 +1561,13 @@ DROP TABLE IF EXISTS `branchtransfers`; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `branchtransfers` ( `branchtransfer_id` int(12) NOT NULL AUTO_INCREMENT COMMENT 'primary key', - `itemnumber` int(11) NOT NULL DEFAULT 0 COMMENT 'the itemnumber that it is in transit (items.itemnumber)', + `itemnumber` int(11) NOT NULL COMMENT 'the itemnumber that it is in transit (items.itemnumber)', `daterequested` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'the date the transfer was requested', `datesent` datetime DEFAULT NULL COMMENT 'the date the transfer was initialized', - `frombranch` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'the branch the transfer is coming from', + `frombranch` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'the branch the transfer is coming from', `datearrived` datetime DEFAULT NULL COMMENT 'the date the transfer arrived at its destination', `datecancelled` datetime DEFAULT NULL COMMENT 'the date the transfer was cancelled', - `tobranch` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'the branch the transfer was going to', + `tobranch` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'the branch the transfer was going to', `comments` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'any comments related to the transfer', `reason` ENUM('Manual','StockrotationAdvance','StockrotationRepatriation','ReturnToHome','ReturnToHolding','RotatingCollection','Reserve','LostReserve','CancelReserve','TransferCancellation','Recall','RecallCancellation') COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'what triggered the transfer', `cancellation_reason` enum('Manual','StockrotationAdvance','StockrotationRepatriation','ReturnToHome','ReturnToHolding','RotatingCollection','Reserve','LostReserve','CancelReserve','ItemLost','WrongTransfer','CancelRecall') COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'what triggered the transfer cancellation', @@ -3053,9 +3053,9 @@ DROP TABLE IF EXISTS `issues`; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `issues` ( `issue_id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key for issues table', - `borrowernumber` int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the borrowers table for the patron this item was checked out to', + `borrowernumber` int(11) NOT NULL COMMENT 'foreign key, linking this to the borrowers table for the patron this item was checked out to', `issuer_id` int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the borrowers table for the user who checked out this item', - `itemnumber` int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the items table for the item that was checked out', + `itemnumber` int(11) NOT NULL COMMENT 'foreign key, linking this to the items table for the item that was checked out', `date_due` datetime DEFAULT NULL COMMENT 'datetime the item is due (yyyy-mm-dd hh:mm::ss)', `branchcode` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'foreign key, linking to the branches table for the location the item was checked out', `returndate` datetime DEFAULT NULL COMMENT 'date the item was returned, will be NULL until moved to old_issues', @@ -3077,8 +3077,8 @@ CREATE TABLE `issues` ( KEY `branchcode_idx` (`branchcode`), KEY `bordate` (`borrowernumber`,`timestamp`), KEY `issues_ibfk_borrowers_borrowernumber` (`issuer_id`), - CONSTRAINT `issues_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON UPDATE CASCADE, - CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON UPDATE CASCADE, + CONSTRAINT `issues_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT `issues_ibfk_2` FOREIGN KEY (`itemnumber`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `issues_ibfk_borrowers_borrowernumber` FOREIGN KEY (`issuer_id`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; @@ -3951,7 +3951,7 @@ DROP TABLE IF EXISTS `old_issues`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `old_issues` ( - `issue_id` int(11) NOT NULL COMMENT 'primary key for issues table', + `issue_id` int(11) DEFAULT NULL COMMENT 'primary key for issues table', `borrowernumber` int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the borrowers table for the patron this item was checked out to', `issuer_id` int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the borrowers table for the user who checked out this item', `itemnumber` int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the items table for the item that was checked out', diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm index 94ffa975103..68df3d91a41 100644 --- a/t/lib/TestBuilder.pm +++ b/t/lib/TestBuilder.pm @@ -27,6 +27,7 @@ sub new { $self->{gen_type} = _gen_type(); $self->{default_values} = _gen_default_values(); + return $self; } @@ -70,6 +71,8 @@ sub build_object { my $class = $params->{class}; my $value = $params->{value}; + my $depth = delete $params->{depth} || 0; + if ( not defined $class ) { carp "Missing class param"; return; @@ -81,7 +84,7 @@ sub build_object { load $class; my $source = $class->_type; - my $hashref = $self->build({ source => $source, value => $value }); + my $hashref = $self->build({ source => $source, value => $value, depth => $depth }); my $object; if ( $class eq 'Koha::Old::Patrons' ) { $object = $class->search({ borrowernumber => $hashref->{borrowernumber} })->next; @@ -111,6 +114,9 @@ sub build { } my $value = $params->{value}; + my $depth = delete $params->{depth} || 0; + $depth++; + my @unknowns = grep( !/^(source|value)$/, keys %{ $params }); carp "Unknown parameter(s): ", join( ', ', @unknowns ) if scalar @unknowns; @@ -122,13 +128,13 @@ sub build { # loop thru all fk and create linked records if needed # fills remaining entries in $col_values - my $foreign_keys = $self->_getForeignKeys( { source => $source } ); + my $foreign_keys = $self->_getForeignKeys( { source => $source, depth => $depth } ); for my $fk ( @$foreign_keys ) { # skip when FK points to itself: e.g. borrowers:guarantorid next if $fk->{source} eq $source; my $keys = $fk->{keys}; my $tbl = $fk->{source}; - my $res = $self->_create_links( $tbl, $keys, $col_values, $value ); + my $res = $self->_create_links( $tbl, $keys, $col_values, $value, $depth ); return if !$res; # failed: no need to go further foreach( keys %$res ) { # save new values $col_values->{$_} = $res->{$_}; @@ -205,7 +211,7 @@ sub build_sample_item { sub _create_links { # returns undef for failure to create linked records # otherwise returns hashref containing new column values for parent record - my ( $self, $linked_tbl, $keys, $col_values, $value ) = @_; + my ( $self, $linked_tbl, $keys, $col_values, $value, $depth ) = @_; my $fk_value = {}; my ( $cnt_scalar, $cnt_null ) = ( 0, 0 ); @@ -241,7 +247,7 @@ sub _create_links { return {} if $self->schema->resultset($linked_tbl)->find( \%fk_pk_value ); } # create record with a recursive build call - my $row = $self->build({ source => $linked_tbl, value => $fk_value }); + my $row = $self->build({ source => $linked_tbl, value => $fk_value, depth => $depth }); return if !$row; # failure # Finally, only return the new values @@ -343,6 +349,11 @@ sub _getForeignKeys { my @keys; while( my ($col_fk_name, $col_name) = each(%{$rel_info->{cond}}) ) { $col_name =~ s|self.(\w+)|$1|; + + next + if $params->{depth} > 1 + && $source->columns_info->{$col_name}->{is_nullable}; + $col_fk_name =~ s|foreign.(\w+)|$1|; push @keys, { col_name => $col_name, -- 2.25.1