improve db trigger, fix some fixtures not being inserted

This commit is contained in:
FrenchGithubUser
2025-09-17 15:16:52 +02:00
parent 0c506be7f7
commit b96a4ecb4b
3 changed files with 34 additions and 38 deletions
@@ -675,40 +675,35 @@ WHERE master_group_id IS NOT NULL;
CREATE FUNCTION enforce_collage_entry_type()
RETURNS TRIGGER AS $$
DECLARE
c_type collage_type_enum;
expected_id_column TEXT;
BEGIN
SELECT collage_type INTO c_type
SELECT
CASE collage_type
WHEN 'Artist' THEN 'artist_id'
WHEN 'Entity' THEN 'entity_id'
WHEN 'TitleGroup' THEN 'title_group_id'
WHEN 'MasterGroup' THEN 'master_group_id'
END
INTO expected_id_column
FROM collage
WHERE id = NEW.collage_id;
IF c_type = 'Artist' THEN
IF NEW.artist_id IS NULL OR
NEW.entity_id IS NOT NULL OR
NEW.title_group_id IS NOT NULL OR
NEW.master_group_id IS NOT NULL THEN
RAISE EXCEPTION 'CollageEntry must reference only artist_id for Artist collages';
END IF;
ELSIF c_type = 'Entity' THEN
IF NEW.entity_id IS NULL OR
NEW.artist_id IS NOT NULL OR
NEW.title_group_id IS NOT NULL OR
NEW.master_group_id IS NOT NULL THEN
RAISE EXCEPTION 'CollageEntry must reference only entity_id for Entity collages';
END IF;
ELSIF c_type = 'TitleGroup' THEN
IF NEW.title_group_id IS NULL OR
NEW.artist_id IS NOT NULL OR
NEW.entity_id IS NOT NULL OR
NEW.master_group_id IS NOT NULL THEN
RAISE EXCEPTION 'CollageEntry must reference only title_group_id for TitleGroup collages';
END IF;
ELSIF c_type = 'MasterGroup' THEN
IF NEW.master_group_id IS NULL OR
NEW.artist_id IS NOT NULL OR
NEW.entity_id IS NOT NULL OR
NEW.title_group_id IS NOT NULL THEN
RAISE EXCEPTION 'CollageEntry must reference only master_group_id for MasterGroup collages';
END IF;
IF (
(expected_id_column = 'artist_id' AND NEW.artist_id IS NULL) OR
(expected_id_column = 'entity_id' AND NEW.entity_id IS NULL) OR
(expected_id_column = 'title_group_id' AND NEW.title_group_id IS NULL) OR
(expected_id_column = 'master_group_id' AND NEW.master_group_id IS NULL)
) THEN
RAISE EXCEPTION 'Collage entry must have a non-null % for collage ID %', expected_id_column, NEW.collage_id;
END IF;
IF (
(expected_id_column != 'artist_id' AND NEW.artist_id IS NOT NULL) OR
(expected_id_column != 'entity_id' AND NEW.entity_id IS NOT NULL) OR
(expected_id_column != 'title_group_id' AND NEW.title_group_id IS NOT NULL) OR
(expected_id_column != 'master_group_id' AND NEW.master_group_id IS NOT NULL)
) THEN
RAISE EXCEPTION 'Collage entry for collage ID % must not reference any other type than %', NEW.collage_id, expected_id_column;
END IF;
RETURN NEW;
@@ -11,7 +11,6 @@ SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
@@ -21,14 +20,14 @@ SET row_security = off;
-- Data for Name: _sqlx_migrations; Type: TABLE DATA; Schema: public; Owner: arcadia
--
INSERT INTO public._sqlx_migrations VALUES (20250312215600, 'initdb', '2025-09-17 09:15:16.459727+00', true, '\xf6e0db7e42d33acc1b0cf4bf604f99520d8c303d3f5c3627fc205c35975e8e7c387f9c10ea75a45afb16de09f18e8fa9', 33483991);
INSERT INTO public._sqlx_migrations VALUES (20250312215600, 'initdb', '2025-09-17 12:42:13.702455+00', true, '\xcbb89aaf6e977779db8db1699079eb30481c2745b8b69cfdd75caa158cd61110a59dcaf436456572a0e6671a330fbfb8', 34635271);
--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: arcadia
--
INSERT INTO public.users VALUES (1, 'creator', NULL, 'none@domain.com', 'none', '127.0.0.1', '2025-09-17 09:15:16.459727+00', '', 0, 0, 1, 1, 0, 0, '2025-09-17 09:15:16.459727+00', 'newbie', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '{}', 1, 1, false, false, '');
INSERT INTO public.users VALUES (1, 'creator', NULL, 'none@domain.com', 'none', '127.0.0.1', '2025-09-17 12:42:13.702455+00', '', 0, 0, 1, 1, 0, 0, '2025-09-17 12:42:13.702455+00', 'newbie', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '{}', 1, 1, false, false, '');
INSERT INTO public.users VALUES (5, 'waterbottle', 'https://i.pinimg.com/736x/a6/27/12/a6271204df8d387c3e614986c106f549.jpg', 'user2@example.com', 'hashedpassword2', '192.168.1.2', '2025-03-30 16:24:57.388152+00', '', 0, 0, 1, 1, 0, 0, '2025-03-30 16:24:57.388152+00', 'newbie', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '{"site_appearance": {"item_detail_layout": "sidebar_right"}}', 5493004881313328037, 2566432999990446913, false, false, '''''');
INSERT INTO public.users VALUES (3, 'coolguy', 'https://i.pinimg.com/474x/c1/5a/6c/c15a6c91515e22f6ea8b766f89c12f0c.jpg', 'user3@example.com', 'hashedpassword3', '192.168.1.3', '2025-03-30 16:24:57.388152+00', '', 0, 0, 1, 1, 0, 0, '2025-03-30 16:24:57.388152+00', 'newbie', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '{"site_appearance": {"item_detail_layout": "sidebar_right"}}', 2274483400846363122, 1270934296711348124, false, false, '''''');
INSERT INTO public.users VALUES (2, 'picolo', 'https://img.freepik.com/premium-vector/random-people-line-art-vector_567805-63.jpg', 'user1@example.com', '$argon2id$v=19$m=19456,t=2,p=1$s4XJtCUk9IrGgNsTfP6Ofw$ktoGbBEoFaVgdiTn19Gh9h45LjFiv7AUEL5KHhzm4d0', '192.168.1.1', '2025-03-30 16:24:57.388152+00', '', 10000, 0, 1, 1, 0, 0, '2025-09-17 09:27:11.336576+00', 'staff', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 999999410, 0, '{"site_appearance": {"item_detail_layout": "sidebar_right"}}', -197409747985172542, 1837889239438807682, false, false, '''''');
@@ -204,7 +203,7 @@ INSERT INTO public.edition_groups VALUES (18, 8, NULL, '2004-03-04 00:00:00+00',
-- Data for Name: forum_categories; Type: TABLE DATA; Schema: public; Owner: arcadia
--
INSERT INTO public.forum_categories VALUES (1, 'Site', '2025-09-17 09:15:16.459727+00', 1);
INSERT INTO public.forum_categories VALUES (1, 'Site', '2025-09-17 12:42:13.702455+00', 1);
INSERT INTO public.forum_categories VALUES (3, 'Community', '2025-05-22 08:01:35.753162+00', 1);
@@ -212,7 +211,7 @@ INSERT INTO public.forum_categories VALUES (3, 'Community', '2025-05-22 08:01:35
-- Data for Name: forum_sub_categories; Type: TABLE DATA; Schema: public; Owner: arcadia
--
INSERT INTO public.forum_sub_categories VALUES (1, 1, 'Announcements', '2025-09-17 09:15:16.459727+00', 1, 1, 1, '{}');
INSERT INTO public.forum_sub_categories VALUES (1, 1, 'Announcements', '2025-09-17 12:42:13.702455+00', 1, 1, 1, '{}');
INSERT INTO public.forum_sub_categories VALUES (5, 3, 'Lounge', '2025-05-22 08:02:15.125336+00', 1, 1, 1, '{}');
INSERT INTO public.forum_sub_categories VALUES (7, 3, 'Technology', '2025-05-22 08:05:01.192217+00', 1, 1, 1, '{}');
@@ -221,7 +220,7 @@ INSERT INTO public.forum_sub_categories VALUES (7, 3, 'Technology', '2025-05-22
-- Data for Name: forum_threads; Type: TABLE DATA; Schema: public; Owner: arcadia
--
INSERT INTO public.forum_threads VALUES (1, 1, 'Welcome to the site!', '2025-09-17 09:15:16.459727+00', 1, 1, false, false);
INSERT INTO public.forum_threads VALUES (1, 1, 'Welcome to the site!', '2025-09-17 12:42:13.702455+00', 1, 1, false, false);
INSERT INTO public.forum_threads VALUES (3, 5, 'Hello everyone', '2025-05-22 08:03:08.400255+00', 1, 1, false, false);
INSERT INTO public.forum_threads VALUES (5, 7, 'Favorite OS for daily driving', '2025-05-22 08:06:06.724797+00', 1, 1, false, false);
@@ -230,7 +229,7 @@ INSERT INTO public.forum_threads VALUES (5, 7, 'Favorite OS for daily driving',
-- Data for Name: forum_posts; Type: TABLE DATA; Schema: public; Owner: arcadia
--
INSERT INTO public.forum_posts VALUES (1, 1, '2025-09-17 09:15:16.459727+00', '2025-09-17 09:15:16.459727+00', 1, 'Welcome!', false);
INSERT INTO public.forum_posts VALUES (1, 1, '2025-09-17 12:42:13.702455+00', '2025-09-17 12:42:13.702455+00', 1, 'Welcome!', false);
INSERT INTO public.forum_posts VALUES (3, 3, '2025-05-22 08:03:48.66391+00', '2025-05-22 08:03:48.66391+00', 1, 'Hello there, I just joined!', false);
INSERT INTO public.forum_posts VALUES (4, 5, '2025-05-22 08:06:36.225458+00', '2025-05-22 08:06:36.225458+00', 2, 'I use arch btw :)', false);
+3 -1
View File
@@ -94,9 +94,11 @@ The default test user is `picolo` with password `test`.
If you added some new test data and wish to include it in your commit, you can export it like so:
```bash
docker exec -i arcadia_db pg_dump -U arcadia -d arcadia --data-only --inserts > backend/storage/migrations/fixtures/fixtures.sql
docker exec -i arcadia_db pg_dump -U arcadia -d arcadia --data-only --inserts > backend/storage/migrations/fixtures/fixtures.sql && sed -i '/SELECT pg_catalog.set_config(\x27search_path\x27, \x27\x27, false);/d' migrations/fixtures/fixtures.sql
```
1 line generated by `pgdump` must be removed as it prevents the `collage_entry` fixtures from being inserted (the trigger somehow can't be interprted). If someone has an explanation, please let us know/open a PR!
## Manual Database Setup (if needed)
Arcadia automatically runs migrations on launch, but if you need to manually set up the database: