Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 3b9c2119

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 3b9c2119792ae678e9368a0141d7f8646fd6ef42
  • Vorgänger 1f0a5bd8
  • Nachfolger 2477c498

Abhängigkeiten von Lieferscheinen und Aufträgen/Angeboten in der Datenbank löschen

und nicht mehr programmseitig. Das geschieht durch Verwendung von
sinnvollen 'ON DELETE CASCADE/SET NULL'-Foreign-Key-Anweisungen und
Triggern, die aus diejenigen Tabellen löschen, in denen eine
trans_id-Spalte auf mehrere Tabellen verweisen kann (status, ship_to).

Unterschiede anzeigen:

SL/DO.pm
39 39
use SL::AM;
40 40
use SL::Common;
41 41
use SL::CVar;
42
use SL::DB::DeliveryOrder;
43
use SL::DB::Status;
42 44
use SL::DBUtils;
43 45
use SL::RecordLinks;
44 46
use SL::IC;
......
507 509
  my $form     = $main::form;
508 510
  my $spool    = $::lx_office_conf{paths}->{spool};
509 511

  
510
  # connect to database
511
  my $dbh = $form->get_standard_dbh($myconfig);
512

  
513
  # delete spool files
514
  my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
515
  my $sth   = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
516

  
517
  my $spoolfile;
518
  my @spoolfiles = ();
519
  my @values;
520

  
521
  while (($spoolfile) = $sth->fetchrow_array) {
522
    push @spoolfiles, $spoolfile;
523
  }
524
  $sth->finish();
525

  
526
  # delete-values
527
  @values = (conv_i($form->{id}));
528

  
529
  # delete status entries
530
  $query = qq|DELETE FROM status
531
              WHERE trans_id = ?|;
532
  do_query($form, $dbh, $query, @values);
512
  my $rc = SL::DB::Order->new->db->with_transaction(sub {
513
    my @spoolfiles = grep { $_ } map { $_->spoolfile } @{ SL::DB::Manager::Status->get_all(where => [ trans_id => $form->{id} ]) };
533 514

  
534
  # delete individual entries
535
  $query = qq|DELETE FROM delivery_order_items_stock
536
              WHERE delivery_order_item_id IN (
537
                SELECT id FROM delivery_order_items
538
                WHERE delivery_order_id = ?
539
              )|;
540
  do_query($form, $dbh, $query, @values);
541

  
542
  # delete individual entries
543
  $query = qq|DELETE FROM delivery_order_items
544
              WHERE delivery_order_id = ?|;
545
  do_query($form, $dbh, $query, @values);
546

  
547
  # delete DO record
548
  $query = qq|DELETE FROM delivery_orders
549
              WHERE id = ?|;
550
  do_query($form, $dbh, $query, @values);
515
    SL::DB::DeliveryOrder->new(id => $form->{id})->delete;
551 516

  
552
  $query = qq|DELETE FROM shipto
553
              WHERE trans_id = ? AND module = 'DO'|;
554
  do_query($form, $dbh, $query, @values);
555

  
556
  my $rc = $dbh->commit();
517
    my $spool = $::lx_office_conf{paths}->{spool};
518
    unlink map { "$spool/$_" } @spoolfiles if $spool;
557 519

  
558
  if ($rc) {
559
    foreach $spoolfile (@spoolfiles) {
560
      unlink "$spool/$spoolfile" if $spoolfile;
561
    }
562
  }
520
    1;
521
  });
563 522

  
564 523
  $main::lxdebug->leave_sub();
565 524

  
SL/OE.pm
40 40
use SL::AM;
41 41
use SL::Common;
42 42
use SL::CVar;
43
use SL::DB::Order;
43 44
use SL::DB::PeriodicInvoicesConfig;
45
use SL::DB::Status;
44 46
use SL::DBUtils;
45 47
use SL::IC;
46 48

  
......
660 662

  
661 663
  my ($self, $myconfig, $form) = @_;
662 664

  
663
  # connect to database
664
  my $dbh = $form->get_standard_dbh;
665
  $dbh->begin_work;
666

  
667
  # delete spool files
668
  my $query = qq|SELECT s.spoolfile FROM status s | .
669
              qq|WHERE s.trans_id = ?|;
670
  my @values = (conv_i($form->{id}));
671
  my $sth = $dbh->prepare($query);
672
  $sth->execute(@values) || $self->dberror($query);
673

  
674
  my $spoolfile;
675
  my @spoolfiles = ();
