adding 2 fields to letter system :
[koha.git] / updater / updatedatabase
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Database Updater
6 # This script checks for required updates to the database.
7
8 # Part of the Koha Library Software www.koha.org
9 # Licensed under the GPL.
10
11 # Bugs/ToDo:
12 # - Would also be a good idea to offer to do a backup at this time...
13
14 # NOTE:  If you do something more than once in here, make it table driven.
15 use strict;
16
17 # CPAN modules
18 use DBI;
19 use Getopt::Long;
20 # Koha modules
21 use C4::Context;
22
23 # FIXME - The user might be installing a new database, so can't rely
24 # on /etc/koha.conf anyway.
25
26 my $debug = 0;
27
28 my (
29     $sth, $sti,
30     $query,
31     %existingtables,    # tables already in database
32     %types,
33     $table,
34     $column,
35     $type, $null, $key, $default, $extra,
36     $prefitem,          # preference item in systempreferences table
37 );
38
39 my $silent;
40 GetOptions(
41         's' =>\$silent
42         );
43 my $dbh = C4::Context->dbh;
44 print "connected to your DB. Checking & modifying it\n" unless $silent;
45
46 #-------------------
47 # Defines
48
49 # Tables to add if they don't exist
50 my %requiretables = (
51     categorytable       => "(categorycode char(5) NOT NULL default '',
52                              description text default '',
53                              itemtypecodes text default '',
54                              PRIMARY KEY (categorycode)
55                             )",
56     subcategorytable       => "(subcategorycode char(5) NOT NULL default '',
57                              description text default '',
58                              itemtypecodes text default '',
59                              PRIMARY KEY (subcategorycode)
60                             )",
61     mediatypetable       => "(mediatypecode char(5) NOT NULL default '',
62                              description text default '',
63                              itemtypecodes text default '',
64                              PRIMARY KEY (mediatypecode)
65                             )",
66     action_logs         => "(
67                                     `timestamp` TIMESTAMP NOT NULL ,
68                                     `user` INT( 11 ) NOT NULL ,
69                                     `module` TEXT default '',
70                                     `action` TEXT default '' ,
71                                     `object` INT(11) default '' ,
72                                     `info` TEXT default '' ,
73                                     PRIMARY KEY ( `timestamp` , `user` )
74                             )",
75         letter          => "(
76                                         module varchar(20) NOT NULL default '',
77                                         code varchar(20) NOT NULL default '',
78                                         name varchar(100) NOT NULL default '',
79                                         title varchar(200) NOT NULL default '',
80                                         content text,
81                                         PRIMARY KEY  (module,code)
82                                 )",
83 );
84
85 my %requirefields = (
86 #    tablename        => { 'field' => 'fieldtype' },
87 );
88
89 my %dropable_table = (
90 # tablename => 'tablename',
91 );
92
93 my %uselessfields = (
94 # tablename => "field1,field2",
95         );
96 # the other hash contains other actions that can't be done elsewhere. they are done
97 # either BEFORE of AFTER everything else, depending on "when" entry (default => AFTER)
98
99 # The tabledata hash contains data that should be in the tables.
100 # The uniquefieldrequired hash entry is used to determine which (if any) fields
101 # must not exist in the table for this row to be inserted.  If the
102 # uniquefieldrequired entry is already in the table, the existing data is not
103 # modified, unless the forceupdate hash entry is also set.  Fields in the
104 # anonymous "forceupdate" hash will be forced to be updated to the default
105 # values given in the %tabledata hash.
106
107 my %tabledata = (
108 # tablename => [
109 #       {       uniquefielrequired => 'fieldname', # the primary key in the table
110 #               fieldname => fieldvalue,
111 #               fieldname2 => fieldvalue2,
112 #       },
113 # ],
114     systempreferences => [
115                 {
116             uniquefieldrequired => 'variable',
117             variable            => 'Activate_Log',
118             value               => 'On',
119             forceupdate         => { 'explanation' => 1,
120                                      'type' => 1},
121             explanation         => 'Turn Log Actions on DB On an Off',
122             type                => 'YesNo',
123         },
124                 {
125             uniquefieldrequired => 'variable',
126             variable            => 'ReturnBeforeExpiry',
127             value               => 'Off',
128             forceupdate         => { 'explanation' => 1,
129                                      'type' => 1},
130             explanation         => 'If Yes, Returndate on issuing can\'t be after borrower card expiry',
131             type                => 'YesNo',
132         },
133     ],
134
135 );
136
137 my %fielddefinitions = (
138 # fieldname => [
139 #       {                 field => 'fieldname',
140 #             type    => 'fieldtype',
141 #             null    => '',
142 #             key     => '',
143 #             default => ''
144 #         },
145 #     ],
146 );
147
148 #-------------------
149 # Initialize
150
151 # Start checking
152
153 # Get version of MySQL database engine.
154 my $mysqlversion = `mysqld --version`;
155 $mysqlversion =~ /Ver (\S*) /;
156 $mysqlversion = $1;
157 if ( $mysqlversion ge '3.23' ) {
158     print "Could convert to MyISAM database tables...\n" unless $silent;
159 }
160
161 #---------------------------------
162 # Tables
163
164 # Collect all tables into a list
165 $sth = $dbh->prepare("show tables");
166 $sth->execute;
167 while ( my ($table) = $sth->fetchrow ) {
168     $existingtables{$table} = 1;
169 }
170
171
172 # Now add any missing tables
173 foreach $table ( keys %requiretables ) {
174     unless ( $existingtables{$table} ) {
175         print "Adding $table table...\n" unless $silent;
176         my $sth = $dbh->prepare("create table $table $requiretables{$table}");
177         $sth->execute;
178         if ( $sth->err ) {
179             print "Error : $sth->errstr \n";
180             $sth->finish;
181         }    # if error
182     }    # unless exists
183 }    # foreach
184
185 # now drop useless tables
186 foreach $table ( keys %dropable_table ) {
187         if ( $existingtables{$table} ) {
188                 print "Dropping unused table $table\n" if $debug and not $silent;
189                 $dbh->do("drop table $table");
190                 if ( $dbh->err ) {
191                         print "Error : $dbh->errstr \n";
192                 }
193         }
194 }
195
196 #---------------------------------
197 # Columns
198
199 foreach $table ( keys %requirefields ) {
200     print "Check table $table\n" if $debug and not $silent;
201     $sth = $dbh->prepare("show columns from $table");
202     $sth->execute();
203     undef %types;
204     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
205     {
206         $types{$column} = $type;
207     }    # while
208     foreach $column ( keys %{ $requirefields{$table} } ) {
209         print "  Check column $column  [$types{$column}]\n" if $debug and not $silent;
210         if ( !$types{$column} ) {
211
212             # column doesn't exist
213             print "Adding $column field to $table table...\n" unless $silent;
214             $query = "alter table $table
215                         add column $column " . $requirefields{$table}->{$column};
216             print "Execute: $query\n" if $debug;
217             my $sti = $dbh->prepare($query);
218             $sti->execute;
219             if ( $sti->err ) {
220                 print "**Error : $sti->errstr \n";
221                 $sti->finish;
222             }    # if error
223         }    # if column
224     }    # foreach column
225 }    # foreach table
226
227 foreach $table ( keys %fielddefinitions ) {
228         print "Check table $table\n" if $debug;
229         $sth = $dbh->prepare("show columns from $table");
230         $sth->execute();
231         my $definitions;
232         while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
233         {
234                 $definitions->{$column}->{type}    = $type;
235                 $definitions->{$column}->{null}    = $null;
236                 $definitions->{$column}->{key}     = $key;
237                 $definitions->{$column}->{default} = $default;
238                 $definitions->{$column}->{extra}   = $extra;
239         }    # while
240         my $fieldrow = $fielddefinitions{$table};
241         foreach my $row (@$fieldrow) {
242                 my $field   = $row->{field};
243                 my $type    = $row->{type};
244                 my $null    = $row->{null};
245                 my $key     = $row->{key};
246                 my $default = $row->{default};
247                 $default="''" unless $default;
248                 my $extra   = $row->{extra};
249                 my $def     = $definitions->{$field};
250                 unless ( $type eq $def->{type}
251                         && $null eq $def->{null}
252                         && $key eq $def->{key}
253                         && $default eq $def->{default}
254                         && $extra eq $def->{extra} )
255                 {
256
257                         if ( $null eq '' ) {
258                                 $null = 'NOT NULL';
259                         }
260                         if ( $key eq 'PRI' ) {
261                                 $key = 'PRIMARY KEY';
262                         }
263                         unless ( $extra eq 'auto_increment' ) {
264                                 $extra = '';
265                         }
266                         # if it's a new column use "add", if it's an old one, use "change".
267                         my $action;
268                         if ($definitions->{$field}->{type}) {
269                                 $action="change $field"
270                         } else {
271                                 $action="add";
272                         }
273 # if it's a primary key, drop the previous pk, before altering the table
274                         my $sth;
275                         if ($key ne 'PRIMARY KEY') {
276                                 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ?");
277                         } else {
278                                 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ?");
279                         }
280                         $sth->execute($default);
281                         print "  Alter $field in $table\n" unless $silent;
282                 }
283         }
284 }
285
286
287 # Populate tables with required data
288 foreach my $table ( keys %tabledata ) {
289     print "Checking for data required in table $table...\n" unless $silent;
290     my $tablerows = $tabledata{$table};
291     foreach my $row (@$tablerows) {
292         my $uniquefieldrequired = $row->{uniquefieldrequired};
293         my $uniquevalue         = $row->{$uniquefieldrequired};
294         my $forceupdate         = $row->{forceupdate};
295         my $sth                 =
296           $dbh->prepare(
297 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
298         );
299         $sth->execute($uniquevalue);
300         if ($sth->rows) {
301             foreach my $field (keys %$forceupdate) {
302                 if ($forceupdate->{$field}) {
303                     my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
304                     $sth->execute($row->{$field}, $uniquevalue);
305                 }
306             }
307         } else {
308             print "Adding row to $table: " unless $silent;
309             my @values;
310             my $fieldlist;
311             my $placeholders;
312             foreach my $field ( keys %$row ) {
313                 next if $field eq 'uniquefieldrequired';
314                 next if $field eq 'forceupdate';
315                 my $value = $row->{$field};
316                 push @values, $value;
317                 print "  $field => $value" unless $silent;
318                 $fieldlist .= "$field,";
319                 $placeholders .= "?,";
320             }
321             print "\n" unless $silent;
322             $fieldlist    =~ s/,$//;
323             $placeholders =~ s/,$//;
324             my $sth =
325               $dbh->prepare(
326                 "insert into $table ($fieldlist) values ($placeholders)");
327             $sth->execute(@values);
328         }
329     }
330 }
331
332 # at last, remove useless fields
333 foreach $table ( keys %uselessfields ) {
334         my @fields = split /,/,$uselessfields{$table};
335         my $fields;
336         my $exists;
337         foreach my $fieldtodrop (@fields) {
338                 $fieldtodrop =~ s/\t//g;
339                 $fieldtodrop =~ s/\n//g;
340                 $exists =0;
341                 $sth = $dbh->prepare("show columns from $table");
342                 $sth->execute;
343                 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
344                 {
345                         $exists =1 if ($column eq $fieldtodrop);
346                 }
347                 if ($exists) {
348                         print "deleting $fieldtodrop field in $table...\n" unless $silent;
349                         my $sth = $dbh->prepare("alter table $table drop $fieldtodrop");
350                         $sth->execute;
351                 }
352         }
353 }    # foreach
354
355
356 $sth->finish;
357
358 exit;
359
360 # $Log$
361 # Revision 1.115  2005/08/02 16:15:34  tipaul
362 # adding 2 fields to letter system :
363 # * module (acquisition, catalogue...) : it will be usefull to show the librarian only letters he may be interested by.
364 # * title, that will be used as mail subject.
365 #
366 # Revision 1.114  2005/07/28 15:10:13  tipaul
367 # Introducing new "Letters" system : Letters will be used everytime you want to sent something to someone (through mail or paper). For example, sending a mail for overdues use letter that you can put as parameters. Sending a mail to a borrower when a suggestion is validated uses a letter too.
368 # the letter table contains 3 fields :
369 # * code => the code of the letter
370 # * name => the complete name of the letter
371 # * content => the complete text. It's a TEXT field type, so has no limits.
372 #
373 # My next goal now is to work on point 2-I "serial issue alert"
374 # With this feature, in serials, a user can subscribe the "issue alert". For every issue arrived/missing, a mail is sent to all subscribers of this list. The mail warns the user that the issue is arrive or missing. Will be in head.
375 # (see mail on koha-devel, 2005/04/07)
376 #
377 # The "serial issue alert" will be the 1st to use this letter system that probably needs some tweaking ;-)
378 #
379 # Once it will be stabilised default letters (in any languages) could be added during installer to help the library begin with this new feature.
380 #
381 # Revision 1.113  2005/07/28 08:38:41  tipaul
382 # For instance, the return date does not rely on the borrower expiration date. A systempref will be added in Koha, to modify return date calculation schema :
383 # * ReturnBeforeExpiry = yes => return date can't be after expiry date
384 # * ReturnBeforeExpiry = no  => return date can be after expiry date
385 #
386 # Revision 1.112  2005/07/26 08:19:47  hdl
387 # Adding IndependantBranches System preference variable in order to manage Branch independancy.
388 #
389 # Revision 1.111  2005/07/25 15:35:38  tipaul
390 # we have decided that moving to Koha 3.0 requires being already in Koha 2.2.x
391 # So, the updatedatabase script can highly be cleaned (90% removed).
392 # Let's play with the new Koha DB structure now ;-)
393 #
394 #!/usr/bin/perl
395
396 # $Id$
397
398 # Database Updater
399 # This script checks for required updates to the database.
400
401 # Part of the Koha Library Software www.koha.org
402 # Licensed under the GPL.
403
404 # Bugs/ToDo:
405 # - Would also be a good idea to offer to do a backup at this time...
406
407 # NOTE:  If you do something more than once in here, make it table driven.
408 use strict;
409
410 # CPAN modules
411 use DBI;
412 use Getopt::Long;
413 # Koha modules
414 use C4::Context;
415
416 # FIXME - The user might be installing a new database, so can't rely
417 # on /etc/koha.conf anyway.
418
419 my $debug = 0;
420
421 my (
422     $sth, $sti,
423     $query,
424     %existingtables,    # tables already in database
425     %types,
426     $table,
427     $column,
428     $type, $null, $key, $default, $extra,
429     $prefitem,          # preference item in systempreferences table
430 );
431
432 my $silent;
433 GetOptions(
434         's' =>\$silent
435         );
436 my $dbh = C4::Context->dbh;
437 print "connected to your DB. Checking & modifying it\n" unless $silent;
438
439 #-------------------
440 # Defines
441
442 # Tables to add if they don't exist
443 my %requiretables = (
444     categorytable       => "(categorycode char(5) NOT NULL default '',
445                              description text default '',
446                              itemtypecodes text default '',
447                              PRIMARY KEY (categorycode)
448                             )",
449     subcategorytable       => "(subcategorycode char(5) NOT NULL default '',
450                              description text default '',
451                              itemtypecodes text default '',
452                              PRIMARY KEY (subcategorycode)
453                             )",
454     mediatypetable       => "(mediatypecode char(5) NOT NULL default '',
455                              description text default '',
456                              itemtypecodes text default '',
457                              PRIMARY KEY (mediatypecode)
458                             )",
459     action_logs         => "(
460                                     `timestamp` TIMESTAMP NOT NULL ,
461                                     `user` INT( 11 ) NOT NULL ,
462                                     `module` TEXT default '',
463                                     `action` TEXT default '' ,
464                                     `object` INT(11) default '' ,
465                                     `info` TEXT default '' ,
466                                     PRIMARY KEY ( `timestamp` , `user` )
467                             )",
468 );
469
470 my %requirefields = (
471 #    tablename        => { 'field' => 'fieldtype' },
472 );
473
474 my %dropable_table = (
475 # tablename => 'tablename',
476 );
477
478 my %uselessfields = (
479 # tablename => "field1,field2",
480         );
481 # the other hash contains other actions that can't be done elsewhere. they are done
482 # either BEFORE of AFTER everything else, depending on "when" entry (default => AFTER)
483
484 # The tabledata hash contains data that should be in the tables.
485 # The uniquefieldrequired hash entry is used to determine which (if any) fields
486 # must not exist in the table for this row to be inserted.  If the
487 # uniquefieldrequired entry is already in the table, the existing data is not
488 # modified, unless the forceupdate hash entry is also set.  Fields in the
489 # anonymous "forceupdate" hash will be forced to be updated to the default
490 # values given in the %tabledata hash.
491
492 my %tabledata = (
493 # tablename => [
494 #       {       uniquefielrequired => 'fieldname', # the primary key in the table
495 #               fieldname => fieldvalue,
496 #               fieldname2 => fieldvalue2,
497 #       },
498 # ],
499     systempreferences => [
500                 {
501             uniquefieldrequired => 'variable',
502             variable            => 'Activate_Log',
503             value               => 'On',
504             forceupdate         => { 'explanation' => 1,
505                                      'type' => 1},
506             explanation         => 'Turn Log Actions on DB On an Off',
507             type                => 'YesNo',
508         },
509         {
510             uniquefieldrequired => 'variable',
511             variable            => 'IndependantBranches',
512             value               => 0,
513             forceupdate         => { 'explanation' => 1,
514                                      'type' => 1},
515             explanation         => 'Turn Branch independancy management On an Off',
516             type                => 'YesNo',
517         },
518
519     ],
520
521 );
522
523 my %fielddefinitions = (
524 # fieldname => [
525 #       {                 field => 'fieldname',
526 #             type    => 'fieldtype',
527 #             null    => '',
528 #             key     => '',
529 #             default => ''
530 #         },
531 #     ],
532 );
533
534 #-------------------
535 # Initialize
536
537 # Start checking
538
539 # Get version of MySQL database engine.
540 my $mysqlversion = `mysqld --version`;
541 $mysqlversion =~ /Ver (\S*) /;
542 $mysqlversion = $1;
543 if ( $mysqlversion ge '3.23' ) {
544     print "Could convert to MyISAM database tables...\n" unless $silent;
545 }
546
547 #---------------------------------
548 # Tables
549
550 # Collect all tables into a list
551 $sth = $dbh->prepare("show tables");
552 $sth->execute;
553 while ( my ($table) = $sth->fetchrow ) {
554     $existingtables{$table} = 1;
555 }
556
557
558 # Now add any missing tables
559 foreach $table ( keys %requiretables ) {
560     unless ( $existingtables{$table} ) {
561         print "Adding $table table...\n" unless $silent;
562         my $sth = $dbh->prepare("create table $table $requiretables{$table}");
563         $sth->execute;
564         if ( $sth->err ) {
565             print "Error : $sth->errstr \n";
566             $sth->finish;
567         }    # if error
568     }    # unless exists
569 }    # foreach
570
571 # now drop useless tables
572 foreach $table ( keys %dropable_table ) {
573         if ( $existingtables{$table} ) {
574                 print "Dropping unused table $table\n" if $debug and not $silent;
575                 $dbh->do("drop table $table");
576                 if ( $dbh->err ) {
577                         print "Error : $dbh->errstr \n";
578                 }
579         }
580 }
581
582 #---------------------------------
583 # Columns
584
585 foreach $table ( keys %requirefields ) {
586     print "Check table $table\n" if $debug and not $silent;
587     $sth = $dbh->prepare("show columns from $table");
588     $sth->execute();
589     undef %types;
590     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
591     {
592         $types{$column} = $type;
593     }    # while
594     foreach $column ( keys %{ $requirefields{$table} } ) {
595         print "  Check column $column  [$types{$column}]\n" if $debug and not $silent;
596         if ( !$types{$column} ) {
597
598             # column doesn't exist
599             print "Adding $column field to $table table...\n" unless $silent;
600             $query = "alter table $table
601                         add column $column " . $requirefields{$table}->{$column};
602             print "Execute: $query\n" if $debug;
603             my $sti = $dbh->prepare($query);
604             $sti->execute;
605             if ( $sti->err ) {
606                 print "**Error : $sti->errstr \n";
607                 $sti->finish;
608             }    # if error
609         }    # if column
610     }    # foreach column
611 }    # foreach table
612
613 foreach $table ( keys %fielddefinitions ) {
614         print "Check table $table\n" if $debug;
615         $sth = $dbh->prepare("show columns from $table");
616         $sth->execute();
617         my $definitions;
618         while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
619         {
620                 $definitions->{$column}->{type}    = $type;
621                 $definitions->{$column}->{null}    = $null;
622                 $definitions->{$column}->{key}     = $key;
623                 $definitions->{$column}->{default} = $default;
624                 $definitions->{$column}->{extra}   = $extra;
625         }    # while
626         my $fieldrow = $fielddefinitions{$table};
627         foreach my $row (@$fieldrow) {
628                 my $field   = $row->{field};
629                 my $type    = $row->{type};
630                 my $null    = $row->{null};
631                 my $key     = $row->{key};
632                 my $default = $row->{default};
633                 $default="''" unless $default;
634                 my $extra   = $row->{extra};
635                 my $def     = $definitions->{$field};
636                 unless ( $type eq $def->{type}
637                         && $null eq $def->{null}
638                         && $key eq $def->{key}
639                         && $default eq $def->{default}
640                         && $extra eq $def->{extra} )
641                 {
642
643                         if ( $null eq '' ) {
644                                 $null = 'NOT NULL';
645                         }
646                         if ( $key eq 'PRI' ) {
647                                 $key = 'PRIMARY KEY';
648                         }
649                         unless ( $extra eq 'auto_increment' ) {
650                                 $extra = '';
651                         }
652                         # if it's a new column use "add", if it's an old one, use "change".
653                         my $action;
654                         if ($definitions->{$field}->{type}) {
655                                 $action="change $field"
656                         } else {
657                                 $action="add";
658                         }
659 # if it's a primary key, drop the previous pk, before altering the table
660                         my $sth;
661                         if ($key ne 'PRIMARY KEY') {
662                                 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ?");
663                         } else {
664                                 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ?");
665                         }
666                         $sth->execute($default);
667                         print "  Alter $field in $table\n" unless $silent;
668                 }
669         }
670 }
671
672
673 # Populate tables with required data
674 foreach my $table ( keys %tabledata ) {
675     print "Checking for data required in table $table...\n" unless $silent;
676     my $tablerows = $tabledata{$table};
677     foreach my $row (@$tablerows) {
678         my $uniquefieldrequired = $row->{uniquefieldrequired};
679         my $uniquevalue         = $row->{$uniquefieldrequired};
680         my $forceupdate         = $row->{forceupdate};
681         my $sth                 =
682           $dbh->prepare(
683 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
684         );
685         $sth->execute($uniquevalue);
686         if ($sth->rows) {
687             foreach my $field (keys %$forceupdate) {
688                 if ($forceupdate->{$field}) {
689                     my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
690                     $sth->execute($row->{$field}, $uniquevalue);
691                 }
692             }
693         } else {
694             print "Adding row to $table: " unless $silent;
695             my @values;
696             my $fieldlist;
697             my $placeholders;
698             foreach my $field ( keys %$row ) {
699                 next if $field eq 'uniquefieldrequired';
700                 next if $field eq 'forceupdate';
701                 my $value = $row->{$field};
702                 push @values, $value;
703                 print "  $field => $value" unless $silent;
704                 $fieldlist .= "$field,";
705                 $placeholders .= "?,";
706             }
707             print "\n" unless $silent;
708             $fieldlist    =~ s/,$//;
709             $placeholders =~ s/,$//;
710             my $sth =
711               $dbh->prepare(
712                 "insert into $table ($fieldlist) values ($placeholders)");
713             $sth->execute(@values);
714         }
715     }
716 }
717
718 # at last, remove useless fields
719 foreach $table ( keys %uselessfields ) {
720         my @fields = split /,/,$uselessfields{$table};
721         my $fields;
722         my $exists;
723         foreach my $fieldtodrop (@fields) {
724                 $fieldtodrop =~ s/\t//g;
725                 $fieldtodrop =~ s/\n//g;
726                 $exists =0;
727                 $sth = $dbh->prepare("show columns from $table");
728                 $sth->execute;
729                 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
730                 {
731                         $exists =1 if ($column eq $fieldtodrop);
732                 }
733                 if ($exists) {
734                         print "deleting $fieldtodrop field in $table...\n" unless $silent;
735                         my $sth = $dbh->prepare("alter table $table drop $fieldtodrop");
736                         $sth->execute;
737                 }
738         }
739 }    # foreach
740
741
742 $sth->finish;
743
744 exit;
745
746 # $Log$
747 # Revision 1.115  2005/08/02 16:15:34  tipaul
748 # adding 2 fields to letter system :
749 # * module (acquisition, catalogue...) : it will be usefull to show the librarian only letters he may be interested by.
750 # * title, that will be used as mail subject.
751 #
752 # Revision 1.114  2005/07/28 15:10:13  tipaul
753 # Introducing new "Letters" system : Letters will be used everytime you want to sent something to someone (through mail or paper). For example, sending a mail for overdues use letter that you can put as parameters. Sending a mail to a borrower when a suggestion is validated uses a letter too.
754 # the letter table contains 3 fields :
755 # * code => the code of the letter
756 # * name => the complete name of the letter
757 # * content => the complete text. It's a TEXT field type, so has no limits.
758 #
759 # My next goal now is to work on point 2-I "serial issue alert"
760 # With this feature, in serials, a user can subscribe the "issue alert". For every issue arrived/missing, a mail is sent to all subscribers of this list. The mail warns the user that the issue is arrive or missing. Will be in head.
761 # (see mail on koha-devel, 2005/04/07)
762 #
763 # The "serial issue alert" will be the 1st to use this letter system that probably needs some tweaking ;-)
764 #
765 # Once it will be stabilised default letters (in any languages) could be added during installer to help the library begin with this new feature.
766 #
767 # Revision 1.113  2005/07/28 08:38:41  tipaul
768 # For instance, the return date does not rely on the borrower expiration date. A systempref will be added in Koha, to modify return date calculation schema :
769 # * ReturnBeforeExpiry = yes => return date can't be after expiry date
770 # * ReturnBeforeExpiry = no  => return date can be after expiry date
771 #
772 # Revision 1.112  2005/07/26 08:19:47  hdl
773 # Adding IndependantBranches System preference variable in order to manage Branch independancy.
774 #
775 # Revision 1.111  2005/07/25 15:35:38  tipaul
776 # we have decided that moving to Koha 3.0 requires being already in Koha 2.2.x
777 # So, the updatedatabase script can highly be cleaned (90% removed).
778 # Let's play with the new Koha DB structure now ;-)
779 #