From fc850f913eec185db5a3eaa41cf814866c99b957 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

---
 Koha/Schema/Result/Biblio.pm           |  23 ++-
 Koha/Schema/Result/Branch.pm           |  19 +-
 Koha/Schema/Result/Deleteditem.pm      |  11 +-
 Koha/Schema/Result/Holding.pm          | 234 +++++++++++++++++++++++++
 Koha/Schema/Result/HoldingsMetadata.pm | 138 +++++++++++++++
 Koha/Schema/Result/Item.pm             |  32 +++-
 6 files changed, 447 insertions(+), 10 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 fa3979886f..cf255b107c 100644
--- a/Koha/Schema/Result/Biblio.pm
+++ b/Koha/Schema/Result/Biblio.pm
@@ -53,7 +53,7 @@ __PACKAGE__->table("biblio");
 
 =head2 subtitle
 
-  data_type: 'longtext'
+  data_type: 'text'
   is_nullable: 1
 
 =head2 part_number
@@ -123,7 +123,7 @@ __PACKAGE__->add_columns(
   "medium",
   { data_type => "longtext", is_nullable => 1 },
   "subtitle",
-  { data_type => "longtext", is_nullable => 1 },
+  { data_type => "text", is_nullable => 1 },
   "part_number",
   { data_type => "longtext", is_nullable => 1 },
   "part_name",
@@ -270,6 +270,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
@@ -451,8 +466,8 @@ __PACKAGE__->has_many(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-10-12 09:13:32
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:R4PNOXHa9cRIx/up7HWz4Q
+# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-10-22 10:51:41
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:q41/EGYCrmV/oa8lbuPnnA
 
 
 __PACKAGE__->has_one(
diff --git a/Koha/Schema/Result/Branch.pm b/Koha/Schema/Result/Branch.pm
index 892e0c2448..adfa1d82b3 100644
--- a/Koha/Schema/Result/Branch.pm
+++ b/Koha/Schema/Result/Branch.pm
@@ -578,6 +578,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
@@ -834,8 +849,8 @@ __PACKAGE__->has_many(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-08-23 20:02:36
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:OB75ORKajzt+l0w9mRFX1Q
+# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-10-22 10:51:41
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:HzuYjaXt0aOMAjUqTSY+iQ
 
 __PACKAGE__->add_columns(
     '+pickup_location' => { is_boolean => 1 }
diff --git a/Koha/Schema/Result/Deleteditem.pm b/Koha/Schema/Result/Deleteditem.pm
index fbed06c6ce..fee2f1c70c 100644
--- a/Koha/Schema/Result/Deleteditem.pm
+++ b/Koha/Schema/Result/Deleteditem.pm
@@ -282,6 +282,11 @@ __PACKAGE__->table("deleteditems");
   data_type: 'tinyint'
   is_nullable: 1
 
+=head2 holding_id
+
+  data_type: 'integer'
+  is_nullable: 1
+
 =cut
 
 __PACKAGE__->add_columns(
@@ -392,6 +397,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
@@ -407,8 +414,8 @@ __PACKAGE__->add_columns(
 __PACKAGE__->set_primary_key("itemnumber");
 
 
-# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-09-30 15:31:01
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1l4g7uiQo9gkdrWbNxAsJg
+# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-10-22 10:51:41
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vUBgNm4rWJ2ZyCWplooNBg
 
 __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..0b2ae736a3
--- /dev/null
+++ b/Koha/Schema/Result/Holding.pm
@@ -0,0 +1,234 @@
+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
+
+=head2 biblionumber
+
+  data_type: 'integer'
+  default_value: 0
+  is_foreign_key: 1
+  is_nullable: 0
+
+=head2 frameworkcode
+
+  data_type: 'varchar'
+  default_value: (empty string)
+  is_nullable: 0
+  size: 4
+
+=head2 holdingbranch
+
+  data_type: 'varchar'
+  is_foreign_key: 1
+  is_nullable: 1
+  size: 10
+
+=head2 location
+
+  data_type: 'varchar'
+  is_nullable: 1
+  size: 80
+
+=head2 ccode
+
+  data_type: 'varchar'
+  is_nullable: 1
+  size: 80
+
+=head2 callnumber
+
+  data_type: 'varchar'
+  is_nullable: 1
+  size: 255
+
+=head2 suppress
+
+  data_type: 'tinyint'
+  is_nullable: 1
+
+=head2 timestamp
+
+  data_type: 'timestamp'
+  datetime_undef_if_invalid: 1
+  default_value: current_timestamp
+  is_nullable: 0
+
+=head2 datecreated
+
+  data_type: 'date'
+  datetime_undef_if_invalid: 1
+  is_nullable: 0
+
+=head2 deleted_on
+
+  data_type: 'datetime'
+  datetime_undef_if_invalid: 1
+  is_nullable: 1
+
+=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", is_nullable => 1 },
+  "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.07049 @ 2020-05-15 12:37:06
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:r5dS7lMIAG11s3PYFp/NKQ
+
+=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 },
+);
+1;
diff --git a/Koha/Schema/Result/HoldingsMetadata.pm b/Koha/Schema/Result/HoldingsMetadata.pm
new file mode 100644
index 0000000000..42d7f886e5
--- /dev/null
+++ b/Koha/Schema/Result/HoldingsMetadata.pm
@@ -0,0 +1,138 @@
+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
+
+=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.07049 @ 2019-02-20 16:55:21
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:42bY17ncjs1OZKH23z4SgQ
+
+
+# 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 6300d846c9..5f438793ef 100644
--- a/Koha/Schema/Result/Item.pm
+++ b/Koha/Schema/Result/Item.pm
@@ -286,6 +286,12 @@ __PACKAGE__->table("items");
   data_type: 'tinyint'
   is_nullable: 1
 
+=head2 holding_id
+
+  data_type: 'integer'
+  is_foreign_key: 1
+  is_nullable: 1
+
 =cut
 
 __PACKAGE__->add_columns(
@@ -406,6 +412,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
@@ -586,6 +594,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
@@ -762,8 +790,8 @@ __PACKAGE__->has_many(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-10-12 09:13:32
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:pmAVO6LxCb6wAJvfh+rndw
+# Created by DBIx::Class::Schema::Loader v0.07049 @ 2020-10-22 10:51:41
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:kBn7/FDT6yH5HRwseri6rg
 
 __PACKAGE__->belongs_to( biblioitem => "Koha::Schema::Result::Biblioitem", "biblioitemnumber" );
 
-- 
2.25.1