- Timestamp:
- 05/07/25 11:05:41 (3 weeks ago)
- Branches:
- master
- Children:
- 75ea229
- Parents:
- d1a8cb0
- Location:
- music
- Files:
-
- 4 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
music/admin.py
rd1a8cb0 rd715225 16 16 admin.site.register(PlaylistTrack, PlaylistTrackAdmin) 17 17 admin.site.register(Track) 18 admin.site.register(DeletedCustomerLog) -
music/models.py
rd1a8cb0 rd715225 113 113 class Invoice(models.Model): 114 114 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) 116 116 invoice_date = models.DateTimeField() 117 117 billing_address = models.CharField(max_length=70, blank=True, null=True) … … 158 158 class Meta: 159 159 db_table = 'playlist_track' 160 managed = False161 160 unique_together = (('playlist', 'track'),) 162 161 … … 170 169 last_name = models.CharField(max_length=20) 171 170 deleted_at = models.DateTimeField() 172 171 total_spent = models.DecimalField(max_digits=10, decimal_places=2) 172 invoice_count = models.IntegerField() 173 173 class Meta: 174 174 db_table = 'deleted_customer_log' 175 managed = False176 175 177 176 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 1 DROP TRIGGER IF EXISTS trg_customer_deletion ON customer; 2 DROP FUNCTION IF EXISTS customer_deletion(); 3 1 4 CREATE OR REPLACE FUNCTION customer_deletion() 2 5 RETURNS TRIGGER AS $$ 6 DECLARE 7 total_spent NUMERIC(10, 2); 8 invoice_count INTEGER; 3 9 BEGIN 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 6 22 RETURN OLD; 7 23 END; 8 24 $$ LANGUAGE plpgsql; 9 25 26 DROP TRIGGER IF EXISTS trg_customer_deletion ON customer; 27 10 28 CREATE TRIGGER trg_customer_deletion 11 AFTERDELETE ON customer29 BEFORE DELETE ON customer 12 30 FOR EACH ROW 13 31 EXECUTE FUNCTION customer_deletion();
Note:
See TracChangeset
for help on using the changeset viewer.