Bug 32030: Rename Package|Resource|Title
[koha.git] / installer / data / mysql / atomicupdate / erm.pl
1 use Modern::Perl;
2
3 return {
4     bug_number => "BUG_NUMBER",
5     description => "Some tables for ERM",
6     up => sub {
7         my ($args) = @_;
8         my ($dbh, $out) = @$args{qw(dbh out)};
9
10         $dbh->do(q{
11             INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type)
12             VALUES ('ERMModule', '0', NULL, 'Enable the E-Resource management module', 'YesNo');
13         });
14
15         $dbh->do(q{
16             INSERT IGNORE INTO userflags (bit, flag, flagdesc, defaulton)
17             VALUES (28, 'erm', 'Manage electronic resources', 0)
18         });
19
20         unless ( TableExists('erm_agreements') ) {
21             $dbh->do(q{
22                 CREATE TABLE `erm_agreements` (
23                     `agreement_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
24                     `vendor_id` INT(11) DEFAULT NULL COMMENT 'foreign key to aqbooksellers',
25                     `name` VARCHAR(255) NOT NULL COMMENT 'name of the agreement',
26                     `description` LONGTEXT DEFAULT NULL COMMENT 'description of the agreement',
27                     `status` VARCHAR(80) NOT NULL COMMENT 'current status of the agreement',
28                     `closure_reason` VARCHAR(80) DEFAULT NULL COMMENT 'reason of the closure',
29                     `is_perpetual` TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'is the agreement perpetual',
30                     `renewal_priority` VARCHAR(80) DEFAULT NULL COMMENT 'priority of the renewal',
31                     `license_info` VARCHAR(80) DEFAULT NULL COMMENT 'info about the license',
32                     CONSTRAINT `erm_agreements_ibfk_1` FOREIGN KEY (`vendor_id`) REFERENCES `aqbooksellers` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
33                     PRIMARY KEY(`agreement_id`)
34                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
35             });
36         }
37
38         $dbh->do(q{
39             INSERT IGNORE INTO authorised_value_categories (category_name, is_system)
40             VALUES
41                 ('ERM_AGREEMENT_STATUS', 1),
42                 ('ERM_AGREEMENT_CLOSURE_REASON', 1),
43                 ('ERM_AGREEMENT_RENEWAL_PRIORITY', 1)
44             });
45         $dbh->do(q{
46             INSERT IGNORE INTO authorised_values (category, authorised_value, lib)
47             VALUES
48                 ('ERM_AGREEMENT_STATUS', 'active', 'Active'),
49                 ('ERM_AGREEMENT_STATUS', 'in_negotiation', 'In negotiation'),
50                 ('ERM_AGREEMENT_STATUS', 'closed', 'Closed'),
51                 ('ERM_AGREEMENT_CLOSURE_REASON', 'expired', 'Expired'),
52                 ('ERM_AGREEMENT_CLOSURE_REASON', 'cancelled', 'Cancelled'),
53                 ('ERM_AGREEMENT_RENEWAL_PRIORITY', 'for_review', 'For review'),
54                 ('ERM_AGREEMENT_RENEWAL_PRIORITY', 'renew', 'Renew'),
55                 ('ERM_AGREEMENT_RENEWAL_PRIORITY', 'cancel', 'Cancel')
56         });
57
58         unless ( TableExists('erm_agreement_periods') ) {
59             $dbh->do(q{
60                 CREATE TABLE `erm_agreement_periods` (
61                     `agreement_period_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
62                     `agreement_id` INT(11) NOT NULL COMMENT 'link to the agreement',
63                     `started_on` DATE NOT NULL COMMENT 'start of the agreement period',
64                     `ended_on` DATE COMMENT 'end of the agreement period',
65                     `cancellation_deadline` DATE DEFAULT NULL COMMENT 'Deadline for the cancellation',
66                     `notes` mediumtext DEFAULT NULL COMMENT 'notes about this period',
67                     CONSTRAINT `erm_agreement_periods_ibfk_1` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
68                     PRIMARY KEY(`agreement_period_id`)
69                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
70             });
71         }
72
73         unless ( TableExists('erm_agreement_user_roles') ) {
74             $dbh->do(q{
75                 CREATE TABLE `erm_agreement_user_roles` (
76                     `agreement_id` INT(11) NOT NULL COMMENT 'link to the agreement',
77                     `user_id` INT(11) NOT NULL COMMENT 'link to the user',
78                     `role` VARCHAR(80) NOT NULL COMMENT 'role of the user',
79                     CONSTRAINT `erm_agreement_users_ibfk_1` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
80                     CONSTRAINT `erm_agreement_users_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE
81                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
82             });
83         }
84         $dbh->do(q{
85             INSERT IGNORE INTO authorised_value_categories (category_name, is_system)
86             VALUES
87                 ('ERM_AGREEMENT_USER_ROLES', 1)
88         });
89         $dbh->do(q{
90             INSERT IGNORE INTO authorised_values (category, authorised_value, lib)
91             VALUES
92                 ('ERM_AGREEMENT_USER_ROLES', 'librarian', 'ERM librarian'),
93                 ('ERM_AGREEMENT_USER_ROLES', 'subject_specialist', 'Subject specialist')
94         });
95
96         unless ( TableExists('erm_licenses') ) {
97             $dbh->do(q{
98                 CREATE TABLE `erm_licenses` (
99                     `license_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
100                     `name` VARCHAR(255) NOT NULL COMMENT 'name of the license',
101                     `description` LONGTEXT DEFAULT NULL COMMENT 'description of the license',
102                     `type` VARCHAR(80) NOT NULL COMMENT 'type of the license',
103                     `status` VARCHAR(80) NOT NULL COMMENT 'current status of the license',
104                     `started_on` DATE COMMENT 'start of the license',
105                     `ended_on` DATE COMMENT 'end of the license',
106                     PRIMARY KEY(`license_id`)
107                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
108             });
109         }
110         unless ( TableExists('erm_agreement_licenses') ) {
111             $dbh->do(q{
112                 CREATE TABLE `erm_agreement_licenses` (
113                     `agreement_license_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
114                     `agreement_id` INT(11) NOT NULL COMMENT 'link to the agreement',
115                     `license_id` INT(11) NOT NULL COMMENT 'link to the license',
116                     `status` VARCHAR(80) NOT NULL COMMENT 'current status of the license',
117                     `physical_location` VARCHAR(80) DEFAULT NULL COMMENT 'physical location of the license',
118                     `notes` mediumtext DEFAULT NULL COMMENT 'notes about this license',
119                     `uri` varchar(255) DEFAULT NULL COMMENT 'URI of the license',
120                     CONSTRAINT `erm_licenses_ibfk_1` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
121                     CONSTRAINT `erm_licenses_ibfk_2` FOREIGN KEY (`license_id`) REFERENCES `erm_licenses` (`license_id`) ON DELETE CASCADE ON UPDATE CASCADE,
122                     PRIMARY KEY(`agreement_license_id`)
123                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
124             });
125         }
126         $dbh->do(q{
127             INSERT IGNORE INTO authorised_value_categories (category_name, is_system)
128             VALUES
129                 ('ERM_LICENSE_TYPE', 1),
130                 ('ERM_LICENSE_STATUS', 1),
131                 ('ERM_AGREEMENT_LICENSE_STATUS', 1),
132                 ('ERM_AGREEMENT_LICENSE_LOCATION', 1);
133         });
134
135         $dbh->do(q{
136             INSERT IGNORE INTO authorised_values (category, authorised_value, lib)
137             VALUES
138                 ('ERM_LICENSE_TYPE', 'local', 'Local'),
139                 ('ERM_LICENSE_TYPE', 'consortial', 'Consortial'),
140                 ('ERM_LICENSE_TYPE', 'national', 'National'),
141                 ('ERM_LICENSE_TYPE', 'alliance', 'Alliance'),
142                 ('ERM_LICENSE_STATUS', 'in_negotiation', 'In negotiation'),
143                 ('ERM_LICENSE_STATUS', 'not_yet_active', 'Not yet active'),
144                 ('ERM_LICENSE_STATUS', 'active', 'Active'),
145                 ('ERM_LICENSE_STATUS', 'rejected', 'Rejected'),
146                 ('ERM_LICENSE_STATUS', 'expired', 'Expired'),
147                 ('ERM_AGREEMENT_LICENSE_STATUS', 'controlling', 'Controlling'),
148                 ('ERM_AGREEMENT_LICENSE_STATUS', 'future', 'Future'),
149                 ('ERM_AGREEMENT_LICENSE_STATUS', 'history', 'Historic'),
150                 ('ERM_AGREEMENT_LICENSE_LOCATION', 'filing_cabinet', 'Filing cabinet'),
151                 ('ERM_AGREEMENT_LICENSE_LOCATION', 'cupboard', 'Cupboard');
152         });
153
154         unless ( TableExists('erm_agreement_relationships') ) {
155             $dbh->do(q{
156                 CREATE TABLE `erm_agreement_relationships` (
157                     `agreement_id` INT(11) NOT NULL COMMENT 'link to the agreement',
158                     `related_agreement_id` INT(11) NOT NULL COMMENT 'link to the related agreement',
159                     `relationship` ENUM('supersedes', 'is-superseded-by', 'provides_post-cancellation_access_for', 'has-post-cancellation-access-in', 'tracks_demand-driven_acquisitions_for', 'has-demand-driven-acquisitions-in', 'has_backfile_in', 'has_frontfile_in', 'related_to') NOT NULL COMMENT 'relationship between the two agreements',
160                     `notes` mediumtext DEFAULT NULL COMMENT 'notes about this relationship',
161                     CONSTRAINT `erm_agreement_relationships_ibfk_1` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
162                     CONSTRAINT `erm_agreement_relationships_ibfk_2` FOREIGN KEY (`related_agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
163                     PRIMARY KEY(`agreement_id`, `related_agreement_id`)
164                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
165             });
166         }
167
168         unless ( TableExists('erm_agreement_documents') ) {
169             $dbh->do(q{
170                 CREATE TABLE `erm_agreement_documents` (
171                     `document_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
172                     `agreement_id` INT(11) NOT NULL COMMENT 'link to the agreement',
173                     `file_name` varchar(255) DEFAULT NULL COMMENT 'name of the file',
174                     `file_type` varchar(255) DEFAULT NULL COMMENT 'type of the file',
175                     `file_description` varchar(255) DEFAULT NULL COMMENT 'description of the file',
176                     `file_content` longblob DEFAULT NULL COMMENT 'the content of the file',
177                     `uploaded_on` datetime DEFAULT NULL COMMENT 'datetime when the file as attached',
178                     `physical_location` VARCHAR(255) DEFAULT NULL COMMENT 'physical location of the document',
179                     `uri` varchar(255) DEFAULT NULL COMMENT 'URI of the document',
180                     `notes` mediumtext DEFAULT NULL COMMENT 'notes about this relationship',
181                     CONSTRAINT `erm_agreement_documents_ibfk_1` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
182                     PRIMARY KEY(`document_id`)
183                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
184             });
185         }
186
187         unless ( TableExists('erm_eholdings_packages') ) {
188             $dbh->do(q{
189                 CREATE TABLE `erm_eholdings_packages` (
190                     `package_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
191                     `vendor_id` INT(11) DEFAULT NULL COMMENT 'foreign key to aqbooksellers',
192                     `name` VARCHAR(255) NOT NULL COMMENT 'name of the package',
193                     `external_package_id` VARCHAR(255) DEFAULT NULL COMMENT 'External key',
194                     `package_type` VARCHAR(80) DEFAULT NULL COMMENT 'type of the package',
195                     `content_type` VARCHAR(80) DEFAULT NULL COMMENT 'type of the package',
196                     `created_on` timestamp NOT NULL DEFAULT current_timestamp() COMMENT 'date of creation of the package',
197                     CONSTRAINT `erm_packages_ibfk_1` FOREIGN KEY (`vendor_id`) REFERENCES `aqbooksellers` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
198                     PRIMARY KEY(`package_id`)
199                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
200             });
201         }
202
203         unless ( TableExists('erm_eholdings_packages_agreements') ) {
204             $dbh->do(q{
205                 CREATE TABLE `erm_eholdings_packages_agreements` (
206                     `package_id` INT(11) NOT NULL COMMENT 'link to the package',
207                     `agreement_id` INT(11) NOT NULL COMMENT 'link to the agreement',
208                     CONSTRAINT `erm_eholdings_packages_agreements_ibfk_1` FOREIGN KEY (`package_id`) REFERENCES `erm_eholdings_packages` (`package_id`) ON DELETE CASCADE ON UPDATE CASCADE,
209                     CONSTRAINT `erm_eholdings_packages_agreements_ibfk_2` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE
210                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
211             });
212         }
213
214         unless ( TableExists('erm_eholdings_titles') ) {
215             $dbh->do(q{
216                 CREATE TABLE `erm_eholdings_titles` (
217                     `title_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
218                     `vendor_id` INT(11) DEFAULT NULL,
219                     `publication_title` VARCHAR(255) DEFAULT NULL,
220                     `print_identifier` VARCHAR(255) DEFAULT NULL,
221                     `online_identifier` VARCHAR(255) DEFAULT NULL,
222                     `date_first_issue_online` VARCHAR(255) DEFAULT NULL,
223                     `num_first_vol_online` VARCHAR(255) DEFAULT NULL,
224                     `num_first_issue_online` VARCHAR(255) DEFAULT NULL,
225                     `date_last_issue_online` VARCHAR(255) DEFAULT NULL,
226                     `num_last_vol_online` VARCHAR(255) DEFAULT NULL,
227                     `num_last_issue_online` VARCHAR(255) DEFAULT NULL,
228                     `title_url` VARCHAR(255) DEFAULT NULL,
229                     `first_author` VARCHAR(255) DEFAULT NULL,
230                     `embargo_info` VARCHAR(255) DEFAULT NULL,
231                     `coverage_depth` VARCHAR(255) DEFAULT NULL,
232                     `notes` VARCHAR(255) DEFAULT NULL,
233                     `publisher_name` VARCHAR(255) DEFAULT NULL,
234                     `publication_type` VARCHAR(255) DEFAULT NULL,
235                     `date_monograph_published_print` VARCHAR(255) DEFAULT NULL,
236                     `date_monograph_published_online` VARCHAR(255) DEFAULT NULL,
237                     `monograph_volume` VARCHAR(255) DEFAULT NULL,
238                     `monograph_edition` VARCHAR(255) DEFAULT NULL,
239                     `first_editor` VARCHAR(255) DEFAULT NULL,
240                     `parent_publication_title_id` VARCHAR(255) DEFAULT NULL,
241                     `preceeding_publication_title_id` VARCHAR(255) DEFAULT NULL,
242                     `access_type` VARCHAR(255) DEFAULT NULL,
243                     CONSTRAINT `erm_eholdings_titles_ibfk_1` FOREIGN KEY (`vendor_id`) REFERENCES `aqbooksellers` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
244                     PRIMARY KEY(`title_id`)
245                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
246             });
247         }
248         unless ( TableExists('erm_eholdings_resources') ) {
249             $dbh->do(q{
250                 CREATE TABLE `erm_eholdings_resources` (
251                     `resource_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
252                     `title_id` INT(11) NOT NULL,
253                     `package_id` INT(11) NOT NULL,
254                     `started_on` DATE,
255                     `ended_on` DATE,
256                     `proxy` VARCHAR(80) DEFAULT NULL,
257                     CONSTRAINT `erm_eholdings_resources_ibfk_1` FOREIGN KEY (`title_id`) REFERENCES `erm_eholdings_titles` (`title_id`) ON DELETE CASCADE ON UPDATE CASCADE,
258                     CONSTRAINT `erm_eholdings_resources_ibfk_2` FOREIGN KEY (`package_id`) REFERENCES `erm_eholdings_packages` (`package_id`) ON DELETE CASCADE ON UPDATE CASCADE,
259                     PRIMARY KEY(`resource_id`)
260                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
261             });
262         }
263
264         unless ( column_exists('aqbooksellers', 'external_id') ) {
265             $dbh->do(q{
266                 ALTER TABLE `aqbooksellers`
267                 ADD COLUMN `external_id` VARCHAR(255) DEFAULT NULL
268                 AFTER `deliverytime`
269             });
270         }
271
272         $dbh->do(q{
273             INSERT IGNORE INTO authorised_value_categories (category_name, is_system)
274             VALUES
275                 ('ERM_PACKAGE_TYPE', 1),
276                 ('ERM_PACKAGE_CONTENT_TYPE', 1)
277         });
278
279         $dbh->do(q{
280             INSERT IGNORE INTO authorised_values (category, authorised_value, lib)
281             VALUES
282                 ('ERM_PACKAGE_TYPE', 'local', 'Local'),
283                 ('ERM_PACKAGE_TYPE', 'complete', 'Complete'),
284                 ('ERM_PACKAGE_CONTENT_TYPE', 'mixed_content', 'Aggregated full'),
285                 ('ERM_PACKAGE_CONTENT_TYPE', 'mixed_content', 'Abstract and index'),
286                 ('ERM_PACKAGE_CONTENT_TYPE', 'e_book', 'E-book'),
287                 ('ERM_PACKAGE_CONTENT_TYPE', 'mixed_content', 'Mixed content'),
288                 ('ERM_PACKAGE_CONTENT_TYPE', 'e_journal', 'E-journal'),
289                 ('ERM_PACKAGE_CONTENT_TYPE', 'online_reference', 'Online reference'),
290                 ('ERM_PACKAGE_CONTENT_TYPE', 'print', 'Print'),
291                 ('ERM_PACKAGE_CONTENT_TYPE', 'streaming_media', 'Streaming media'),
292                 ('ERM_PACKAGE_CONTENT_TYPE', 'unknown', 'Unknown')
293         });
294     }
295 };