Skip to content

Commit

Permalink
feat: add picture_lines in db and save lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Nov 12, 2024
1 parent 6332499 commit 0c47394
Show file tree
Hide file tree
Showing 8 changed files with 2,413 additions and 149 deletions.
8 changes: 8 additions & 0 deletions db/migrations/909-add_picture_lines.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE picture_lines (
id TEXT PRIMARY KEY,
"pictureId" TEXT REFERENCES pictures(id) ON DELETE CASCADE,
lines TEXT NOT NULL,
"createdAt" TIMESTAMP
);

ALTER TABLE picture_lines ENABLE ELECTRIC;
541 changes: 524 additions & 17 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 @@ -253,5 +253,24 @@ export default [
"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"
},
{
"statements": [
"CREATE TABLE \"picture_lines\" (\n \"id\" TEXT NOT NULL,\n \"pictureId\" TEXT,\n \"lines\" TEXT NOT NULL,\n \"createdAt\" TEXT,\n CONSTRAINT \"picture_lines_pictureId_fkey\" FOREIGN KEY (\"pictureId\") REFERENCES \"pictures\" (\"id\") ON DELETE CASCADE,\n CONSTRAINT \"picture_lines_pkey\" PRIMARY KEY (\"id\")\n) WITHOUT ROWID;\n",
"INSERT OR IGNORE INTO _electric_trigger_settings (namespace, tablename, flag) VALUES ('main', 'picture_lines', 1);",
"DROP TRIGGER IF EXISTS update_ensure_main_picture_lines_primarykey;",
"CREATE TRIGGER update_ensure_main_picture_lines_primarykey\n BEFORE UPDATE ON \"main\".\"picture_lines\"\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_picture_lines_into_oplog;",
"CREATE TRIGGER insert_main_picture_lines_into_oplog\n AFTER INSERT ON \"main\".\"picture_lines\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'picture_lines')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'picture_lines', 'INSERT', json_patch('{}', json_object('id', new.\"id\")), json_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'lines', new.\"lines\", 'pictureId', new.\"pictureId\"), NULL, NULL);\nEND;",
"DROP TRIGGER IF EXISTS update_main_picture_lines_into_oplog;",
"CREATE TRIGGER update_main_picture_lines_into_oplog\n AFTER UPDATE ON \"main\".\"picture_lines\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'picture_lines')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'picture_lines', 'UPDATE', json_patch('{}', json_object('id', new.\"id\")), json_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'lines', new.\"lines\", 'pictureId', new.\"pictureId\"), json_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'lines', old.\"lines\", 'pictureId', old.\"pictureId\"), NULL);\nEND;",
"DROP TRIGGER IF EXISTS delete_main_picture_lines_into_oplog;",
"CREATE TRIGGER delete_main_picture_lines_into_oplog\n AFTER DELETE ON \"main\".\"picture_lines\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'picture_lines')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'picture_lines', 'DELETE', json_patch('{}', json_object('id', old.\"id\")), NULL, json_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'lines', old.\"lines\", 'pictureId', old.\"pictureId\"), NULL);\nEND;",
"DROP TRIGGER IF EXISTS compensation_insert_main_picture_lines_pictureId_into_oplog;",
"CREATE TRIGGER compensation_insert_main_picture_lines_pictureId_into_oplog\n AFTER INSERT ON \"main\".\"picture_lines\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'picture_lines') 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', 'pictures', 'COMPENSATION', json_patch('{}', json_object('id', \"id\")), json_object('id', \"id\"), NULL, NULL\n FROM \"main\".\"pictures\" WHERE \"id\" = new.\"pictureId\";\nEND;",
"DROP TRIGGER IF EXISTS compensation_update_main_picture_lines_pictureId_into_oplog;",
"CREATE TRIGGER compensation_update_main_picture_lines_pictureId_into_oplog\n AFTER UPDATE ON \"main\".\"picture_lines\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'picture_lines') 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', 'pictures', 'COMPENSATION', json_patch('{}', json_object('id', \"id\")), json_object('id', \"id\"), NULL, NULL\n FROM \"main\".\"pictures\" WHERE \"id\" = new.\"pictureId\";\nEND;"
],
"version": "909"
}
]
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 @@ -333,5 +333,30 @@ export default [
"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"
},
{
"statements": [
"CREATE TABLE picture_lines (\n id text NOT NULL,\n \"pictureId\" text,\n lines text NOT NULL,\n \"createdAt\" timestamp without time zone,\n CONSTRAINT picture_lines_pkey PRIMARY KEY (id),\n CONSTRAINT \"picture_lines_pictureId_fkey\" FOREIGN KEY (\"pictureId\") REFERENCES pictures(id) ON DELETE CASCADE\n)",
"INSERT INTO \"public\".\"_electric_trigger_settings\" (\"namespace\", \"tablename\", \"flag\")\n VALUES ('public', 'picture_lines', 1)\n ON CONFLICT DO NOTHING;",
"DROP TRIGGER IF EXISTS update_ensure_public_picture_lines_primarykey ON \"public\".\"picture_lines\";",
"CREATE OR REPLACE FUNCTION update_ensure_public_picture_lines_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_picture_lines_primarykey\n BEFORE UPDATE ON \"public\".\"picture_lines\"\n FOR EACH ROW\n EXECUTE FUNCTION update_ensure_public_picture_lines_primarykey_function();",
"DROP TRIGGER IF EXISTS insert_public_picture_lines_into_oplog ON \"public\".\"picture_lines\";",
" CREATE OR REPLACE FUNCTION insert_public_picture_lines_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 = 'picture_lines';\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 'picture_lines',\n 'INSERT',\n json_strip_nulls(json_build_object('id', new.\"id\")),\n jsonb_build_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'lines', new.\"lines\", 'pictureId', new.\"pictureId\"),\n NULL,\n NULL\n );\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER insert_public_picture_lines_into_oplog\n AFTER INSERT ON \"public\".\"picture_lines\"\n FOR EACH ROW\n EXECUTE FUNCTION insert_public_picture_lines_into_oplog_function();",
"DROP TRIGGER IF EXISTS update_public_picture_lines_into_oplog ON \"public\".\"picture_lines\";",
" CREATE OR REPLACE FUNCTION update_public_picture_lines_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 = 'picture_lines';\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 'picture_lines',\n 'UPDATE',\n json_strip_nulls(json_build_object('id', new.\"id\")),\n jsonb_build_object('createdAt', new.\"createdAt\", 'id', new.\"id\", 'lines', new.\"lines\", 'pictureId', new.\"pictureId\"),\n jsonb_build_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'lines', old.\"lines\", 'pictureId', old.\"pictureId\"),\n NULL\n );\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER update_public_picture_lines_into_oplog\n AFTER UPDATE ON \"public\".\"picture_lines\"\n FOR EACH ROW\n EXECUTE FUNCTION update_public_picture_lines_into_oplog_function();",
"DROP TRIGGER IF EXISTS delete_public_picture_lines_into_oplog ON \"public\".\"picture_lines\";",
" CREATE OR REPLACE FUNCTION delete_public_picture_lines_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 = 'picture_lines';\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 'picture_lines',\n 'DELETE',\n json_strip_nulls(json_build_object('id', old.\"id\")),\n NULL,\n jsonb_build_object('createdAt', old.\"createdAt\", 'id', old.\"id\", 'lines', old.\"lines\", 'pictureId', old.\"pictureId\"),\n NULL\n );\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER delete_public_picture_lines_into_oplog\n AFTER DELETE ON \"public\".\"picture_lines\"\n FOR EACH ROW\n EXECUTE FUNCTION delete_public_picture_lines_into_oplog_function();",
"DROP TRIGGER IF EXISTS compensation_insert_public_picture_lines_pictureId_into_oplog ON \"public\".\"picture_lines\";",
" CREATE OR REPLACE FUNCTION compensation_insert_public_picture_lines_pictureId_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 = 'picture_lines';\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 'pictures',\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\".\"pictures\"\n WHERE \"id\" = NEW.\"pictureId\";\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER compensation_insert_public_picture_lines_pictureId_into_oplog\n AFTER INSERT ON \"public\".\"picture_lines\"\n FOR EACH ROW\n EXECUTE FUNCTION compensation_insert_public_picture_lines_pictureId_into_oplog_function();",
"DROP TRIGGER IF EXISTS compensation_update_public_picture_lines_pictureId_into_oplog ON \"public\".\"picture_lines\";",
" CREATE OR REPLACE FUNCTION compensation_update_public_picture_lines_pictureId_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 = 'picture_lines';\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 'pictures',\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\".\"pictures\"\n WHERE \"id\" = NEW.\"pictureId\";\n END IF;\n\n RETURN NEW;\n END;\n END;\n $$ LANGUAGE plpgsql;",
"CREATE TRIGGER compensation_update_public_picture_lines_pictureId_into_oplog\n AFTER UPDATE ON \"public\".\"picture_lines\"\n FOR EACH ROW\n EXECUTE FUNCTION compensation_update_public_picture_lines_pictureId_into_oplog_function();"
],
"version": "909"
}
]
Loading

0 comments on commit 0c47394

Please sign in to comment.