From 9f88970fddd6f62016e657318bdeeeac06cf5111 Mon Sep 17 00:00:00 2001
From: Ere Maijala <ere.maijala@helsinki.fi>
Date: Wed, 20 Feb 2019 16:56:55 +0200
Subject: [PATCH] Bug 20447: DBIX schema changes
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Rebased-by: Andrew Nugged <nugged@gmail.com>
Rebased-by: Joonas Kylmälä <joonas.kylmala@helsinki.fi>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
---
 Koha/Schema/Result/Biblio.pm           |  19 +-
 Koha/Schema/Result/Branch.pm           |  15 ++
 Koha/Schema/Result/Deleteditem.pm      |  13 +-
 Koha/Schema/Result/Holding.pm          | 262 +++++++++++++++++++++++++
 Koha/Schema/Result/HoldingsMetadata.pm | 140 +++++++++++++
 Koha/Schema/Result/Item.pm             |  34 +++-
 6 files changed, 477 insertions(+), 6 deletions(-)
 create mode 100644 Koha/Schema/Result/Holding.pm
 create mode 100644 Koha/Schema/Result/HoldingsMetadata.pm

diff --git a/Koha/Schema/Result/Biblio.pm b/Koha/Schema/Result/Biblio.pm
index 10e717b369..fa839c62de 100644
--- a/Koha/Schema/Result/Biblio.pm
+++ b/Koha/Schema/Result/Biblio.pm
@@ -315,6 +315,21 @@ __PACKAGE__->has_many(
   { cascade_copy => 0, cascade_delete => 0 },
 );
 
+=head2 holdings
+
+Type: has_many
+
+Related object: L<Koha::Schema::Result::Holding>
+
+=cut
+
+__PACKAGE__->has_many(
+  "holdings",
+  "Koha::Schema::Result::Holding",
+  { "foreign.biblionumber" => "self.biblionumber" },
+  { cascade_copy => 0, cascade_delete => 0 },
+);
+
 =head2 items
 
 Type: has_many
