From 12d23c81fd988c71985324ed8709bb4ed3b679d2 Mon Sep 17 00:00:00 2001 From: tonnesen Date: Tue, 12 Nov 2002 17:42:40 +0000 Subject: [PATCH] Merged some features over from rel-1-2, including primary key checking. --- updater/updatedatabase | 142 ++++++++++++++++++++++++++++++++++------- 1 file changed, 120 insertions(+), 22 deletions(-) diff --git a/updater/updatedatabase b/updater/updatedatabase index 1f61d9570c..a3cc10310d 100755 --- a/updater/updatedatabase +++ b/updater/updatedatabase @@ -232,16 +232,56 @@ my %dropable_table=( serialissues =>'serialissues', ); -# Default system preferences -my %defaultprefs=( - 'autoMemberNum'=> ['1','1 or else. If 1, Barcode is auto-calculated'], - 'acquisitions'=> ['simple','normal or simple : will use acquisition system found in directory acqui.simple or acquisition'], - 'template' => ['default','template default name'], - 'autoBarcode' => ['0','1 or else. If 1, Barcode is auto-calculated'], - 'insecure' => ['no','if YES, no auth at all is needed. Be careful if you set this to yes !'], - 'authoritysep' => ['--','the separator used in authority/thesaurus. Usually --'] - ); +# The tabledata hash contains data that should be in the tables. +# The uniquefieldrequired hash entry is used to determine which (if any) fields +# must not exist in the table for this row to be inserted. If the +# uniquefieldrequired entry is already in the table, the existing data is not +# modified. + +my %tabledata=( + userflags => [ + { uniquefieldrequired => 'bit', bit => 0, flag => 'superlibrarian', flagdesc => 'Access to all librarian functions', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 1, flag => 'circulate', flagdesc => 'Circulate books', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 2, flag => 'catalogue', flagdesc => 'View Catalogue (Librarian Interface)', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 3, flag => 'parameters', flagdesc => 'Set Koha system paramters', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 4, flag => 'borrowers', flagdesc => 'Add or modify borrowers', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 5, flag => 'permissions', flagdesc => 'Set user permissions', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 6, flag => 'reserveforothers', flagdesc => 'Reserve books for patrons', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 7, flag => 'borrow', flagdesc => 'Borrow books', defaulton => 1 }, + { uniquefieldrequired => 'bit', bit => 8, flag => 'reserveforself', flagdesc => 'Reserve books for self', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 9, flag => 'editcatalogue', flagdesc => 'Edit Catalogue (Modify bibliographic/holdings data)', defaulton => 0 }, + { uniquefieldrequired => 'bit', bit => 10, flag => 'updatecharges', flagdesc => 'Update borrower charges', defaulton => 0 }, + ], + systempreferences => [ + { uniquefieldrequired => 'variable', variable => 'autoMemberNum', value => '1', explanation => '1 or else. If 1, Barcode is auto-calculated' }, + { uniquefieldrequired => 'variable', variable => 'acquisitions', value => 'simple', explanation => 'normal or simple : will use acquisition system found in directory acqui.simple or acquisition' }, + { uniquefieldrequired => 'variable', variable => 'dateformat', value => 'metric', explanation => 'metric or us' }, + { uniquefieldrequired => 'variable', variable => 'template', value => 'metric', explanation => 'template default name' }, + { uniquefieldrequired => 'variable', variable => 'autoBarcode', value => 'metric', explanation => '1 or else. If 1, Barcode is auto-calculated' }, + { uniquefieldrequired => 'variable', variable => 'insecure', value => 'metric', explanation => 'if YES, no auth at all is needed. Be careful if you set this to yes !' }, + { uniquefieldrequired => 'variable', variable => 'authoritysep', value => '--', explanation => 'the separator used in authority/thesaurus. Usually --' }, + ], + +); + + +my %fielddefinitions=( +printers => [ + { field => 'printername', type => 'char(40)', null => '', key => 'PRI', default => '' }, + ], +aqbookfund => [ + { field => 'bookfundid', type => 'char(5)', null => '', key => 'PRI', default => '' }, + ], +z3950servers => [ + { field => 'id', type => 'int', null => '', key => 'PRI', default => '', extra => 'auto_increment' }, + ], +); + + + +#------------------- +# Initialize # Start checking @@ -341,6 +381,44 @@ foreach $table ( keys %requirefields ) { } # foreach column } # foreach table +foreach $table ( keys %fielddefinitions ) { + print "Check table $table\n" if $debug; + $sth=$dbh->prepare("show columns from $table"); + $sth->execute(); + my $definitions; + while ( ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) { + $definitions->{$column}->{type}=$type; + $definitions->{$column}->{null}=$null; + $definitions->{$column}->{key}=$key; + $definitions->{$column}->{default}=$default; + $definitions->{$column}->{extra}=$extra; + } # while + my $fieldrow=$fielddefinitions{$table}; + foreach my $row ( @$fieldrow ) { + my $field = $row->{field}; + my $type = $row->{type}; + my $null = $row->{null}; + my $key = $row->{key}; + my $default = $row->{default}; + my $extra = $row->{extra}; + my $def=$definitions->{$field}; + unless ($type eq $def->{type} && $null eq $def->{null} && $key eq $def->{key} && $default eq $def->{default} && $extra eq $def->{extra}) { + if ($null eq '') { + $null='NOT NULL'; + } + if ($key eq 'PRI') { + $key ='PRIMARY KEY'; + } + unless ($extra eq 'auto_increment') { + $extra=''; + } + my $sth=$dbh->prepare("alter table $table change $field $field $type $null $key $extra default ?"); + $sth->execute($default); + print " Alter $field in $table\n"; + } + } +} + # Get list of columns from items table my %itemtypes; @@ -411,26 +489,46 @@ unless ($branchcategories{'codedescription'} eq 'text') { } -# Populate systempreferences if it is empty - -foreach $prefitem ( keys %defaultprefs ) { - $sth=$dbh->prepare("select value - from systempreferences - where variable=?"); - $sth->execute($prefitem); +# Populate tables with required data + +foreach my $table (keys %tabledata) { + print "Checking for data required in table $table...\n"; + my $tablerows=$tabledata{$table}; + foreach my $row (@$tablerows) { + my $uniquefieldrequired=$row->{uniquefieldrequired}; + my $uniquevalue=$row->{$uniquefieldrequired}; + my $sth=$dbh->prepare("select $uniquefieldrequired from $table where $uniquefieldrequired=?"); + $sth->execute($uniquevalue); unless ($sth->rows) { - print "Adding system preference item $prefitem with value " .$defaultprefs{$prefitem}[0] ."\n"; - $sti=$dbh->prepare("insert into systempreferences (variable, value,explanation) values (?,?,?)"); - $sti->execute($prefitem,$defaultprefs{$prefitem}[0],$defaultprefs{$prefitem}[1]); - } # unless -} # foreach - + print "Adding row to $table: "; + my @values; + my $fieldlist; + my $placeholders; + foreach my $field (keys %$row) { + (next) if ($field eq 'uniquefieldrequired'); + my $value=$row->{$field}; + push @values, $value; + print " $field => $value"; + $fieldlist.="$field,"; + $placeholders.="?,"; + } + print "\n"; + $fieldlist=~s/,$//; + $placeholders=~s/,$//; + my $sth=$dbh->prepare("insert into $table ($fieldlist) values ($placeholders)"); + $sth->execute(@values); + } + } +} $sth->finish; exit; # $Log$ +# Revision 1.26 2002/11/12 17:42:40 tonnesen +# Merged some features over from rel-1-2, including primary key checking. +# # Revision 1.25 2002/11/12 16:44:38 tipaul # road to 1.3.2 : # * many bugfixes -- 2.39.2