676

  
677
  while (($spoolfile) = $sth->fetchrow_array) {
678
    push @spoolfiles, $spoolfile;
679
  }
680
  $sth->finish;
681

  
682
  # delete-values
683
  @values = (conv_i($form->{id}));
684

  
685
  # periodic invoices and their configuration
686
  do_query($form, $dbh, qq|DELETE FROM periodic_invoices         WHERE config_id IN (SELECT id FROM periodic_invoices_configs WHERE oe_id = ?)|, @values);
687
  do_query($form, $dbh, qq|DELETE FROM periodic_invoices_configs WHERE oe_id = ?|, @values);
688

  
689
  # delete status entries
690
  $query = qq|DELETE FROM status | .
691
           qq|WHERE trans_id = ?|;
692
  do_query($form, $dbh, $query, @values);
693

  
694
  # delete individual entries
695
  $query = qq|DELETE FROM orderitems | .
696
           qq|WHERE trans_id = ?|;
697
  do_query($form, $dbh, $query, @values);
665
  my $rc = SL::DB::Order->new->db->with_transaction(sub {
666
    my @spoolfiles = grep { $_ } map { $_->spoolfile } @{ SL::DB::Manager::Status->get_all(where => [ trans_id => $form->{id} ]) };
698 667

  
699
  $query = qq|DELETE FROM shipto | .
700
           qq|WHERE trans_id = ? AND module = 'OE'|;
701
  do_query($form, $dbh, $query, @values);
702

  
703
  # delete OE record
704
  $query = qq|DELETE FROM oe | .
705
           qq|WHERE id = ?|;
706
  do_query($form, $dbh, $query, @values);
668
    SL::DB::Order->new(id => $form->{id})->delete;
707 669

  
708
  my $rc = $dbh->commit;
709

  
710
  if ($rc) {
711 670
    my $spool = $::lx_office_conf{paths}->{spool};
712
    foreach $spoolfile (@spoolfiles) {
713
      unlink "$spool/$spoolfile" if $spoolfile;
714
    }
715
  }
671
    unlink map { "$spool/$_" } @spoolfiles if $spool;
672

  
673
    1;
674
  });
716 675

  
717 676
  $main::lxdebug->leave_sub();