@@ -526,8 +541,8 @@ __PACKAGE__->has_many(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-20 12:00:15
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:troBZEbpX+2RhgH2jptVZQ
+# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-17 09:52:38
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:51iOnRg+TOdClw9ktstMRg
 
 __PACKAGE__->has_many(
   "biblioitem",
diff --git a/Koha/Schema/Result/Branch.pm b/Koha/Schema/Result/Branch.pm
index 9a2b058bcd..11ee5d68b6 100644
--- a/Koha/Schema/Result/Branch.pm
+++ b/Koha/Schema/Result/Branch.pm
@@ -656,6 +656,21 @@ __PACKAGE__->has_many(
   { cascade_copy => 0, cascade_delete => 0 },
 );
 
+=head2 holdings
+
+Type: has_many
+
+Related object: L<Koha::Schema::Result::Holding>
+
+=cut
+
+__PACKAGE__->has_many(
+  "holdings",
+  "Koha::Schema::Result::Holding",
+  { "foreign.holdingbranch" => "self.branchcode" },
+  { cascade_copy => 0, cascade_delete => 0 },
+);
+
 =head2 illrequests
 
 Type: has_many
diff --git a/Koha/Schema/Result/Deleteditem.pm b/Koha/Schema/Result/Deleteditem.pm
index 12c8900c5a..04fef557d9 100644
--- a/Koha/Schema/Result/Deleteditem.pm
+++ b/Koha/Schema/Result/Deleteditem.pm
@@ -370,6 +370,13 @@ inventory number (MARC21 952$i)
 
 Exclude this item from local holds priority
 
+=head2 holding_id
+
+  data_type: 'integer'
+  is_nullable: 1
+
+foreign key from holdings table used to link this item to the right holdings record
+
 =cut
 
 __PACKAGE__->add_columns(
@@ -480,6 +487,8 @@ __PACKAGE__->add_columns(
   { data_type => "varchar", is_nullable => 1, size => 32 },
   "exclude_from_local_holds_priority",
   { data_type => "tinyint", is_nullable => 1 },
+  "holding_id",
+  { data_type => "integer", is_nullable => 1 },
 );
 
 =head1 PRIMARY KEY
@@ -495,8 +504,8 @@ __PACKAGE__->add_columns(
 __PACKAGE__->set_primary_key("itemnumber");
 
 
-# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-01-21 13:39:29
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ytND0t16cwo9dseU+JZSGQ
+# Created by DBIx::Class::Schema::Loader v0.07048 @ 2021-01-24 12:34:57
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:HDZPRz/I5vlIgkw/c44QcA
 
 __PACKAGE__->add_columns(
     '+exclude_from_local_holds_priority' => { is_boolean => 1 },
diff --git a/Koha/Schema/Result/Holding.pm b/Koha/Schema/Result/Holding.pm
new file mode 100644
index 0000000000..aa22f7a14a
--- /dev/null
+++ b/Koha/Schema/Result/Holding.pm
@@ -0,0 +1,262 @@
+use utf8;
+package Koha::Schema::Result::Holding;
+
+# Created by DBIx::Class::Schema::Loader
+# DO NOT MODIFY THE FIRST PART OF THIS FILE
+
+=head1 NAME
+
+Koha::Schema::Result::Holding
+
+=cut
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+=head1 TABLE: C<holdings>
+
+=cut
+
+__PACKAGE__->table("holdings");
+
+=head1 ACCESSORS
+
+=head2 holding_id
+
+  data_type: 'integer'
+  is_auto_increment: 1
+  is_nullable: 0
+
+unique identifier assigned to each holdings record
+
+=head2 biblionumber
+
+  data_type: 'integer'
+  default_value: 0
+  is_foreign_key: 1
+  is_nullable: 0
+
+foreign key from biblio table used to link this record to the right bib record
+
+=head2 frameworkcode
+
+  data_type: 'varchar'
+  default_value: (empty string)
+  is_nullable: 0
+  size: 4
+
+foreign key from the biblio_framework table to identify which framework was used in cataloging this record
+
+=head2 holdingbranch
+
+  data_type: 'varchar'
+  is_foreign_key: 1
+  is_nullable: 1
+  size: 10
+
+foreign key from the branches table for the library that owns this record (MARC21 852$a)
+
+=head2 location
+
+  data_type: 'varchar'
+  is_nullable: 1
+  size: 80
+
+authorized value for the shelving location for this record (MARC21 852$b)
+
+=head2 ccode
+
+  data_type: 'varchar'
+  is_nullable: 1
+  size: 80
+
+authorized value for the collection code associated with this item (MARC21 852$g)
+
+=head2 callnumber
+
+  data_type: 'varchar'
+  is_nullable: 1
+  size: 255
+
+call number (852$h+$i in MARC21)
+
+=head2 suppress
+
+  data_type: 'tinyint'
+  default_value: 0
+  is_nullable: 0
+
+Boolean indicating whether the record is suppressed in OPAC
+
+=head2 timestamp
+
+  data_type: 'timestamp'
+  datetime_undef_if_invalid: 1
+  default_value: current_timestamp
+  is_nullable: 0
+
+date and time this record was last touched
+
+=head2 datecreated
+
+  data_type: 'date'
+  datetime_undef_if_invalid: 1
+  is_nullable: 0
+
+the date this record was added to Koha
+
+=head2 deleted_on
+
+  data_type: 'datetime'
+  datetime_undef_if_invalid: 1
+  is_nullable: 1
+
+the date this record was deleted
+
+=cut
+
+__PACKAGE__->add_columns(
+  "holding_id",
+  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
+  "biblionumber",
+  {
+    data_type      => "integer",
+    default_value  => 0,
+    is_foreign_key => 1,
+    is_nullable    => 0,
+  },
+  "frameworkcode",
+  { data_type => "varchar", default_value => "", is_nullable => 0, size => 4 },
+  "holdingbranch",
+  { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 10 },
+  "location",
+  { data_type => "varchar", is_nullable => 1, size => 80 },
+  "ccode",
+  { data_type => "varchar", is_nullable => 1, size => 80 },
+  "callnumber",
+  { data_type => "varchar", is_nullable => 1, size => 255 },
+  "suppress",
+  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
+  "timestamp",
+  {
+    data_type => "timestamp",
+    datetime_undef_if_invalid => 1,
+    default_value => \"current_timestamp",
+    is_nullable => 0,
+  },
+  "datecreated",
+  { data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 0 },
+  "deleted_on",
+  {
+    data_type => "datetime",
+    datetime_undef_if_invalid => 1,
+    is_nullable => 1,
+  },
+);
+
+=head1 PRIMARY KEY
+
+=over 4
+
+=item * L</holding_id>
+
+=back
+
+=cut
+
+__PACKAGE__->set_primary_key("holding_id");
+
+=head1 RELATIONS
+
+=head2 biblionumber
+
+Type: belongs_to
+
+Related object: L<Koha::Schema::Result::Biblio>
+
+=cut
+
+__PACKAGE__->belongs_to(
+  "biblionumber",
+  "Koha::Schema::Result::Biblio",
+  { biblionumber => "biblionumber" },
+  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
+);
+
+=head2 holdingbranch
+
+Type: belongs_to
+
+Related object: L<Koha::Schema::Result::Branch>
+
+=cut
+
+__PACKAGE__->belongs_to(
+  "holdingbranch",
+  "Koha::Schema::Result::Branch",
+  { branchcode => "holdingbranch" },
+  {
+    is_deferrable => 1,
+    join_type     => "LEFT",
+    on_delete     => "RESTRICT",
+    on_update     => "CASCADE",
+  },
+);
+
+=head2 holdings_metadatas
+
+Type: has_many
+
+Related object: L<Koha::Schema::Result::HoldingsMetadata>
+
+=cut
+
+__PACKAGE__->has_many(
+  "holdings_metadatas",
+  "Koha::Schema::Result::HoldingsMetadata",
+  { "foreign.holding_id" => "self.holding_id" },
+  { cascade_copy => 0, cascade_delete => 0 },
+);
+
+=head2 items
+
+Type: has_many
+
+Related object: L<Koha::Schema::Result::Item>
+
+=cut
+
+__PACKAGE__->has_many(
+  "items",
+  "Koha::Schema::Result::Item",
+  { "foreign.holding_id" => "self.holding_id" },
+  { cascade_copy => 0, cascade_delete => 0 },
+);
+
+
+# Created by DBIx::Class::Schema::Loader v0.07046 @ 2021-03-19 12:18:01
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Q9Pn2fbd7/xNx7heNn/n5Q
+
+=head2 metadata
+
+This relationship makes it possible to use metadata as a prefetch table:
+
+my $holdings = Koha::Holdings->search({}, {prefetch => 'metadata'});
+my $metadata = $holdings->next()->metadata();
+
+=cut
+
+__PACKAGE__->has_one(
+  "metadata",
+  "Koha::Schema::Result::HoldingsMetadata",
+  { "foreign.holding_id" => "self.holding_id" },
+  { cascade_copy => 0, cascade_delete => 0 },
+);
+
+__PACKAGE__->add_columns(
+    '+suppress' => { is_boolean => 1 },
+);
+
+1;
diff --git a/Koha/Schema/Result/HoldingsMetadata.pm b/Koha/Schema/Result/HoldingsMetadata.pm
new file mode 100644
index 0000000000..07446bbfdc
--- /dev/null
+++ b/Koha/Schema/Result/HoldingsMetadata.pm
@@ -0,0 +1,140 @@
+use utf8;
+package Koha::Schema::Result::HoldingsMetadata;
+
+# Created by DBIx::Class::Schema::Loader
+# DO NOT MODIFY THE FIRST PART OF THIS FILE
+
+=head1 NAME
+
+Koha::Schema::Result::HoldingsMetadata
+
+=cut
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Core';
+
+=head1 TABLE: C<holdings_metadata>
+
+=cut
+
+__PACKAGE__->table("holdings_metadata");
+
+=head1 ACCESSORS
+
+=head2 id
+
+  data_type: 'integer'
+  is_auto_increment: 1
+  is_nullable: 0
+
+=head2 holding_id
+
+  data_type: 'integer'
+  is_foreign_key: 1
+  is_nullable: 0
+
+=head2 format
+
+  data_type: 'varchar'
+  is_nullable: 0
+  size: 16
+
+=head2 schema
+
+  data_type: 'varchar'
+  is_nullable: 0
+  size: 16
+
+=head2 metadata
+
+  data_type: 'longtext'
+  is_nullable: 0
+
+=head2 deleted_on
+
+  data_type: 'datetime'
+  datetime_undef_if_invalid: 1
+  is_nullable: 1
+
+the date this record was deleted
+
+=cut
+
+__PACKAGE__->add_columns(
+  "id",
+  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
+  "holding_id",
+  { data_type => "integer", is_foreign_key => 1, is_nullable => 0 },
+  "format",
+  { data_type => "varchar", is_nullable => 0, size => 16 },
+  "schema",
+  { data_type => "varchar", is_nullable => 0, size => 16 },
+  "metadata",
+  { data_type => "longtext", is_nullable => 0 },
+  "deleted_on",
+  {
+    data_type => "datetime",
+    datetime_undef_if_invalid => 1,
+    is_nullable => 1,
+  },
+);
+
+=head1 PRIMARY KEY
+
+=over 4
+
+=item * L</id>
+
+=back
+
+=cut
+
+__PACKAGE__->set_primary_key("id");
+
+=head1 UNIQUE CONSTRAINTS
+
+=head2 C<holdings_metadata_uniq_key>
+
+=over 4
+
+=item * L</holding_id>
+
+=item * L</format>
+
+=item * L</schema>
+
+=back
+
+=cut
+
+__PACKAGE__->add_unique_constraint(
+  "holdings_metadata_uniq_key",
+  ["holding_id", "format", "schema"],
+);
+
+=head1 RELATIONS
+
+=head2 holding
+
+Type: belongs_to
+
+Related object: L<Koha::Schema::Result::Holding>
+
+=cut
+
+__PACKAGE__->belongs_to(
+  "holding",
+  "Koha::Schema::Result::Holding",
+  { holding_id => "holding_id" },
+  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
+);
+
+
+# Created by DBIx::Class::Schema::Loader v0.07048 @ 2021-01-24 12:34:57
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:msXxqOTn5rKZf188+KFk8g
+
+
+# You can replace this text with custom code or comments, and it will be preserved on regeneration
+1;
diff --git a/Koha/Schema/Result/Item.pm b/Koha/Schema/Result/Item.pm
index a53e10e98a..9dcce11904 100644
--- a/Koha/Schema/Result/Item.pm
+++ b/Koha/Schema/Result/Item.pm
@@ -374,6 +374,14 @@ inventory number (MARC21 952$i)
 
 Exclude this item from local holds priority
 
+=head2 holding_id
+
+  data_type: 'integer'
+  is_foreign_key: 1
+  is_nullable: 1
+
+foreign key from holdings table used to link this item to the right holdings record
+
 =cut
 
 __PACKAGE__->add_columns(
@@ -494,6 +502,8 @@ __PACKAGE__->add_columns(
   { data_type => "varchar", is_nullable => 1, size => 32 },
   "exclude_from_local_holds_priority",
   { data_type => "tinyint", is_nullable => 1 },
+  "holding_id",
+  { data_type => "integer", is_foreign_key => 1, is_nullable => 1 },
 );
 
 =head1 PRIMARY KEY
@@ -674,6 +684,26 @@ __PACKAGE__->might_have(
   { cascade_copy => 0, cascade_delete => 0 },
 );
 
+=head2 holding
+
+Type: belongs_to
+
+Related object: L<Koha::Schema::Result::Holding>
+
+=cut
+
+__PACKAGE__->belongs_to(
+  "holding",
+  "Koha::Schema::Result::Holding",
+  { holding_id => "holding_id" },
+  {
+    is_deferrable => 1,
+    join_type     => "LEFT",
+    on_delete     => "CASCADE",
+    on_update     => "CASCADE",
+  },
+);
+
 =head2 holdingbranch
 
 Type: belongs_to
@@ -865,8 +895,8 @@ __PACKAGE__->has_many(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-08-27 08:42:21
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:SjZn3haOtUZWu1jrMigjNQ
+# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-09-17 09:52:38
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:iAfcOSJbb1RxA/PhSdqdwA
 
 __PACKAGE__->belongs_to( biblioitem => "Koha::Schema::Result::Biblioitem", "biblioitemnumber" );
 
-- 
2.30.2