Files
quality_recticel/py_app/app/backup_db_scripts/create_triggers.py
Quality System Admin af62fa478f feat: Implement comprehensive database setup system
- Add complete database setup script (setup_complete_database.py)
- Add quick deployment script (quick_deploy.sh)
- Add comprehensive documentation (DATABASE_SETUP_README.md)
- Move individual db scripts to backup_db_scripts folder
- Update external_server.conf with correct database settings
- Clean up obsolete documentation files
- Streamline deployment process to single command

Features:
- One-script database creation for all tables and triggers
- Automated permissions and roles setup
- Complete verification and error handling
- Production-ready deployment workflow
- Maintains backward compatibility with individual scripts
2025-10-11 21:45:37 +03:00

70 lines
2.3 KiB
Python
Executable File

import mariadb
# Database connection credentials
db_config = {
"user": "trasabilitate",
"password": "Initial01!",
"host": "localhost",
"database": "trasabilitate"
}
# Connect to the database
try:
conn = mariadb.connect(**db_config)
cursor = conn.cursor()
print("Connected to the database successfully!")
# Delete old triggers if they exist
try:
cursor.execute("DROP TRIGGER IF EXISTS increment_approved_quantity;")
print("Old trigger 'increment_approved_quantity' deleted successfully.")
except mariadb.Error as e:
print(f"Error deleting old trigger 'increment_approved_quantity': {e}")
try:
cursor.execute("DROP TRIGGER IF EXISTS increment_rejected_quantity;")
print("Old trigger 'increment_rejected_quantity' deleted successfully.")
except mariadb.Error as e:
print(f"Error deleting old trigger 'increment_rejected_quantity': {e}")
# Create corrected trigger for approved_quantity
create_approved_trigger = """
CREATE TRIGGER increment_approved_quantity
BEFORE INSERT ON scan1_orders
FOR EACH ROW
BEGIN
IF NEW.quality_code = 000 THEN
SET NEW.approved_quantity = (
SELECT COUNT(*)
FROM scan1_orders
WHERE CP_base_code = NEW.CP_base_code AND quality_code = 000
) + 1;
SET NEW.rejected_quantity = (
SELECT COUNT(*)
FROM scan1_orders
WHERE CP_base_code = NEW.CP_base_code AND quality_code != 000
);
ELSE
SET NEW.approved_quantity = (
SELECT COUNT(*)
FROM scan1_orders
WHERE CP_base_code = NEW.CP_base_code AND quality_code = 000
);
SET NEW.rejected_quantity = (
SELECT COUNT(*)
FROM scan1_orders
WHERE CP_base_code = NEW.CP_base_code AND quality_code != 000
) + 1;
END IF;
END;
"""
cursor.execute(create_approved_trigger)
print("Trigger 'increment_approved_quantity' created successfully!")
# Commit changes and close the connection
conn.commit()
cursor.close()
conn.close()
except mariadb.Error as e:
print(f"Error connecting to the database or creating triggers: {e}")