Changeset d715225 for music


Ignore:
Timestamp:
05/07/25 11:05:41 (3 weeks ago)
Author:
ManuelTrajcev <manueltrajcev7@…>
Branches:
master
Children:
75ea229
Parents:
d1a8cb0
Message:

deleted customer log trigger

Location:
music
Files:
4 added
6 edited

Legend:

Unmodified
Added
Removed
  • music/admin.py

    rd1a8cb0 rd715225  
    1616admin.site.register(PlaylistTrack, PlaylistTrackAdmin)
    1717admin.site.register(Track)
     18admin.site.register(DeletedCustomerLog)
  • music/models.py

    rd1a8cb0 rd715225  
    113113class Invoice(models.Model):
    114114    invoice_id = models.AutoField(primary_key=True)
    115     customer = models.ForeignKey(Customer, on_delete=models.CASCADE, db_column='customer_id')
     115    customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, db_column='customer_id', null=True)
    116116    invoice_date = models.DateTimeField()
    117117    billing_address = models.CharField(max_length=70, blank=True, null=True)
     
    158158    class Meta:
    159159        db_table = 'playlist_track'
    160         managed = False
    161160        unique_together = (('playlist', 'track'),)
    162161
     
    170169    last_name = models.CharField(max_length=20)
    171170    deleted_at = models.DateTimeField()
    172 
     171    total_spent = models.DecimalField(max_digits=10, decimal_places=2)
     172    invoice_count = models.IntegerField()
    173173    class Meta:
    174174        db_table = 'deleted_customer_log'
    175         managed = False
    176175
    177176    def __str__(self):
    178         return f"{self.first_name} {self.last_name} {self.deleted_at}"
     177        return f"{self.first_name} {self.last_name} - Deleted on {self.deleted_at} - Total Spent: ${self.total_spent} from {self.invoice_count} invoices"
  • music/triggers/customer_deletion.sql

    rd1a8cb0 rd715225  
     1DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
     2DROP FUNCTION IF EXISTS customer_deletion();
     3
    14CREATE OR REPLACE FUNCTION customer_deletion()
    25RETURNS TRIGGER AS $$
     6DECLARE
     7    total_spent NUMERIC(10, 2);
     8    invoice_count INTEGER;
    39BEGIN
    4     INSERT INTO deleted_customer_log (first_name, last_name, deleted_at)
    5     VALUES (OLD.first_name, OLD.last_name, NOW());
     10    SELECT COALESCE(SUM(total), 0), COUNT(*)
     11    INTO total_spent, invoice_count
     12    FROM invoice
     13    WHERE customer_id = OLD.customer_id;
     14
     15    INSERT INTO deleted_customer_log (
     16        first_name, last_name, deleted_at, total_spent, invoice_count
     17    )
     18    VALUES (
     19        OLD.first_name, OLD.last_name, NOW(), total_spent, invoice_count
     20    );
     21
    622    RETURN OLD;
    723END;
    824$$ LANGUAGE plpgsql;
    925
     26DROP TRIGGER IF EXISTS trg_customer_deletion ON customer;
     27
    1028CREATE TRIGGER trg_customer_deletion
    11 AFTER DELETE ON customer
     29BEFORE DELETE ON customer
    1230FOR EACH ROW
    1331EXECUTE FUNCTION customer_deletion();
Note: See TracChangeset for help on using the changeset viewer.