Skip to content

Commit

Permalink
feat: add tmp_pictures table and make backend create pictures
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Oct 21, 2024
1 parent c01123a commit 125ad0d
Show file tree
Hide file tree
Showing 13 changed files with 2,340 additions and 212 deletions.
7 changes: 7 additions & 0 deletions db/migrations/908-add_tmp_pictures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE tmp_pictures (
id TEXT PRIMARY KEY,
"reportId" TEXT REFERENCES report(id) ON DELETE CASCADE,
"createdAt" TIMESTAMP
);

ALTER TABLE tmp_pictures ENABLE ELECTRIC;
7 changes: 5 additions & 2 deletions packages/backend/src/routes/uploadRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export const uploadPlugin: FastifyPluginAsyncTypebox = async (fastify, _) => {

debug("adding url to pic", id, "for report", reportId);

// await db.pictures.create({ data: { id, url, reportId, createdAt: new Date() } });
await db.pictures.update({ where: { id }, data: { url } });
await db.pictures.create({ data: { id, url, reportId, createdAt: new Date() } });
// try {
// await db.tmp_pictures.delete({ where: { id } });
// } catch (e) {}
// await db.pictures.update({ where: { id }, data: { url } });

reply.send(url);

Expand Down
16 changes: 12 additions & 4 deletions packages/electric-client/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ model clause {
}

model report {
id String @id
id String @id
title String?
projectDescription String?
redactedBy String?
meetDate DateTime? @db.Timestamp(6)
meetDate DateTime? @db.Timestamp(6)
applicantName String?
applicantAddress String?
projectCadastralRef String?
Expand All @@ -52,7 +52,7 @@ model report {
contacts String?
furtherInformation String?
createdBy String
createdAt DateTime @db.Timestamp(6)
createdAt DateTime @db.Timestamp(6)
serviceInstructeur Int?
pdf String?
disabled Boolean?
Expand All @@ -62,7 +62,8 @@ model report {
city String?
zipCode String?
pictures pictures[]
user user @relation(fields: [createdBy], references: [id], onDelete: SetNull, onUpdate: NoAction)
user user @relation(fields: [createdBy], references: [id], onDelete: SetNull, onUpdate: NoAction)
tmp_pictures tmp_pictures[]
}

model delegation {
Expand Down Expand Up @@ -150,3 +151,10 @@ model pictures {
createdAt DateTime? @db.Timestamp(6)
report report? @relation(fields: [reportId], references: [id], onDelete: Cascade, onUpdate: NoAction)
}

model tmp_pictures {
id String @id
reportId String?
createdAt DateTime? @db.Timestamp(6)
report report? @relation(fields: [reportId], references: [id], onDelete: Cascade, onUpdate: NoAction)
}
574 changes: 562 additions & 12 deletions packages/electric-client/src/generated/client/index.ts

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions packages/electric-client/src/generated/client/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,24 @@ export default [
"CREATE TRIGGER compensation_update_main_pictures_reportId_into_oplog\n AFTER UPDATE ON \"main\".\"pictures\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'pictures') AND\n 1 = (SELECT value from _electric_meta WHERE key = 'compensations')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n SELECT 'main', 'report', 'COMPENSATION', json_patch('{}', json_object('id', \"id\")), json_object('id', \"id\"), NULL, NULL\n FROM \"main\".\"report\" WHERE \"id\" = new.\"reportId\";\nEND;"
],
"version": "907"
},
{
"statements": [
"CREATE TABLE \"tmp_pictures\" (\n \"id\" TEXT NOT NULL,\n \"reportId\" TEXT,\n \"createdAt\" TEXT,\n CONSTRAINT \"tmp_pictures_reportId_fkey\" FOREIGN KEY (\"reportId\") REFERENCES \"report\" (\"id\") ON DELETE CASCADE,\n CONSTRAINT \"tmp_pictures_pkey\" PRIMARY KEY (\"id\")\n) WITHOUT ROWID;\n",
"INSERT OR IGNORE INTO _electric_trigger_settings (namespace, tablename, flag) VALUES ('main', 'tmp_pictures', 1);",
"DROP TRIGGER IF EXISTS update_ensure_main_tmp_pictures_primarykey;",
"CREATE TRIGGER update_ensure_main_tmp_pictures_primarykey\n BEFORE UPDATE ON \"main\".\"tmp_pictures\"\nBEGIN\n SELECT\n CASE\n WHEN old.\"id\" != new.\"id\" THEN\n \t\tRAISE (ABORT, 'cannot change the value of column id as it belongs to the primary key')\n END;\nEND;",
"DROP TRIGGER IF EXISTS insert_main_tmp_pictures_into_oplog;",
"CREATE TRIGGER insert_main_tmp_pictures_into_oplog\n AFTER INSERT ON \"main\".\"tmp_pictures\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'tmp_pictures')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'tmp_pictures', 'INSERT', json_patch('{}', json_object('id', new.\"id\")), json_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'reportId', new.\"reportId\"), NULL, NULL);\nEND;",
"DROP TRIGGER IF EXISTS update_main_tmp_pictures_into_oplog;",
"CREATE TRIGGER update_main_tmp_pictures_into_oplog\n AFTER UPDATE ON \"main\".\"tmp_pictures\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'tmp_pictures')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'tmp_pictures', 'UPDATE', json_patch('{}', json_object('id', new.\"id\")), json_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'reportId', new.\"reportId\"), json_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'reportId', old.\"reportId\"), NULL);\nEND;",
"DROP TRIGGER IF EXISTS delete_main_tmp_pictures_into_oplog;",
"CREATE TRIGGER delete_main_tmp_pictures_into_oplog\n AFTER DELETE ON \"main\".\"tmp_pictures\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'tmp_pictures')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'tmp_pictures', 'DELETE', json_patch('{}', json_object('id', old.\"id\")), NULL, json_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'reportId', old.\"reportId\"), NULL);\nEND;",
"DROP TRIGGER IF EXISTS compensation_insert_main_tmp_pictures_reportId_into_oplog;",
"CREATE TRIGGER compensation_insert_main_tmp_pictures_reportId_into_oplog\n AFTER INSERT ON \"main\".\"tmp_pictures\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'tmp_pictures') AND\n 1 = (SELECT value from _electric_meta WHERE key = 'compensations')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n SELECT 'main', 'report', 'COMPENSATION', json_patch('{}', json_object('id', \"id\")), json_object('id', \"id\"), NULL, NULL\n FROM \"main\".\"report\" WHERE \"id\" = new.\"reportId\";\nEND;",
"DROP TRIGGER IF EXISTS compensation_update_main_tmp_pictures_reportId_into_oplog;",
"CREATE TRIGGER compensation_update_main_tmp_pictures_reportId_into_oplog\n AFTER UPDATE ON \"main\".\"tmp_pictures\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'tmp_pictures') AND\n 1 = (SELECT value from _electric_meta WHERE key = 'compensations')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n SELECT 'main', 'report', 'COMPENSATION', json_patch('{}', json_object('id', \"id\")), json_object('id', \"id\"), NULL, NULL\n FROM \"main\".\"report\" WHERE \"id\" = new.\"reportId\";\nEND;"
],
"version": "908"
}
]
25 changes: 25 additions & 0 deletions packages/electric-client/src/generated/client/pg-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,30 @@ export default [
"CREATE TRIGGER compensation_update_public_pictures_reportId_into_oplog\n AFTER UPDATE ON \"public\".\"pictures\"\n FOR EACH ROW\n EXECUTE FUNCTION compensation_update_public_pictures_reportId_into_oplog_function();"
],
"version": "907"
},
{
"statements": [
"CREATE TABLE tmp_pictures (\n id text NOT NULL,\n \"reportId\" text,\n \"createdAt\" timestamp without time zone,\n CONSTRAINT tmp_pictures_pkey PRIMARY KEY (id),\n CONSTRAINT \"tmp_pictures_reportId_fkey\" FOREIGN KEY (\"reportId\") REFERENCES report(id) ON DELETE CASCADE\n)",
"INSERT INTO \"public\".\"_electric_trigger_settings\" (\"namespace\", \"tablename\", \"flag\")\n VALUES ('public', 'tmp_pictures', 1)\n ON CONFLICT DO NOTHING;",
"DROP TRIGGER IF EXISTS update_ensure_public_tmp_pictures_primarykey ON \"public\".\"tmp_pictures\";",
"CREATE OR REPLACE FUNCTION update_ensure_public_tmp_pictures_primarykey_function()\nRETURNS TRIGGER AS $$\nBEGIN\n IF OLD.\"id\" IS DISTINCT FROM NEW.\"id\" THEN\n RAISE EXCEPTION 'Cannot change the value of column id as it belongs to the primary key';\n END IF;\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql;",
"CREATE TRIGGER update_ensure_public_tmp_pictures_primarykey\n BEFORE UPDATE ON \"public\".\"tmp_pictures\"\n FOR EACH ROW\n EXECUTE FUNCTION update_ensure_public_tmp_pictures_primarykey_function();",
"DROP TRIGGER IF EXISTS insert_public_tmp_pictures_into_oplog ON \"public\".\"tmp_pictures\";",
" CREATE OR REPLACE FUNCTION insert_public_tmp_pictures_into_oplog_function()\n RETURNS TRIGGER AS $$\n BEGIN\n DECLARE\n flag_value INTEGER;\n BEGIN\n -- Get the flag value from _electric_trigger_settings\n SELECT flag INTO flag_value FROM \"public\"._electric_trigger_settings WHERE namespace = 'public' AND tablename = 'tmp_pictures';\n\n IF flag_value = 1 THEN\n -- Insert into _electric_oplog\n INSERT INTO \"public\"._electric_oplog (namespace, tablename, optype, \"primaryKey\", \"newRow\", \"oldRow\", timestamp)\n VALUES (\n 'public',\n 'tmp_pictures',\n 'INSERT',\n json_strip_nulls(json_build_object('id', new.\"id\")),\n jsonb_build_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'reportId', new.\"reportId\"),\n NULL,\n NULL\n );\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER insert_public_tmp_pictures_into_oplog\n AFTER INSERT ON \"public\".\"tmp_pictures\"\n FOR EACH ROW\n EXECUTE FUNCTION insert_public_tmp_pictures_into_oplog_function();",
"DROP TRIGGER IF EXISTS update_public_tmp_pictures_into_oplog ON \"public\".\"tmp_pictures\";",
" CREATE OR REPLACE FUNCTION update_public_tmp_pictures_into_oplog_function()\n RETURNS TRIGGER AS $$\n BEGIN\n DECLARE\n flag_value INTEGER;\n BEGIN\n -- Get the flag value from _electric_trigger_settings\n SELECT flag INTO flag_value FROM \"public\"._electric_trigger_settings WHERE namespace = 'public' AND tablename = 'tmp_pictures';\n\n IF flag_value = 1 THEN\n -- Insert into _electric_oplog\n INSERT INTO \"public\"._electric_oplog (namespace, tablename, optype, \"primaryKey\", \"newRow\", \"oldRow\", timestamp)\n VALUES (\n 'public',\n 'tmp_pictures',\n 'UPDATE',\n json_strip_nulls(json_build_object('id', new.\"id\")),\n jsonb_build_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'reportId', new.\"reportId\"),\n jsonb_build_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'reportId', old.\"reportId\"),\n NULL\n );\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER update_public_tmp_pictures_into_oplog\n AFTER UPDATE ON \"public\".\"tmp_pictures\"\n FOR EACH ROW\n EXECUTE FUNCTION update_public_tmp_pictures_into_oplog_function();",
"DROP TRIGGER IF EXISTS delete_public_tmp_pictures_into_oplog ON \"public\".\"tmp_pictures\";",
" CREATE OR REPLACE FUNCTION delete_public_tmp_pictures_into_oplog_function()\n RETURNS TRIGGER AS $$\n BEGIN\n DECLARE\n flag_value INTEGER;\n BEGIN\n -- Get the flag value from _electric_trigger_settings\n SELECT flag INTO flag_value FROM \"public\"._electric_trigger_settings WHERE namespace = 'public' AND tablename = 'tmp_pictures';\n\n IF flag_value = 1 THEN\n -- Insert into _electric_oplog\n INSERT INTO \"public\"._electric_oplog (namespace, tablename, optype, \"primaryKey\", \"newRow\", \"oldRow\", timestamp)\n VALUES (\n 'public',\n 'tmp_pictures',\n 'DELETE',\n json_strip_nulls(json_build_object('id', old.\"id\")),\n NULL,\n jsonb_build_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'reportId', old.\"reportId\"),\n NULL\n );\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER delete_public_tmp_pictures_into_oplog\n AFTER DELETE ON \"public\".\"tmp_pictures\"\n FOR EACH ROW\n EXECUTE FUNCTION delete_public_tmp_pictures_into_oplog_function();",
"DROP TRIGGER IF EXISTS compensation_insert_public_tmp_pictures_reportId_into_oplog ON \"public\".\"tmp_pictures\";",
" CREATE OR REPLACE FUNCTION compensation_insert_public_tmp_pictures_reportId_into_oplog_function()\n RETURNS TRIGGER AS $$\n BEGIN\n DECLARE\n flag_value INTEGER;\n meta_value INTEGER;\n BEGIN\n SELECT flag INTO flag_value FROM \"public\"._electric_trigger_settings WHERE namespace = 'public' AND tablename = 'tmp_pictures';\n\n SELECT value INTO meta_value FROM \"public\"._electric_meta WHERE key = 'compensations';\n\n IF flag_value = 1 AND meta_value = 1 THEN\n INSERT INTO \"public\"._electric_oplog (namespace, tablename, optype, \"primaryKey\", \"newRow\", \"oldRow\", timestamp)\n SELECT\n 'public',\n 'report',\n 'COMPENSATION',\n json_strip_nulls(json_strip_nulls(json_build_object('id', \"id\"))),\n jsonb_build_object('id', \"id\"),\n NULL,\n NULL\n FROM \"public\".\"report\"\n WHERE \"id\" = NEW.\"reportId\";\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER compensation_insert_public_tmp_pictures_reportId_into_oplog\n AFTER INSERT ON \"public\".\"tmp_pictures\"\n FOR EACH ROW\n EXECUTE FUNCTION compensation_insert_public_tmp_pictures_reportId_into_oplog_function();",
"DROP TRIGGER IF EXISTS compensation_update_public_tmp_pictures_reportId_into_oplog ON \"public\".\"tmp_pictures\";",
" CREATE OR REPLACE FUNCTION compensation_update_public_tmp_pictures_reportId_into_oplog_function()\n RETURNS TRIGGER AS $$\n BEGIN\n DECLARE\n flag_value INTEGER;\n meta_value INTEGER;\n BEGIN\n SELECT flag INTO flag_value FROM \"public\"._electric_trigger_settings WHERE namespace = 'public' AND tablename = 'tmp_pictures';\n\n SELECT value INTO meta_value FROM \"public\"._electric_meta WHERE key = 'compensations';\n\n IF flag_value = 1 AND meta_value = 1 THEN\n INSERT INTO \"public\"._electric_oplog (namespace, tablename, optype, \"primaryKey\", \"newRow\", \"oldRow\", timestamp)\n SELECT\n 'public',\n 'report',\n 'COMPENSATION',\n json_strip_nulls(json_strip_nulls(json_build_object('id', \"id\"))),\n jsonb_build_object('id', \"id\"),\n NULL,\n NULL\n FROM \"public\".\"report\"\n WHERE \"id\" = NEW.\"reportId\";\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER compensation_update_public_tmp_pictures_reportId_into_oplog\n AFTER UPDATE ON \"public\".\"tmp_pictures\"\n FOR EACH ROW\n EXECUTE FUNCTION compensation_update_public_tmp_pictures_reportId_into_oplog_function();"
],
"version": "908"
}
]
Loading

0 comments on commit 125ad0d

Please sign in to comment.