Add some consistency checks to SQL model

This commit is contained in:
Greg Burri 2025-05-23 12:03:50 +02:00
parent 03f74e76ca
commit 988849e598

View file

@ -263,7 +263,16 @@ CREATE TABLE [ShoppingEntry] (
CHECK ( CHECK (
length([name]) <= 255 AND length([name]) <= 255 AND
([is_checked] = TRUE OR [is_checked] = FALSE) ([is_checked] = TRUE OR [is_checked] = FALSE) AND
(
[recipe_scheduled_id] IS NULL OR
(
[name] = '' AND
[quantity_value] IS NULL AND
[quantity_unit] = '' AND
[servings] IS NULL
)
)
), ),
FOREIGN KEY([user_id]) REFERENCES [User]([id]) ON DELETE CASCADE, FOREIGN KEY([user_id]) REFERENCES [User]([id]) ON DELETE CASCADE,
@ -284,20 +293,21 @@ BEGIN
[name] = OLD.[name], [name] = OLD.[name],
[quantity_value] = OLD.[quantity_value], [quantity_value] = OLD.[quantity_value],
[quantity_unit] = OLD.[quantity_unit], [quantity_unit] = OLD.[quantity_unit],
[servings] = ( [servings] = COALESCE((
SELECT [servings] SELECT [servings]
FROM [Recipe] FROM [Recipe]
INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id] INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id]
INNER JOIN [Step] ON [Step].[group_id] = [Group].[id] INNER JOIN [Step] ON [Step].[group_id] = [Group].[id]
WHERE [Step].[id] = OLD.[step_id] WHERE [Step].[id] = OLD.[step_id]
) LIMIT 1 -- In case a step is owned by more than one recipe (should never happen).
), 4)
WHERE [ingredient_id] = OLD.[id]; WHERE [ingredient_id] = OLD.[id];
END; END;
CREATE TABLE [Settings] ( CREATE TABLE [Settings] (
[name] TEXT NOT NULL PRIMARY KEY, [name] TEXT NOT NULL PRIMARY KEY,
-- Value can by anything that can be read from a text by -- Value can be anything that can be read from a text by
-- implementing the trait 'std::str::FromStr'. -- implementing the trait 'std::str::FromStr'.
[value] TEXT NOT NULL [value] TEXT NOT NULL
) STRICT; ) STRICT;