# @tag: change_taxzone_id_0
-# @description: Verändert die id von 0 auf einen anderen Wert größer 0 in tax_zones, wenn es so einen Eintrag gibt.
+# @description: Verändert die id von 0 auf einen anderen Wert größer 0 in tax_zones, wenn es so einen Eintrag gibt und setzt zwei Fremdschlüssel von Tabellen ar/ap auf tax_zones.
# @depends: convert_taxzone taxzone_default_id taxzone_sortkey
package SL::DBUpgrade2::change_taxzone_id_0;
sub run {
my ($self) = @_;
- my $query = qq|SELECT id FROM tax_zones ORDER BY id DESC|;
+ my $query = qq|SELECT id FROM tax_zones ORDER BY id DESC LIMIT 1|;
my ($id) = $self->dbh->selectrow_array($query);
$id++;
my $sth = $self->dbh->prepare($query);
$sth->execute || $::form->dberror($query);
- my $ref = $sth->fetchrow_hashref("NAME_lc");
+ if (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
- $query = qq|INSERT INTO tax_zones (id, description, sortkey) VALUES (?,?,?)|;
- $self->db_query($query, bind => [ $id, $ref->{description}, $ref->{sortkey} ]);
+ $query = qq|INSERT INTO tax_zones (id, description, sortkey) VALUES (?,?,?)|;
+ $self->db_query($query, bind => [ $id, $ref->{description}, $ref->{sortkey} ]);
- $query = qq|UPDATE taxzone_charts SET taxzone_id=$id WHERE taxzone_id=0|;
- $self->db_query($query);
+ $query = qq|UPDATE taxzone_charts SET taxzone_id=$id WHERE taxzone_id=0|;
+ $self->db_query($query);
- $sth->finish;
+ $query = qq|DELETE FROM tax_zones WHERE id=0|;
+ $self->db_query($query);
+
+ #Adapt other tables to the new id:
+ $query = qq|UPDATE ar SET taxzone_id=$id WHERE taxzone_id=0|;
+ $self->db_query($query);
+
+ $query = qq|UPDATE ap SET taxzone_id=$id WHERE taxzone_id=0|;
+ $self->db_query($query);
+ }
- $query = qq|DELETE FROM tax_zones WHERE id=0|;
+ #Set Constraints:
+ $query = qq|ALTER TABLE ar ADD FOREIGN KEY (taxzone_id) REFERENCES tax_zones (id)|;
$self->db_query($query);
+ $query = qq|ALTER TABLE ap ADD FOREIGN KEY (taxzone_id) REFERENCES tax_zones (id)|;
+ $self->db_query($query);
+
+ $sth->finish;
+
return 1;
} # end run