Charset & "use utf8" bei UTF-8-encodierten Datenbankupgradescripten gefixt
[kivitendo-erp.git] / sql / Pg-upgrade2 / add_more_constraints_fibu_projekt_xplace.pl
1 # @tag: add_more_constraints_fibu_projekt_xplace3
2 # @description: Falls der Datenbestand es unproblematisch hergibt, ein paar 'schärfere' Constraints für die acc_trans gesetzt. Keine acc_trans-Eintrag ohne trans_id oder chart_id. Ferner project_id in acc_trans als Fremdschlüssel für project definiert.
3 # @depends: release_2_6_0 fix_acc_trans_ap_taxkey_bug
4 # @charset: utf-8
5
6 use utf8;
7 use strict;
8 use Data::Dumper;
9 die("This script cannot be run from the command line.") unless ($main::form);
10
11 sub mydberror {
12   my ($msg) = @_;
13   die($dbup_locale->text("Database update error:") .
14       "<br>$msg<br>" . $DBI::errstr);
15 }
16
17 sub do_query {
18   my ($query, $may_fail) = @_;
19
20   if (!$dbh->do($query)) {
21     mydberror($query) unless ($may_fail);
22     $dbh->rollback();
23     $dbh->begin_work();
24   }
25 }
26
27 sub do_update {
28   my @queries;
29
30   # die project_id in der acc_trans ist auch zwingend fremdschlüssel in project 
31   push @queries, "ALTER TABLE acc_trans ADD FOREIGN KEY (project_id) REFERENCES project(id)";
32
33   my $query = qq|select count(*) from acc_trans where chart_id is NULL|;
34   my $sth_all_groups = prepare_execute_query($form, $dbh, $query);
35   while (my $hash_ref = $sth_all_groups->fetchrow_hashref()) {  # Schleife
36     if ($hash_ref->{count} eq 0){
37       # Falls wir keine alte buggy Installation haben, ist es super die 
38       # Gewissheit zu haben, dass kein acc_trans-Eintrag ohne chart_id vorhanden ist
39       push @queries, "ALTER TABLE acc_trans ALTER COLUMN chart_id SET NOT NULL";
40     }
41   }
42   $sth_all_groups->finish();
43   my $query = qq|select count(*) from acc_trans where trans_id is NULL|;
44   my $sth_all_groups = prepare_execute_query($form, $dbh, $query);
45   while (my $hash_ref = $sth_all_groups->fetchrow_hashref()) {  # Schleife
46     if ($hash_ref->{count} eq 0){
47       # Falls wir keine alte buggy Installation haben, ist es super die 
48       # Gewissheit zu haben, dass kein acc_trans-Eintrag ohne trans_id vorhanden ist
49       push @queries, "ALTER TABLE acc_trans ALTER COLUMN trans_id SET NOT NULL";
50     }
51   }
52   $sth_all_groups->finish();
53
54   # if in doubt use brute force ;-) jb
55   foreach my $query (@queries){
56     my $sth   = prepare_query($form, $dbh, $query);
57     do_statement($form,$sth,$query);
58     $sth->finish();
59   }
60   $dbh ->commit();
61   return 1;
62 }
63
64 return do_update();
65