Commit 8a817bb1 authored by Hugo Hörnquist's avatar Hugo Hörnquist

Removed diff.sql

Patch file only for updating. Shouldn't clutter file system after that.
parent c67a1efd
/*
* This file is to patch up an already existing SQLite file
* to use the new product diffing system
*/
CREATE TABLE diff_help (
id INTEGER PRIMARY KEY NOT NULL,
product_id INTEGER NOT NULL,
amount INTEGER,
FOREIGN KEY (product_id) REFERENCES products(id)
);
CREATE VIEW diff_view AS
SELECT d.id AS id,
p.id AS product_id,
p.sale_status AS sale_status,
-- visible border
p.name AS name,
s.active AS expected,
d.amount AS actual,
d.amount - s.active AS diff
FROM products p
INNER JOIN stock s ON p.id = s.product_id
INNER JOIN diff_help d ON p.id = d.product_id;
CREATE TRIGGER after_diff_help_delete_last
AFTER DELETE ON diff_help
FOR EACH ROW
WHEN (SELECT count (1) = 0 FROM diff_help)
BEGIN
INSERT INTO diff_help (product_id)
SELECT id FROM products p;
END;
CREATE TRIGGER diff_view_transfer
INSTEAD OF DELETE ON diff_view
BEGIN
UPDATE stock SET active = OLD.actual
WHERE OLD.actual IS NOT NULL
AND product_id = OLD.product_id;
INSERT INTO stock_diff (product_id, expected, actual)
SELECT OLD.product_id, OLD.expected, OLD.actual
WHERE OLD.actual IS NOT NULL;
DELETE FROM diff_help WHERE id = OLD.id;
END;
CREATE TRIGGER update_view_trigger
INSTEAD OF UPDATE ON diff_view
BEGIN
UPDATE diff_help SET amount = NEW.actual WHERE id = NEW.id;
END;
INSERT INTO diff_help (product_id)
SELECT id FROM products LIMIT 1;
DELETE FROM diff_help;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment