View | Details | Raw Unified | Return to bug 18265
Collapse All | Expand All

(-)a/C4/Biblio.pm (-5 / +37 lines)
Lines 42-48 use Koha::Caches; Link Here
42
use Koha::Authority::Types;
42
use Koha::Authority::Types;
43
use Koha::Acquisition::Currencies;
43
use Koha::Acquisition::Currencies;
44
use Koha::Biblio::Metadata;
44
use Koha::Biblio::Metadata;
45
use Koha::Biblio::DeletedMetadata;
45
use Koha::Biblio::Metadatas;
46
use Koha::Biblio::Metadatas;
47
use Koha::Biblio::DeletedMetadatas;
46
use Koha::SearchEngine;
48
use Koha::SearchEngine;
47
use Koha::Libraries;
49
use Koha::Libraries;
48
50
Lines 257-263 sub AddBiblio { Link Here
257
    _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
259
    _koha_marc_update_biblioitem_cn_sort( $record, $olddata, $frameworkcode );
258
260
259
    # now add the record
261
    # now add the record
260
    ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
262
    ModBiblioMarc( $record, $biblionumber, $biblioitemnumber, $frameworkcode ) unless $defer_marc_save;
261
263
262
    # update OAI-PMH sets
264
    # update OAI-PMH sets
263
    if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
265
    if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
Lines 335-341 sub ModBiblio { Link Here
335
    _koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode );
337
    _koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode );
336
338
337
    # update the MARC record (that now contains biblio and items) with the new record data
339
    # update the MARC record (that now contains biblio and items) with the new record data
338
    &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
340
    &ModBiblioMarc( $record, $biblionumber, $biblioitemnumber, $frameworkcode );
339
341
340
    # modify the other koha tables
342
    # modify the other koha tables
341
    _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
343
    _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
