Bug 24584: Rewrite optional/sample_itemtypes to YAML
[koha.git] / installer / data / mysql / patroncards_upgrade.pl
blobfde702e1f423a9f44a5131536c5849277ead9e08
1 #!/usr/bin/perl
3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use C4::Context;
22 my $sth = C4::Context->dbh;
24 # NOTE: As long as we die on error *before* the DROP TABLE instructions are executed, the script may simply be rerun after addressing whatever errors occur; If we get past the data conversion without error, the DROPs and ALTERs could be executed manually if need be.
26 # Turn off key checks for duration of script...
27 $sth->do("
28 SET UNIQUE_CHECKS = 0;
29 ") or die "DB ERROR: " . $sth->errstr . "\n";
31 $sth->do("
32 SET FOREIGN_KEY_CHECKS = 0;
33 ") or die "DB ERROR: " . $sth->errstr . "\n";
35 # Create new tables with temporary names...
36 $sth->do("
37 DROP TABLE IF EXISTS creator_batches_tmp;");
38 $sth->do("
39 CREATE TABLE `creator_batches_tmp` (
40 `label_id` int(11) NOT NULL AUTO_INCREMENT,
41 `batch_id` int(10) NOT NULL DEFAULT '1',
42 `item_number` int(11) DEFAULT NULL,
43 `borrower_number` int(11) DEFAULT NULL,
44 `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
45 `branch_code` varchar(10) NOT NULL DEFAULT 'NB',
46 `creator` char(15) NOT NULL DEFAULT 'Labels',
47 PRIMARY KEY (`label_id`),
48 KEY `branch_fk_constraint` (`branch_code`),
49 KEY `item_fk_constraint` (`item_number`),
50 KEY `borrower_fk_constraint` (`borrower_number`),
51 FOREIGN KEY (`borrower_number`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
52 FOREIGN KEY (`branch_code`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE,
53 FOREIGN KEY (`item_number`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE
54 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
55 ") or die "DB ERROR: " . $sth->errstr . "\n";
57 $sth->do("
58 DROP TABLE IF EXISTS creator_layouts_tmp;");
59 $sth->do("
60 CREATE TABLE `creator_layouts_tmp` (
61 `layout_id` int(4) NOT NULL AUTO_INCREMENT,
62 `barcode_type` char(100) NOT NULL DEFAULT 'CODE39',
63 `start_label` int(2) NOT NULL DEFAULT '1',
64 `printing_type` char(32) NOT NULL DEFAULT 'BAR',
65 `layout_name` char(20) NOT NULL DEFAULT 'DEFAULT',
66 `guidebox` int(1) DEFAULT '0',
67 `font` char(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'TR',
68 `font_size` int(4) NOT NULL DEFAULT '10',
69 `units` char(20) NOT NULL DEFAULT 'POINT',
70 `callnum_split` int(1) DEFAULT '0',
71 `text_justify` char(1) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'L',
72 `format_string` varchar(210) NOT NULL DEFAULT 'barcode',
73 `layout_xml` text NOT NULL,
74 `creator` char(15) NOT NULL DEFAULT 'Labels',
75 PRIMARY KEY (`layout_id`)
76 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
77 ") or die "DB ERROR: " . $sth->errstr . "\n";
79 $sth->do("
80 DROP TABLE IF EXISTS creator_templates_tmp;");
81 $sth->do("
82 CREATE TABLE `creator_templates_tmp` (
83 `template_id` int(4) NOT NULL AUTO_INCREMENT,
84 `profile_id` int(4) DEFAULT NULL,
85 `template_code` char(100) NOT NULL DEFAULT 'DEFAULT TEMPLATE',
86 `template_desc` char(100) NOT NULL DEFAULT 'Default description',
87 `page_width` float NOT NULL DEFAULT '0',
88 `page_height` float NOT NULL DEFAULT '0',
89 `label_width` float NOT NULL DEFAULT '0',
90 `label_height` float NOT NULL DEFAULT '0',
91 `top_text_margin` float NOT NULL DEFAULT '0',
92 `left_text_margin` float NOT NULL DEFAULT '0',
93 `top_margin` float NOT NULL DEFAULT '0',
94 `left_margin` float NOT NULL DEFAULT '0',
95 `cols` int(2) NOT NULL DEFAULT '0',
96 `rows` int(2) NOT NULL DEFAULT '0',
97 `col_gap` float NOT NULL DEFAULT '0',
98 `row_gap` float NOT NULL DEFAULT '0',
99 `units` char(20) NOT NULL DEFAULT 'POINT',
100 `creator` char(15) NOT NULL DEFAULT 'Labels',
101 PRIMARY KEY (`template_id`),
102 KEY `template_profile_fk_constraint` (`profile_id`)
103 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
104 ") or die "DB ERROR: " . $sth->errstr . "\n";
106 $sth->do("
107 DROP TABLE IF EXISTS `creator_images`;");
108 $sth->do("
109 CREATE TABLE `creator_images` (
110 `image_id` int(4) NOT NULL AUTO_INCREMENT,
111 `imagefile` mediumblob,
112 `image_name` char(20) NOT NULL DEFAULT 'DEFAULT',
113 PRIMARY KEY (`image_id`),
114 UNIQUE KEY `image_name_index` (`image_name`)
115 ) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8;
116 ") or die "DB ERROR: " . $sth->errstr . "\n";
118 $sth->do("
119 ALTER TABLE printers_profile ADD COLUMN `creator` char(15) NOT NULL DEFAULT 'Labels';
120 ") or die "DB ERROR: " . $sth->errstr . "\n";
122 $sth->do("
123 ALTER TABLE printers_profile DROP KEY printername;
124 ") or die "DB ERROR: " . $sth->errstr . "\n";
126 $sth->do("
127 ALTER TABLE printers_profile ADD UNIQUE KEY `printername` (`printer_name`,`template_id`,`paper_bin`,`creator`);
128 ") or die "DB ERROR: " . $sth->errstr . "\n";
130 # Migrate data from existing tables to new tables...
132 $sth->do("INSERT INTO `creator_batches_tmp` (label_id, batch_id, item_number, timestamp, branch_code) SELECT label_id, batch_id, item_number, timestamp, branch_code FROM labels_batches;") or die "DB ERROR: " . $sth->errstr . "\n";
134 $sth->do("INSERT INTO `creator_layouts_tmp` (layout_id, barcode_type, printing_type, layout_name, guidebox, callnum_split, text_justify, format_string) SELECT layout_id, barcode_type, printing_type, layout_name, guidebox, callnum_split, text_justify, format_string FROM labels_layouts;") or die "DB ERROR: " . $sth->errstr . "\n";
136 $sth->do("INSERT INTO `creator_templates_tmp` (template_id, template_code, template_desc, page_width, page_height, label_width, label_height, top_margin, left_margin, cols, `rows`, col_gap, row_gap, units) SELECT template_id, template_code, template_desc, page_width, page_height, label_width, label_height, top_margin, left_margin, cols, `rows`, col_gap, row_gap, units FROM labels_templates;") or die "DB ERROR: " . $sth->errstr . "\n";
138 # Drop old tables....
140 $sth->do("DROP TABLE IF EXISTS labels_batches;") or die "DB ERROR: " . $sth->errstr . "\n";
141 $sth->do("DROP TABLE IF EXISTS labels_layouts;") or die "DB ERROR: " . $sth->errstr . "\n";
142 $sth->do("DROP TABLE IF EXISTS labels_templates;") or die "DB ERROR: " . $sth->errstr . "\n";
144 # Rename temporary tables to permenant names...
146 $sth->do("ALTER TABLE creator_batches_tmp RENAME TO creator_batches;") or die "DB ERROR: " . $sth->errstr . "\n";
147 $sth->do("ALTER TABLE creator_layouts_tmp RENAME TO creator_layouts;") or die "DB ERROR: " . $sth->errstr . "\n";
148 $sth->do("ALTER TABLE creator_templates_tmp RENAME TO creator_templates;") or die "DB ERROR: " . $sth->errstr . "\n";
150 # Re-enable key checks...
151 $sth->do("
152 SET UNIQUE_CHECKS = 1;
153 ") or die "DB ERROR: " . $sth->errstr . "\n";
155 $sth->do("
156 SET FOREIGN_KEY_CHECKS = 1;
157 ") or die "DB ERROR: " . $sth->errstr . "\n";