Bug 16258: Add a syspref to govern EDIFACT functionality
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use C4::Context;
23
24 my $sth = C4::Context->dbh;
25
26 # 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.
27
28 # Turn off key checks for duration of script...
29 $sth->do("
30     SET UNIQUE_CHECKS = 0;
31 ") or die "DB ERROR: " . $sth->errstr . "\n";
32
33 $sth->do("
34     SET FOREIGN_KEY_CHECKS = 0;
35 ") or die "DB ERROR: " . $sth->errstr . "\n";
36
37 # Create new tables with temporary names...
38 $sth->do("
39     DROP TABLE IF EXISTS creator_batches_tmp;");
40 $sth->do("
41     CREATE TABLE `creator_batches_tmp` (
42       `label_id` int(11) NOT NULL AUTO_INCREMENT,
43       `batch_id` int(10) NOT NULL DEFAULT '1',
44       `item_number` int(11) DEFAULT NULL,
45       `borrower_number` int(11) DEFAULT NULL,
46       `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
47       `branch_code` varchar(10) NOT NULL DEFAULT 'NB',
48       `creator` char(15) NOT NULL DEFAULT 'Labels',
49       PRIMARY KEY (`label_id`),
50       KEY `branch_fk_constraint` (`branch_code`),
51       KEY `item_fk_constraint` (`item_number`),
52       KEY `borrower_fk_constraint` (`borrower_number`),
53       FOREIGN KEY (`borrower_number`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE,
54       FOREIGN KEY (`branch_code`) REFERENCES `branches` (`branchcode`) ON DELETE CASCADE,
55       FOREIGN KEY (`item_number`) REFERENCES `items` (`itemnumber`) ON DELETE CASCADE
56     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
57 ") or die "DB ERROR: " . $sth->errstr . "\n";
58
59 $sth->do("
60     DROP TABLE IF EXISTS creator_layouts_tmp;");
61 $sth->do("
62     CREATE TABLE `creator_layouts_tmp` (
63       `layout_id` int(4) NOT NULL AUTO_INCREMENT,
64       `barcode_type` char(100) NOT NULL DEFAULT 'CODE39',
65       `start_label` int(2) NOT NULL DEFAULT '1',
66       `printing_type` char(32) NOT NULL DEFAULT 'BAR',
67       `layout_name` char(20) NOT NULL DEFAULT 'DEFAULT',
68       `guidebox` int(1) DEFAULT '0',
69       `font` char(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'TR',
70       `font_size` int(4) NOT NULL DEFAULT '10',
71       `units` char(20) NOT NULL DEFAULT 'POINT',
72       `callnum_split` int(1) DEFAULT '0',
73       `text_justify` char(1) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'L',
74       `format_string` varchar(210) NOT NULL DEFAULT 'barcode',
75       `layout_xml` text NOT NULL,
76       `creator` char(15) NOT NULL DEFAULT 'Labels',
77       PRIMARY KEY (`layout_id`)
78     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
79 ") or die "DB ERROR: " . $sth->errstr . "\n";
80
81 $sth->do("
82     DROP TABLE IF EXISTS creator_templates_tmp;");
83 $sth->do("
84     CREATE TABLE `creator_templates_tmp` (
85       `template_id` int(4) NOT NULL AUTO_INCREMENT,
86       `profile_id` int(4) DEFAULT NULL,
87       `template_code` char(100) NOT NULL DEFAULT 'DEFAULT TEMPLATE',
88       `template_desc` char(100) NOT NULL DEFAULT 'Default description',
89       `page_width` float NOT NULL DEFAULT '0',
90       `page_height` float NOT NULL DEFAULT '0',
91       `label_width` float NOT NULL DEFAULT '0',
92       `label_height` float NOT NULL DEFAULT '0',
93       `top_text_margin` float NOT NULL DEFAULT '0',
94       `left_text_margin` float NOT NULL DEFAULT '0',
95       `top_margin` float NOT NULL DEFAULT '0',
96       `left_margin` float NOT NULL DEFAULT '0',
97       `cols` int(2) NOT NULL DEFAULT '0',
98       `rows` int(2) NOT NULL DEFAULT '0',
99       `col_gap` float NOT NULL DEFAULT '0',
100       `row_gap` float NOT NULL DEFAULT '0',
101       `units` char(20) NOT NULL DEFAULT 'POINT',
102       `creator` char(15) NOT NULL DEFAULT 'Labels',
103       PRIMARY KEY (`template_id`),
104       KEY `template_profile_fk_constraint` (`profile_id`)
105     ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
106 ") or die "DB ERROR: " . $sth->errstr . "\n";
107
108 $sth->do("
109     DROP TABLE IF EXISTS `creator_images`;");
110 $sth->do("
111 CREATE TABLE `creator_images` (
112       `image_id` int(4) NOT NULL AUTO_INCREMENT,
113       `imagefile` mediumblob,
114       `image_name` char(20) NOT NULL DEFAULT 'DEFAULT',
115       PRIMARY KEY (`image_id`),
116       UNIQUE KEY `image_name_index` (`image_name`)
117     ) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8;
118 ") or die "DB ERROR: " . $sth->errstr . "\n";
119
120 $sth->do("
121     ALTER TABLE printers_profile ADD COLUMN `creator` char(15) NOT NULL DEFAULT 'Labels';
122 ") or die "DB ERROR: " . $sth->errstr . "\n";
123
124 $sth->do("
125     ALTER TABLE printers_profile DROP KEY printername;
126 ") or die "DB ERROR: " . $sth->errstr . "\n";
127
128 $sth->do("
129     ALTER TABLE printers_profile ADD UNIQUE KEY `printername` (`printer_name`,`template_id`,`paper_bin`,`creator`);
130 ") or die "DB ERROR: " . $sth->errstr . "\n";
131
132 # Migrate data from existing tables to new tables...
133
134 $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";
135
136 $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";
137
138 $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";
139
140 # Drop old tables....
141
142 $sth->do("DROP TABLE IF EXISTS labels_batches;") or die "DB ERROR: " . $sth->errstr . "\n";
143 $sth->do("DROP TABLE IF EXISTS labels_layouts;") or die "DB ERROR: " . $sth->errstr . "\n";
144 $sth->do("DROP TABLE IF EXISTS labels_templates;") or die "DB ERROR: " . $sth->errstr . "\n";
145
146 # Rename temporary tables to permenant names...
147
148 $sth->do("ALTER TABLE creator_batches_tmp RENAME TO creator_batches;") or die "DB ERROR: " . $sth->errstr . "\n";
149 $sth->do("ALTER TABLE creator_layouts_tmp RENAME TO creator_layouts;") or die "DB ERROR: " . $sth->errstr . "\n";
150 $sth->do("ALTER TABLE creator_templates_tmp RENAME TO creator_templates;") or die "DB ERROR: " . $sth->errstr . "\n";
151
152 # Re-enable key checks...
153 $sth->do("
154     SET UNIQUE_CHECKS = 1;
155 ") or die "DB ERROR: " . $sth->errstr . "\n";
156
157 $sth->do("
158     SET  FOREIGN_KEY_CHECKS = 1;
159 ") or die "DB ERROR: " . $sth->errstr . "\n";