From 480d7740a172002fc6b115f9ca4bf73bf7809ead Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Tue, 6 Feb 2024 19:58:49 +0100
Subject: WIP: model: Rework database model

---
 model/sql/migrate.sql | 79 ++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 66 insertions(+), 13 deletions(-)

(limited to 'model/sql/migrate.sql')

diff --git a/model/sql/migrate.sql b/model/sql/migrate.sql
index 4cbf75b..57f9e1d 100644
--- a/model/sql/migrate.sql
+++ b/model/sql/migrate.sql
@@ -1,14 +1,67 @@
-CREATE TABLE 'recipes' (
-	'id'       integer PRIMARY KEY AUTOINCREMENT,
-	'title'    text    DEFAULT '',
-	'portions' text    DEFAULT '',  -- FIXME has to be integer
-	'url'      text    DEFAULT '',
-	'notes'    text    DEFAULT ''
-);
-
-CREATE TABLE 'recipe_steps' (
-	'id'        integer PRIMARY KEY,
-	'recipe_id' integer DEFAULT NULL,
-	'index'     integer DEFAULT NULL,
-	'text'      text    DEFAULT ''
+PRAGMA foreign_keys = ON;
+
+CREATE TABLE instances (
+	id           INTEGER PRIMARY KEY,
+	uuid         TEXT    NOT NULL,
+	name         TEXT    NOT NULL,
+	is_self      BOOLEAN NOT NULL,
+	last_changed INTEGER NOT NULL
+);
+
+CREATE TABLE users (
+	id           INTEGER PRIMARY KEY,
+	uuid         TEXT    NOT NULL,
+	name         TEXT    NOT NULL,
+	home         INTEGER NOT NULL,
+	last_changed INTEGER NOT NULL,
+	FOREIGN KEY(home) REFERENCES instances(id)
+);
+
+CREATE TABLE recipes (
+	id   INTEGER PRIMARY KEY,
+	uuid TEXT    NOT NULL
+);
+
+CREATE TABLE recipe_versions (
+	id           INTEGER PRIMARY KEY,
+	parent       INTEGER NOT NULL,
+	recipe       INTEGER NOT NULL,
+	author       INTEGER NOT NULL,
+	instance     INTEGER NOT NULL,
+	language     TEXT    NOT NULL,
+	title        TEXT    NOT NULL,
+	portions     INTEGER NOT NULL,
+	url          TEXT    NOT NULL,
+	notes        TEXT    NOT NULL,
+	last_changed INTEGER NOT NULL,
+	FOREIGN KEY(parent)   REFERENCES recipe_versions(id),
+	FOREIGN KEY(recipe)   REFERENCES recipes(id),
+	FOREIGN KEY(author)   REFERENCES users(id),
+	FOREIGN KEY(instance) REFERENCES instances(id)
+);
+
+CREATE TABLE steps (
+	id   INTEGER PRIMARY KEY,
+	text TEXT    NOT NULL
+);
+
+CREATE TABLE ingredients (
+	id            INTEGER PRIMARY KEY,
+	ingredient_id TEXT    NOT NULL,
+	amount        REAL    NOT NULL,
+	unit          INTEGER NOT NULL -- must match unit enum from application code
+);
+
+CREATE TABLE foodstuffs (
+	id     INTEGER PRIMARY KEY,
+	uuid   TEXT    NOT NULL,
+	name   TEXT    NOT NULL, -- in US english
+	plural TEXT              -- in US english or NULL if uncountable
+);
+
+CREATE TABLE products (
+	id   INTEGER PRIMARY KEY,
+	uuid TEXT    NOT NULL,
+	name TEXT    NOT NULL,
+	gtin TEXT
 );
-- 
cgit v1.2.3-70-g09d2

