Fixed the scan error and backup problems
This commit is contained in:
@@ -394,56 +394,76 @@ def create_database_triggers():
|
||||
conn = mariadb.connect(**DB_CONFIG)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Drop existing triggers if they exist
|
||||
# Drop existing triggers if they exist (old and new names)
|
||||
trigger_drops = [
|
||||
"DROP TRIGGER IF EXISTS increment_approved_quantity;",
|
||||
"DROP TRIGGER IF EXISTS increment_rejected_quantity;",
|
||||
"DROP TRIGGER IF EXISTS increment_approved_quantity_fg;",
|
||||
"DROP TRIGGER IF EXISTS increment_rejected_quantity_fg;"
|
||||
"DROP TRIGGER IF EXISTS increment_rejected_quantity_fg;",
|
||||
"DROP TRIGGER IF EXISTS set_quantities_scan1;",
|
||||
"DROP TRIGGER IF EXISTS set_quantities_fg;"
|
||||
]
|
||||
|
||||
for drop_query in trigger_drops:
|
||||
cursor.execute(drop_query)
|
||||
|
||||
# Create trigger for scan1_orders approved quantity
|
||||
scan1_approved_trigger = """
|
||||
CREATE TRIGGER increment_approved_quantity
|
||||
AFTER INSERT ON scan1_orders
|
||||
# Create trigger for scan1_orders - BEFORE INSERT to set quantities
|
||||
scan1_trigger = """
|
||||
CREATE TRIGGER set_quantities_scan1
|
||||
BEFORE INSERT ON scan1_orders
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.quality_code = 000 THEN
|
||||
UPDATE scan1_orders
|
||||
SET approved_quantity = approved_quantity + 1
|
||||
WHERE CP_base_code = NEW.CP_base_code;
|
||||
-- Count existing approved for this CP_base_code
|
||||
SET @approved = (SELECT COUNT(*) FROM scan1_orders
|
||||
WHERE CP_base_code = LEFT(NEW.CP_full_code, 10)
|
||||
AND quality_code = 0);
|
||||
|
||||
-- Count existing rejected for this CP_base_code
|
||||
SET @rejected = (SELECT COUNT(*) FROM scan1_orders
|
||||
WHERE CP_base_code = LEFT(NEW.CP_full_code, 10)
|
||||
AND quality_code != 0);
|
||||
|
||||
-- Add 1 to appropriate counter for this new row
|
||||
IF NEW.quality_code = 0 THEN
|
||||
SET NEW.approved_quantity = @approved + 1;
|
||||
SET NEW.rejected_quantity = @rejected;
|
||||
ELSE
|
||||
UPDATE scan1_orders
|
||||
SET rejected_quantity = rejected_quantity + 1
|
||||
WHERE CP_base_code = NEW.CP_base_code;
|
||||
SET NEW.approved_quantity = @approved;
|
||||
SET NEW.rejected_quantity = @rejected + 1;
|
||||
END IF;
|
||||
END;
|
||||
"""
|
||||
cursor.execute(scan1_approved_trigger)
|
||||
print_success("Trigger 'increment_approved_quantity' created for scan1_orders")
|
||||
cursor.execute(scan1_trigger)
|
||||
print_success("Trigger 'set_quantities_scan1' created for scan1_orders")
|
||||
|
||||
# Create trigger for scanfg_orders approved quantity
|
||||
scanfg_approved_trigger = """
|
||||
CREATE TRIGGER increment_approved_quantity_fg
|
||||
AFTER INSERT ON scanfg_orders
|
||||
# Create trigger for scanfg_orders - BEFORE INSERT to set quantities
|
||||
scanfg_trigger = """
|
||||
CREATE TRIGGER set_quantities_fg
|
||||
BEFORE INSERT ON scanfg_orders
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.quality_code = 000 THEN
|
||||
UPDATE scanfg_orders
|
||||
SET approved_quantity = approved_quantity + 1
|
||||
WHERE CP_base_code = NEW.CP_base_code;
|
||||
-- Count existing approved for this CP_base_code
|
||||
SET @approved = (SELECT COUNT(*) FROM scanfg_orders
|
||||
WHERE CP_base_code = LEFT(NEW.CP_full_code, 10)
|
||||
AND quality_code = 0);
|
||||
|
||||
-- Count existing rejected for this CP_base_code
|
||||
SET @rejected = (SELECT COUNT(*) FROM scanfg_orders
|
||||
WHERE CP_base_code = LEFT(NEW.CP_full_code, 10)
|
||||
AND quality_code != 0);
|
||||
|
||||
-- Add 1 to appropriate counter for this new row
|
||||
IF NEW.quality_code = 0 THEN
|
||||
SET NEW.approved_quantity = @approved + 1;
|
||||
SET NEW.rejected_quantity = @rejected;
|
||||
ELSE
|
||||
UPDATE scanfg_orders
|
||||
SET rejected_quantity = rejected_quantity + 1
|
||||
WHERE CP_base_code = NEW.CP_base_code;
|
||||
SET NEW.approved_quantity = @approved;
|
||||
SET NEW.rejected_quantity = @rejected + 1;
|
||||
END IF;
|
||||
END;
|
||||
"""
|
||||
cursor.execute(scanfg_approved_trigger)
|
||||
print_success("Trigger 'increment_approved_quantity_fg' created for scanfg_orders")
|
||||
cursor.execute(scanfg_trigger)
|
||||
print_success("Trigger 'set_quantities_fg' created for scanfg_orders")
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
|
||||
Reference in New Issue
Block a user