Add LOT model mapping management, UI, and first-seen tracking
This commit is contained in:
2
migrations/0013_lot_model_mappings/down.sql
Normal file
2
migrations/0013_lot_model_mappings/down.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
DROP TABLE IF EXISTS lot_model_mappings;
|
||||
DELETE FROM id_sequences WHERE entity_type = 'lot_model_mapping';
|
||||
15
migrations/0013_lot_model_mappings/up.sql
Normal file
15
migrations/0013_lot_model_mappings/up.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
CREATE TABLE IF NOT EXISTS lot_model_mappings (
|
||||
id VARCHAR(16) PRIMARY KEY,
|
||||
model VARCHAR(255) NOT NULL,
|
||||
lot_id VARCHAR(16) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_lot_model_mappings_lot
|
||||
FOREIGN KEY (lot_id) REFERENCES lots(id)
|
||||
ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
UNIQUE KEY uniq_lot_model_mappings_model (model),
|
||||
INDEX idx_lot_model_mappings_lot (lot_id)
|
||||
);
|
||||
|
||||
INSERT IGNORE INTO id_sequences (entity_type, next_value)
|
||||
VALUES ('lot_model_mapping', 1);
|
||||
1
migrations/0014_backfill_lot_model_mappings/down.sql
Normal file
1
migrations/0014_backfill_lot_model_mappings/down.sql
Normal file
@@ -0,0 +1 @@
|
||||
DELETE FROM lot_model_mappings WHERE id LIKE 'LM-BF-%';
|
||||
19
migrations/0014_backfill_lot_model_mappings/up.sql
Normal file
19
migrations/0014_backfill_lot_model_mappings/up.sql
Normal file
@@ -0,0 +1,19 @@
|
||||
SET @bf_row := 0;
|
||||
|
||||
INSERT INTO lot_model_mappings (id, model, lot_id, created_at, updated_at)
|
||||
SELECT
|
||||
CONCAT('LM-BF-', LPAD(@bf_row := @bf_row + 1, 7, '0')) AS id,
|
||||
src.model,
|
||||
src.lot_id,
|
||||
CURRENT_TIMESTAMP,
|
||||
CURRENT_TIMESTAMP
|
||||
FROM (
|
||||
SELECT TRIM(model) AS model, MIN(lot_id) AS lot_id
|
||||
FROM parts
|
||||
WHERE lot_id IS NOT NULL
|
||||
AND model IS NOT NULL
|
||||
AND TRIM(model) <> ''
|
||||
GROUP BY TRIM(model)
|
||||
) src
|
||||
LEFT JOIN lot_model_mappings m ON m.model = src.model
|
||||
WHERE m.id IS NULL;
|
||||
2
migrations/0015_parts_first_seen_at/down.sql
Normal file
2
migrations/0015_parts_first_seen_at/down.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE parts
|
||||
DROP COLUMN first_seen_at;
|
||||
11
migrations/0015_parts_first_seen_at/up.sql
Normal file
11
migrations/0015_parts_first_seen_at/up.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
ALTER TABLE parts
|
||||
ADD COLUMN first_seen_at TIMESTAMP NULL;
|
||||
|
||||
UPDATE parts p
|
||||
JOIN (
|
||||
SELECT part_id, MIN(observed_at) AS first_seen_at
|
||||
FROM observations
|
||||
GROUP BY part_id
|
||||
) o ON o.part_id = p.id
|
||||
SET p.first_seen_at = o.first_seen_at
|
||||
WHERE p.first_seen_at IS NULL;
|
||||
Reference in New Issue
Block a user