brewlog/migrations/0003_activities.sql
Jon Seager 97c46d034b
fix(db): add AUTOINCREMENT to prevent SQLite ROWID reuse
Without AUTOINCREMENT, SQLite can reassign the highest deleted ROWID to
a new row. This is a data integrity bug: a deleted entity's ID could be
assigned to a completely different entity, breaking bookmarks, API
caches, and URL stability.
2026-02-06 10:09:13 +00:00

42 lines
1.7 KiB
SQL

-- Brew, cafe, and cup tables
CREATE TABLE brews (
id INTEGER PRIMARY KEY AUTOINCREMENT,
bag_id INTEGER NOT NULL REFERENCES bags(id) ON DELETE CASCADE,
coffee_weight REAL NOT NULL,
grinder_id INTEGER NOT NULL REFERENCES gear(id) ON DELETE RESTRICT,
grind_setting REAL NOT NULL,
brewer_id INTEGER NOT NULL REFERENCES gear(id) ON DELETE RESTRICT,
filter_paper_id INTEGER REFERENCES gear(id) ON DELETE SET NULL,
water_volume INTEGER NOT NULL,
water_temp REAL NOT NULL,
quick_notes TEXT,
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);
CREATE INDEX idx_brews_bag_id ON brews(bag_id);
CREATE INDEX idx_brews_created_at ON brews(created_at DESC);
CREATE TABLE cafes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
slug TEXT NOT NULL,
city TEXT NOT NULL,
country TEXT NOT NULL,
latitude REAL NOT NULL,
longitude REAL NOT NULL,
website TEXT,
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);
CREATE UNIQUE INDEX idx_cafes_slug ON cafes(slug);
CREATE TABLE cups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
roast_id INTEGER NOT NULL REFERENCES roasts(id) ON DELETE RESTRICT,
cafe_id INTEGER NOT NULL REFERENCES cafes(id) ON DELETE RESTRICT,
created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
updated_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
);
CREATE INDEX idx_cups_roast_id ON cups(roast_id);
CREATE INDEX idx_cups_cafe_id ON cups(cafe_id);