Simplify change tracking
This commit is contained in:
@@ -1,44 +1,6 @@
|
||||
-- Disable foreign key checks
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- Create metric status tracking table
|
||||
CREATE TABLE IF NOT EXISTS product_metric_status (
|
||||
pid BIGINT NOT NULL,
|
||||
last_calculated_at TIMESTAMP NULL,
|
||||
needs_recalculation BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (pid),
|
||||
INDEX idx_needs_recalc (needs_recalculation),
|
||||
INDEX idx_last_calc (last_calculated_at),
|
||||
FOREIGN KEY (pid) REFERENCES products(pid) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB;
|
||||
|
||||
-- Add triggers for metric recalculation
|
||||
DROP TRIGGER IF EXISTS orders_after_insert_update;
|
||||
CREATE TRIGGER orders_after_insert_update AFTER INSERT ON orders FOR EACH ROW
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES (NEW.pid, TRUE)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP;
|
||||
|
||||
DROP TRIGGER IF EXISTS purchase_orders_after_insert_update;
|
||||
CREATE TRIGGER purchase_orders_after_insert_update AFTER INSERT ON purchase_orders FOR EACH ROW
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES (NEW.pid, TRUE)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP;
|
||||
|
||||
DROP TRIGGER IF EXISTS products_after_insert_update;
|
||||
CREATE TRIGGER products_after_insert_update AFTER INSERT ON products FOR EACH ROW
|
||||
INSERT INTO product_metric_status (pid, needs_recalculation)
|
||||
VALUES (NEW.pid, TRUE)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
needs_recalculation = TRUE,
|
||||
updated_at = CURRENT_TIMESTAMP;
|
||||
|
||||
-- Temporary tables for batch metrics processing
|
||||
CREATE TABLE IF NOT EXISTS temp_sales_metrics (
|
||||
pid BIGINT NOT NULL,
|
||||
|
||||
Reference in New Issue
Block a user