Bugzilla – Attachment 45777 Details for
Bug 15336
Script for merging vendors
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 15336 - Move code related to merging into Koha::Acquisition::Bookseller.pm
Bug-15336---Move-code-related-to-merging-into-Koha.patch (text/plain), 8.19 KB, created by
Alex Arnaud
on 2015-12-17 15:00:50 UTC
(
hide
)
Description:
Bug 15336 - Move code related to merging into Koha::Acquisition::Bookseller.pm
Filename:
MIME Type:
Creator:
Alex Arnaud
Created:
2015-12-17 15:00:50 UTC
Size:
8.19 KB
patch
obsolete
>From d5cbb6329ffe3777402d2bfc9577155a3da4dedf Mon Sep 17 00:00:00 2001 >From: Alex Arnaud <alex.arnaud@biblibre.com> >Date: Thu, 17 Dec 2015 16:00:20 +0100 >Subject: [PATCH] Bug 15336 - Move code related to merging into > Koha::Acquisition::Bookseller.pm > >--- > Koha/Acquisition/Bookseller.pm | 26 +++++++ > misc/migration_tools/merge_booksellers.pl | 38 +++++----- > t/Bookseller.t | 113 ++++++++++++++++++++++++++++- > 3 files changed, 156 insertions(+), 21 deletions(-) > >diff --git a/Koha/Acquisition/Bookseller.pm b/Koha/Acquisition/Bookseller.pm >index 79b1510..35d51b8 100644 >--- a/Koha/Acquisition/Bookseller.pm >+++ b/Koha/Acquisition/Bookseller.pm >@@ -111,6 +111,32 @@ sub insert { > return $self; > } > >+sub merge_to { >+ my ($self, $mergeto) = @_; >+ >+ my $error = ''; >+ my $result = {}; >+ >+ unless (my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto})) { >+ $error = "Unknown target bookseller number: $mergeto"; >+ return ($error, {}); >+ } >+ >+ foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) { >+ my $rs = Koha::Database->new()->schema->resultset($entity); >+ >+ my $foreign_key = $entity eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid'; >+ $rs = $rs->search({$foreign_key => $self->{id} }); >+ >+ while (my $e = $rs->next) { >+ $e->set_column($foreign_key, $mergeto); >+ $e->update(); >+ push @{ $result->{$entity} }, $e->id(); >+ } >+ } >+ return ($error, $result); >+} >+ > # TODO Move code from ModBookseller > sub update { > die "not implemented yet"; >diff --git a/misc/migration_tools/merge_booksellers.pl b/misc/migration_tools/merge_booksellers.pl >index 9ec664e..ec5156e 100644 >--- a/misc/migration_tools/merge_booksellers.pl >+++ b/misc/migration_tools/merge_booksellers.pl >@@ -44,36 +44,40 @@ if ($mergefrom eq $mergeto) { > pod2usage('Source and target booksellers should be different.'); > } > >+my $schema = Koha::Database->schema; >+$schema->storage->txn_begin; >+ > my $from_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergefrom}); > die "Unknown bookseller id ($mergefrom)" unless $from_bookseller; > >-my $to_bookseller = Koha::Acquisition::Bookseller->fetch({id => $mergeto}); >-die "Unknown bookseller id ($mergeto)" unless $to_bookseller; >- >-my $report; >-foreach my $entity (qw(Aqbasketgroup Aqbasket Aqcontract Aqcontact Aqinvoice Deleteditem Item Subscription)) { >- my $rs = Koha::Database->new()->schema->resultset($entity); >- my $foreign_key = $entity eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid'; >- $rs = $rs->search({$foreign_key => $mergefrom }); >- while (my $e = $rs->next) { >- $e->set_column('booksellerid', $mergeto); >- print "$entity n° " . $e->id() . " merged into bookseller n° $mergeto\n" if $verbose; >- $e->update() if $confirm; >- $report->{$entity}++; >- } >+my ($error, $report) = $from_bookseller->merge_to($mergeto); >+ >+if ($error) { >+ die $error; >+} >+ >+if ($confirm) { >+ $schema->storage->txn_commit; >+} >+else { >+ $schema->storage->txn_rollback; > } > > DelBookseller($mergefrom) if $confirm; > >-print "\n" if $verbose; >-print_report($report); >+if ($verbose) { >+ print_report($report); >+} >+ > print "Nothing done (test only). Use confirm option to commit changes in database\n" unless $confirm; > > sub print_report { > my $report = shift; > > foreach my $entity (keys %$report) { >- print "$entity merged: $report->{$entity}\n"; >+ my $count = scalar @{ $report->{$entity} }; >+ my $ids = join(', ', @{ $report->{$entity} }); >+ print "$entity merged: $count (numbers: $ids)\n"; > } > } > >diff --git a/t/Bookseller.t b/t/Bookseller.t >index 360a0ad..92f7127 100755 >--- a/t/Bookseller.t >+++ b/t/Bookseller.t >@@ -1,14 +1,119 @@ > #!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. > # >-# This Koha test module is a stub! >-# Add more tests here!!! >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. > > use strict; > use warnings; > >-use Test::More tests => 1; >+use C4::Acquisition qw( GetBasketgroup GetBasket GetInvoice ); >+use C4::Contract qw( GetContract ); >+use C4::Items qw ( GetItem ); >+use C4::Serials qw( GetSubscription ); >+use t::lib::TestBuilder; >+ >+use Test::More tests => 14; > > BEGIN { >- use_ok('C4::Bookseller'); >+ use_ok('Koha::Acquisition::Bookseller'); >+} >+ >+my $schema = Koha::Database->schema; >+$schema->storage->txn_begin; >+ >+our $builder = t::lib::TestBuilder->new; >+ >+my $bookseller1 = create('Aqbookseller'); >+my $booksellerid1 = $bookseller1->{id}; >+my $bookseller2 = create('Aqbookseller'); >+my $booksellerid2 = $bookseller2->{id}; >+ >+my $bg1 = create('Aqbasketgroup', $booksellerid1); >+my $bg2 = create('Aqbasketgroup', $booksellerid1); >+my $bg3 = create('Aqbasketgroup', $booksellerid2); >+ >+my $basket1 = create('Aqbasket', $booksellerid1); >+my $basket2 = create('Aqbasket', $booksellerid1); >+ >+my $contract = create('Aqcontract', $booksellerid1); >+ >+my $contact = create('Aqcontact', $booksellerid1); >+ >+my $invoice = create('Aqinvoice', $booksellerid1); >+ >+my $deleteditem = create('Deleteditem', $booksellerid1); >+ >+my $item1 = create('Item', $booksellerid1); >+ >+my $subscription = create('Subscription', $booksellerid1); >+ >+my $bookseller = Koha::Acquisition::Bookseller->fetch({id => $booksellerid1}); >+ >+my ($error, $result) = $bookseller->merge_to(345876876); >+is($error, "Unknown target bookseller number: 345876876", 'Merge to unknown bookseller return an error' ); >+ >+($error, $result) = $bookseller->merge_to($booksellerid2); >+my $expected_result = { >+ Aqbasketgroup => [$bg1->{id}, $bg2->{id}], >+ Aqbasket => [$basket1->{basketno}, $basket2->{basketno}], >+ Aqcontract => [$contract->{contractnumber}], >+ Aqcontact => [$contact->{id}], >+ Aqinvoice => [$invoice->{invoiceid}], >+ Deleteditem => [$deleteditem->{itemnumber}], >+ Item => [$item1->{itemnumber}], >+ Subscription => [$subscription->{subscriptionid}] >+}; >+ >+is_deeply($result, $expected_result, 'Result should be as expected'); >+ >+is(GetBasketgroup($bg1->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 1 has bookseller 2'); >+ >+is(GetBasketgroup($bg2->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 2 has bookseller 2'); >+ >+is(GetBasketgroup($bg3->{id})->{'booksellerid'}, $booksellerid2, 'Basket group 3 keep bookseller 2'); >+ >+is(GetBasket($basket1->{basketno})->{'booksellerid'}, $booksellerid2, 'Basket 1 has bookseller 2'); >+ >+is(GetBasket($basket2->{basketno})->{'booksellerid'}, $booksellerid2, 'Basket 2 has bookseller 2'); >+ >+is(GetContract({contractnumber => $contract->{contractnumber}})->{'booksellerid'}, $booksellerid2, 'Basket 1 has bookseller 2'); >+ >+is(C4::Bookseller::Contact->fetch($contact->{id})->{'booksellerid'}, $booksellerid2, 'Contact 1 has booksellerid 2'); >+ >+is(GetInvoice($invoice->{invoiceid})->{'booksellerid'}, $booksellerid2, 'Invoice has bookseller 2'); >+ >+my $item = Koha::Database->new()->schema->resultset('Deleteditem') >+ ->find({itemnumber => $deleteditem->{itemnumber}}); >+is($item->get_column('booksellerid'), $booksellerid2, 'Deleted item has bookseller 2'); >+ >+is(GetItem($item1->{itemnumber})->{'booksellerid'}, $booksellerid2, 'Item has bookseller 2'); >+ >+is(GetSubscription($subscription->{subscriptionid})->{'aqbooksellerid'}, $booksellerid2, 'Subscription has bookseller 2'); >+ >+$schema->storage->txn_rollback; >+ >+sub create { >+ my ($source, $booksellerid) = @_; >+ >+ my $build = { source => $source}; >+ my $foreign_key = $source eq 'Subscription' ? 'aqbooksellerid' : 'booksellerid'; >+ $build->{value} = {$foreign_key => $booksellerid} if $booksellerid; >+ >+ my $entity = $builder->build($build); >+ >+ return $entity; > } > >+1; >-- >1.7.10.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 15336
:
45521
|
45530
|
45607
|
45777
|
49313
|
61070
|
61071
|
61104
|
61105
|
61108
|
61109
|
64595
|
66765
|
66766
|
66767
|
68676
|
72023
|
72734
|
73078