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