Lines 426-432 sub DelBiblio { Link Here
426
    $sth->execute($biblionumber);
428
    $sth->execute($biblionumber);
427
    while ( my $biblioitemnumber = $sth->fetchrow ) {
429
    while ( my $biblioitemnumber = $sth->fetchrow ) {
428
430
429
        # delete this biblioitem
431
        #Handle biblio_metadata and deletedbiblio_metadata first before biblioitems operations CASCADE there
432
        $error = _koha_delete_biblio_metadata( $dbh, $biblioitemnumber, $biblionumber );
433
        return $error if $error;
434
430
        $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
435
        $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
431
        return $error if $error;
436
        return $error if $error;
432
    }
437
    }
Lines 436-441 sub DelBiblio { Link Here
436
    # delete cascade will prevent deletedbiblioitems rows
441
    # delete cascade will prevent deletedbiblioitems rows
437
    # from being generated by _koha_delete_biblioitems
442
    # from being generated by _koha_delete_biblioitems
438
    $error = _koha_delete_biblio( $dbh, $biblionumber );
443
    $error = _koha_delete_biblio( $dbh, $biblionumber );
444
    return $error if $error;
439
445
440
    logaction( "CATALOGUING", "DELETE", $biblionumber, "biblio" ) if C4::Context->preference("CataloguingLog");
446
    logaction( "CATALOGUING", "DELETE", $biblionumber, "biblio" ) if C4::Context->preference("CataloguingLog");
441
447
Lines 3438-3448 sub _koha_delete_biblioitems { Link Here
3438
    return;
3444
    return;
3439
}
3445
}
3440
3446
3447
=head2 _koha_delete_biblio_metadata
3448
3449
  $error = _koha_delete_biblio_metadata($dbh, $biblioitemnumber, $biblionumber);
3450
3451
Internal sub for deleting from biblio_metadata table -- also saves to deletedbiblio_metadata
3452
3453
C<$dbh> - the database handle
3454
C<$biblioitemnumber> - the biblioitemnumber of the biblio_metadata to be deleted
3455
3456
=cut
3457
3458
# FIXME: add error handling
3459
3460
sub _koha_delete_biblio_metadata {
3461
    my ( $dbh, $biblioitemnumber ) = @_;
3462
3463
    my $m = Koha::Biblio::Metadatas->find({biblioitemnumber => $biblioitemnumber});
3464
    die "_koha_delete_biblio_metadata($biblioitemnumber):> No biblio_metadata-row!" unless $m;
3465
3466
    my $dm = Koha::Biblio::DeletedMetadata->new( $m->_result->{_column_data} );
3467
    $dm->store;
3468
    $m->delete();
3469
    return undef;
3470
}
3471
3441
=head1 UNEXPORTED FUNCTIONS
3472
=head1 UNEXPORTED FUNCTIONS
3442
3473
3443
=head2 ModBiblioMarc
3474
=head2 ModBiblioMarc
3444
3475
3445
  &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3476
  &ModBiblioMarc($newrec,$biblionumber,$biblioitemnumber,$frameworkcode);
3446
3477
3447
Add MARC XML data for a biblio to koha
3478
Add MARC XML data for a biblio to koha
3448
3479
Lines 3453-3459 Function exported, but should NOT be used, unless you really know what you're do Link Here
3453
sub ModBiblioMarc {
3484
sub ModBiblioMarc {
3454
    # pass the MARC::Record to this function, and it will create the records in
3485
    # pass the MARC::Record to this function, and it will create the records in
3455
    # the marcxml field
3486
    # the marcxml field
3456
    my ( $record, $biblionumber, $frameworkcode ) = @_;
3487
    my ( $record, $biblionumber, $biblioitemnumber, $frameworkcode ) = @_;
3457
    if ( !$record ) {
3488
    if ( !$record ) {
3458
        carp 'ModBiblioMarc passed an undefined record';
3489
        carp 'ModBiblioMarc passed an undefined record';
3459
        return;
3490
        return;
Lines 3500-3505 sub ModBiblioMarc { Link Here
3500
    }
3531
    }
3501
3532
3502
    my $metadata = {
3533
    my $metadata = {
3534
        biblioitemnumber => $biblioitemnumber,
3503
        biblionumber => $biblionumber,
3535
        biblionumber => $biblionumber,
3504
        format       => 'marcxml',
3536
        format       => 'marcxml',
3505
        marcflavour  => C4::Context->preference('marcflavour'),
3537
        marcflavour  => C4::Context->preference('marcflavour'),
(-)a/Koha/Biblio/DeletedMetadata.pm (+32 lines)
Line 0 Link Here
1
package Koha::Biblio::DeletedMetadata;
2
3
# This file is part of Koha.
4
#
5
6
use Modern::Perl;
7
8
use Carp;
9
10
use Koha::Database;
11
12
use base qw(Koha::Object);
13
14
=head1 NAME
15
16
Koha::Biblio::DeletedMetadata - Koha Metadata Object class
17
18
=head1 API
19
20
=head2 Class Methods
21
22
=cut
23
24
=head3 type
25
26
=cut
27
28
sub _type {
29
    return 'DeletedbiblioMetadata';
30
}
31
32
1;
(-)a/Koha/Biblio/DeletedMetadatas.pm (+38 lines)
Line 0 Link Here
1
package Koha::Biblio::DeletedMetadatas;
2
3
# This file is part of Koha.
4
#
5
6
use Modern::Perl;
7
8
use Carp;
9
10
use Koha::Database;
11
12
use Koha::Biblio::Metadata;
13
14
use base qw(Koha::Objects);
15
16
=head1 NAME
17
18
Koha::Biblio::DeletedMetadatas - Koha Metadata Object set class
19
20
=head1 API
21
22
=head2 Class Methods
23
24
=cut
25
26
=head3 type
27
28
=cut
29
30
sub _type {
31
    return 'DeletedbiblioMetadata';
32
}
33
34
sub object_class {
35
    return 'Koha::Biblio::DeletedMetadata';
36
}
37
38
1;
(-)a/Koha/Biblio/Metadata.pm (-1 / +1 lines)
Lines 25-31 use base qw(Koha::Object); Link Here
25
25
26
=head1 NAME
26
=head1 NAME
27
27
28
Koha::Metadata - Koha Metadata Object class
28
Koha::Biblio::Metadata - Koha Metadata Object class
29
29
30
=head1 API
30
=head1 API
31
31
(-)a/installer/data/mysql/atomicupdate/Bug-18265-Followup_to_Buuug_17196.sql (+20 lines)
Line 0 Link Here
1
ALTER TABLE biblio_metadata DROP FOREIGN KEY `record_metadata_fk_1`;
2
ALTER TABLE biblio_metadata DROP KEY `biblio_metadata_uniq_key`;
3
ALTER TABLE biblio_metadata ADD COLUMN `biblioitemnumber` int(11) NOT NULL;
4
ALTER TABLE biblio_metadata ADD FOREIGN KEY `biblioitems_metadata_fk_1` (biblioitemnumber) REFERENCES biblioitems (biblioitemnumber) ON DELETE CASCADE ON UPDATE CASCADE;
5
ALTER TABLE biblio_metadata ADD UNIQUE KEY `biblio_metadata_uniq_key` (`biblioitemnumber`, `biblionumber`,`format`,`marcflavour`);
6
7
8
9
ALTER TABLE deletedbiblio_metadata DROP FOREIGN KEY `deletedrecord_metadata_fk_1`;
10
ALTER TABLE deletedbiblio_metadata DROP KEY `deletedbiblio_metadata_uniq_key`;
11
ALTER TABLE deletedbiblio_metadata ADD COLUMN `biblioitemnumber` int(11) NOT NULL;
12
## If this FK is preserved, most of the DelBiblio needs to be rewritten to gracefully
13
## move biblio and friends to deletedbiblio and deletedfriends without a chance for
14
## data loss in between.
15
## DelBiblio is implemented in a leaf-first way of handling the joint tables.
16
## You cannot INSERT leaf-first to deletedbiblio_metadata without the presence of deletedbiblioitems
17
#ALTER TABLE deletedbiblio_metadata ADD FOREIGN KEY `deletedbiblioitems_metadata_fk_1` (biblioitemnumber) REFERENCES deletedbiblioitems (biblioitemnumber) ON DELETE CASCADE ON UPDATE CASCADE;
18
ALTER TABLE deletedbiblio_metadata ADD UNIQUE KEY `deletedbiblio_metadata_uniq_key` (`biblioitemnumber`, `biblionumber`,`format`,`marcflavour`);
19
20
(-)a/t/db_dependent/Biblio_crud.t (-1 / +106 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
5
use Modern::Perl;
6
use Test::More;
7
8
use C4::Biblio;
9
use Koha::Biblios;
10
use Koha::Deletedbiblios;
11
use Koha::Biblio::Metadatas;
12
use Koha::Biblio::DeletedMetadatas;
13
14
15
16
##This test is not intended to fully cover all aspects of Biblio creation and it's linked business objects.
17
##It is simply intended for now to test biblio_metadata-table population.
18
subtest "Scenario: CRUD a Biblio, happy path", \&crudBiblio;
19
sub crudBiblio {
20
    my ($record, $kohaBiblio, $deletedKohaBiblio, $biblionumber, $biblioitemnumber, $metadata, $deletedMetadata);
21
    my $title = 'Incurable disease of test apparatuses';
22
    eval {
23
24
    $record = C4::Biblio::TransformKohaToMarc({
25
        'biblio.title'      => $title,
26
        'biblioitems.isbn'  => '0123456789',
27
    });
28
    ok($record,
29
        "Given a fresh Biblio from the top of my head");
30
31
32
    subtest "Create", sub {
33
34
        ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio($record,'');
35
        ok($biblionumber && $biblioitemnumber,
36
            "When the Biblio is added to DB");
37
    };
38
39
40
    subtest "Read", sub {
41
42
        $kohaBiblio = Koha::Biblios->find($biblionumber);
43
        ok($kohaBiblio,
44
            "Then it can be found from the DB");
45
46
        ok($kohaBiblio->biblionumber,
47
            "And it looks like a real biblio");
48
49
        $metadata = Koha::Biblio::Metadatas->find({biblioitemnumber => $biblioitemnumber});
50
        ok($metadata,
51
            "When I fetch the Metadata record from DB");
52
53
        is($metadata->format, 'marcxml',
54
            "Then the Metadata record format is 'marcxml'");
55
56
        is($metadata->biblionumber, $biblionumber,
57
            "And biblionumber matches the original Biblio");
58
59
        is($metadata->biblioitemnumber, $biblioitemnumber,
60
            "And the biblioitemnumber matches the original Biblio");
61
62
        #Shouldn't marcflavour be just flavour since we don't always store MARC to the biblio_metadata?
63
        is($metadata->marcflavour, C4::Context->preference("marcflavour"),
64
            "And the marcflavour is the default");
65
66
        like($metadata->metadata, qr/$title/,
67
            "And the record itself contains the correct title");
68
    };
69
70
71
    subtest "Delete", sub {
72
73
        ok(! C4::Biblio::DelBiblio($biblionumber),  #return value here doesnt matter. Should throw exceptions instead.
74
            "When I delete the Biblio");
75
76
        ok(! Koha::Biblios->find({biblionumber => $biblionumber}),
77
            "Then I get nothing when I fetch it from DB");
78
79
        $deletedKohaBiblio = Koha::Deletedbiblios->find({biblionumber => $biblionumber});
80
        ok($deletedKohaBiblio,
81
            "But I find it in the deletedbiblios");
82
83
        is($deletedKohaBiblio->title, $kohaBiblio->title,
84
            "And the Biblio looks just like before!");
85
86
        ok(! Koha::Biblio::Metadatas->find({biblioitemnumber => $biblioitemnumber}),
87
            "When I fetch the Metadata record, I get nothing");
88
89
        $deletedMetadata = Koha::Biblio::DeletedMetadatas->find({biblioitemnumber => $biblioitemnumber});
90
        ok($deletedMetadata,
91
            "But I found it in the deletedbiblio_metadata");
92
93
        is_deeply($deletedMetadata->_result->{_column_data}, $metadata->_result->{_column_data},
94
            "And the Metadata looks just like before!");
95
    };
96
97
    };
98
    ok(0, $@) if $@;
99
}
100
101
102
103
104
105
done_testing;
106

Return to bug 18265