718 677

  
sql/Pg-upgrade2/oe_do_delete_via_trigger.pl
1
# @tag: oe_do_delete_via_trigger
2
# @description: Aus oe/delivery_orders via Trigger löschen können
3
# @depends: release_3_0_0
4

  
5
package SL::DBUpgrade2::oe_do_delete_via_trigger;
6

  
7
use utf8;
8
use strict;
9

  
10
use parent qw(SL::DBUpgrade2::Base);
11

  
12
sub run {
13
  my ($self) = @_;
14

  
15
  $self->drop_constraints(table => $_) for qw(periodic_invoices periodic_invoices_configs orderitems delivery_order_items delivery_order_items_stock);
16

  
17
  my @queries = (
18
    q|ALTER TABLE periodic_invoices          ADD CONSTRAINT periodic_invoices_ar_id_fkey                           FOREIGN KEY (ar_id)                  REFERENCES ar                        (id) ON DELETE CASCADE|,
19
    q|ALTER TABLE periodic_invoices          ADD CONSTRAINT periodic_invoices_config_id_fkey                       FOREIGN KEY (config_id)              REFERENCES periodic_invoices_configs (id) ON DELETE CASCADE|,
20

  
21
    q|ALTER TABLE periodic_invoices_configs  ADD CONSTRAINT periodic_invoices_configs_ar_chart_id_fkey             FOREIGN KEY (ar_chart_id)            REFERENCES chart                     (id) ON DELETE RESTRICT|,
22
    q|ALTER TABLE periodic_invoices_configs  ADD CONSTRAINT periodic_invoices_configs_oe_id_fkey                   FOREIGN KEY (oe_id)                  REFERENCES oe                        (id) ON DELETE CASCADE|,
23
    q|ALTER TABLE periodic_invoices_configs  ADD CONSTRAINT periodic_invoices_configs_printer_id_fkey              FOREIGN KEY (printer_id)             REFERENCES printers                  (id) ON DELETE SET NULL|,
24

  
25
    q|ALTER TABLE orderitems                 ADD CONSTRAINT orderitems_parts_id_fkey                               FOREIGN KEY (parts_id)               REFERENCES parts                     (id) ON DELETE RESTRICT|,
26
    q|ALTER TABLE orderitems                 ADD CONSTRAINT orderitems_price_factor_id_fkey                        FOREIGN KEY (price_factor_id)        REFERENCES price_factors             (id) ON DELETE RESTRICT|,
27
    q|ALTER TABLE orderitems                 ADD CONSTRAINT orderitems_pricegroup_id_fkey                          FOREIGN KEY (pricegroup_id)          REFERENCES pricegroup                (id) ON DELETE RESTRICT|,
28
    q|ALTER TABLE orderitems                 ADD CONSTRAINT orderitems_project_id_fkey                             FOREIGN KEY (project_id)             REFERENCES project                   (id) ON DELETE SET NULL|,
29
    q|ALTER TABLE orderitems                 ADD CONSTRAINT orderitems_trans_id_fkey                               FOREIGN KEY (trans_id)               REFERENCES oe                        (id) ON DELETE CASCADE|,
30

  
31
    q|ALTER TABLE delivery_order_items       ADD CONSTRAINT delivery_order_items_delivery_order_id_fkey            FOREIGN KEY (delivery_order_id)      REFERENCES delivery_orders           (id) ON DELETE CASCADE|,
32
    q|ALTER TABLE delivery_order_items       ADD CONSTRAINT delivery_order_items_parts_id_fkey                     FOREIGN KEY (parts_id)               REFERENCES parts                     (id) ON DELETE RESTRICT|,
33
    q|ALTER TABLE delivery_order_items       ADD CONSTRAINT delivery_order_items_price_factor_id_fkey              FOREIGN KEY (price_factor_id)        REFERENCES price_factors             (id) ON DELETE RESTRICT|,
34
    q|ALTER TABLE delivery_order_items       ADD CONSTRAINT delivery_order_items_pricegroup_id_fkey                FOREIGN KEY (pricegroup_id)          REFERENCES pricegroup                (id) ON DELETE RESTRICT|,
35
    q|ALTER TABLE delivery_order_items       ADD CONSTRAINT delivery_order_items_project_id_fkey                   FOREIGN KEY (project_id)             REFERENCES project                   (id) ON DELETE SET NULL|,
36

  
37
    q|ALTER TABLE delivery_order_items_stock ADD CONSTRAINT delivery_order_items_stock_bin_id_fkey                 FOREIGN KEY (bin_id)                 REFERENCES bin                       (id) ON DELETE RESTRICT|,
38
    q|ALTER TABLE delivery_order_items_stock ADD CONSTRAINT delivery_order_items_stock_delivery_order_item_id_fkey FOREIGN KEY (delivery_order_item_id) REFERENCES delivery_order_items      (id) ON DELETE CASCADE|,
39
    q|ALTER TABLE delivery_order_items_stock ADD CONSTRAINT delivery_order_items_stock_warehouse_id_fkey           FOREIGN KEY (warehouse_id)           REFERENCES warehouse                 (id) ON DELETE RESTRICT|,
40

  
41
    q|CREATE OR REPLACE FUNCTION oe_before_delete_trigger() RETURNS trigger AS $$
42
        BEGIN
43
          DELETE FROM status WHERE trans_id = OLD.id;
44
          DELETE FROM shipto WHERE (trans_id = OLD.id) AND (module = 'OE');
45

  
46
          RETURN OLD;
47
        END;
48
      $$ LANGUAGE plpgsql|,
49

  
50
    q|DROP TRIGGER IF EXISTS delete_oe_dependencies ON oe|,
51

  
52
    q|CREATE TRIGGER delete_oe_dependencies
53
      BEFORE DELETE ON oe
54
      FOR EACH ROW EXECUTE PROCEDURE oe_before_delete_trigger()|,
55

  
56
    q|CREATE OR REPLACE FUNCTION delivery_orders_before_delete_trigger() RETURNS trigger AS $$
57
        BEGIN
58
          DELETE FROM status                     WHERE trans_id = OLD.id;
59
          DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = OLD.id);
60
          DELETE FROM shipto                     WHERE (trans_id = OLD.id) AND (module = 'OE');
61

  
62
          RETURN OLD;
63
        END;
64
      $$ LANGUAGE plpgsql|,
65

  
66
    q|DROP TRIGGER IF EXISTS delete_delivery_orders_dependencies ON delivery_orders|,
67

  
68
    q|CREATE TRIGGER delete_delivery_orders_dependencies
69
      BEFORE DELETE ON delivery_orders
70
      FOR EACH ROW EXECUTE PROCEDURE delivery_orders_before_delete_trigger()|);
71

  
72
  $self->db_query($_) for @queries;
73

  
74
  return 1;
75
}
76

  
77
1;

Auch abrufbar als: Unified diff