1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
-- All tables should be declared STRICT, as I actually like to have types checking (and a
-- db that doesn't lie to me).
CREATE TABLE version (
-- The `namespace` is only useful, if other tools ever build on this database
namespace TEXT NOT NULL,
-- The version.
number INTEGER UNIQUE NOT NULL PRIMARY KEY,
-- The validity of this version as UNIX time stamp
valid_from INTEGER NOT NULL CHECK (valid_from < valid_to),
-- If set to `NULL`, represents the current version
valid_to INTEGER UNIQUE CHECK (valid_to > valid_from)
) STRICT;
-- Encodes the tree structure of the products.
-- A parent cannot be a product, but can have parents on it's own.
-- TODO: Fix the possibility for cyclic parent-ship entries <2025-09-05>
CREATE TABLE parents (
id TEXT UNIQUE NOT NULL PRIMARY KEY,
parent TEXT DEFAULT NULL CHECK (
id IS NOT parent
),
FOREIGN KEY(parent) REFERENCES parents(id)
) STRICT;
-- Record with barcodes were bought, and how much of this buy is already used up.
CREATE TABLE buys (
buy_id TEXT UNIQUE NOT NULL PRIMARY KEY,
barcode_id INTEGER NOT NULL,
used_amount INTEGER DEFAULT NULL CHECK (used_amount > 0),
timestamp INTEGER NOT NULL,
FOREIGN KEY(barcode_id) REFERENCES barcodes(id)
) STRICT;
CREATE TRIGGER used_amount_lower_or_equal_than_actual_amount_update
BEFORE UPDATE OF used_amount ON buys
FOR EACH ROW
BEGIN
SELECT RAISE(FAIL, "Used amount higher than actual amount")
FROM barcodes
WHERE NEW.barcode_id = barcodes.id
AND NEW.used_amount > barcodes.amount;
END;
CREATE TRIGGER used_amount_lower_or_equal_than_actual_amount_insert
BEFORE INSERT ON buys
FOR EACH ROW
BEGIN
SELECT RAISE(FAIL, "Used amount higher than actual amount")
FROM barcodes
WHERE NEW.barcode_id = barcodes.id
AND NEW.used_amount > barcodes.amount;
END;
CREATE TABLE products (
id TEXT UNIQUE NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
description TEXT,
parent TEXT DEFAULT NULL,
FOREIGN KEY(parent) REFERENCES parents(id)
) STRICT;
CREATE TABLE barcodes (
id INTEGER UNIQUE NOT NULL PRIMARY KEY,
product_id TEXT NOT NULL,
amount INTEGER NOT NULL,
unit TEXT NOT NULL,
FOREIGN KEY(product_id) REFERENCES products(id),
FOREIGN KEY(unit) REFERENCES units(id)
) STRICT;
CREATE TABLE units (
id TEXT UNIQUE NOT NULL PRIMARY KEY,
full_name_singular TEXT UNIQUE NOT NULL,
full_name_plural TEXT UNIQUE NOT NULL,
short_name TEXT UNIQUE NOT NULL,
description TEXT
) STRICT;
-- Encodes unit conversions:
-- {factor} {from_unit} = 1 {to_unit}
-- E.g.: 1000 g = 1 kg
CREATE TABLE unit_conversions (
from_unit TEXT NOT NULL,
to_unit TEXT NOT NULL,
factor REAL NOT NULL,
FOREIGN KEY(from_unit) REFERENCES units(id),
FOREIGN KEY(to_unit) REFERENCES units(id)
) STRICT;
-- Log of all the applied operations to this db.
CREATE TABLE txn_log (
timestamp INTEGER NOT NULL,
operation TEXT NOT NULL
) STRICT;
|