epic-s6ts
[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 package SL::DBUpgrade2::add_more_constraints_fibu_projekt_xplace3;
5
6 use strict;
7 use utf8;
8
9 use parent qw(SL::DBUpgrade2::Base);
10
11 use SL::DBUtils;
12
13 sub run {
14   my ($self) = @_;
15
16   my @queries;
17
18   # die project_id in der acc_trans ist auch zwingend fremdschlüssel in project
19   push @queries, "ALTER TABLE acc_trans ADD FOREIGN KEY (project_id) REFERENCES project(id)";
20
21   my $query = qq|select count(*) from acc_trans where chart_id is NULL|;
22   my $sth_all_groups = prepare_execute_query($::form, $self->dbh, $query);
23   while (my $hash_ref = $sth_all_groups->fetchrow_hashref()) {  # Schleife
24     if ($hash_ref->{count} eq 0){
25       # Falls wir keine alte buggy Installation haben, ist es super die
26       # Gewissheit zu haben, dass kein acc_trans-Eintrag ohne chart_id vorhanden ist
27       push @queries, "ALTER TABLE acc_trans ALTER COLUMN chart_id SET NOT NULL";
28     }
29   }
30   $sth_all_groups->finish();
31   $query = qq|select count(*) from acc_trans where trans_id is NULL|;
32   $sth_all_groups = prepare_execute_query($::form, $self->dbh, $query);
33   while (my $hash_ref = $sth_all_groups->fetchrow_hashref()) {  # Schleife
34     if ($hash_ref->{count} eq 0){
35       # Falls wir keine alte buggy Installation haben, ist es super die
36       # Gewissheit zu haben, dass kein acc_trans-Eintrag ohne trans_id vorhanden ist
37       push @queries, "ALTER TABLE acc_trans ALTER COLUMN trans_id SET NOT NULL";
38     }
39   }
40   $sth_all_groups->finish();
41
42   # if in doubt use brute force ;-) jb
43   foreach my $query (@queries){
44     my $sth   = prepare_query($::form, $self->dbh, $query);
45     do_statement($::form,$sth,$query);
46     $sth->finish();
47   }
48   return 1;
49 }
50
51 1;