Skip to content

Commit

Permalink
feat: add chips v2
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Jul 2, 2024
1 parent 7b8f19f commit 1629ad9
Show file tree
Hide file tree
Showing 18 changed files with 1,946 additions and 389 deletions.
1 change: 1 addition & 0 deletions db/migrations/04-add_chips_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CREATE TABLE clause(
,value text NOT NULL
,udap_id text
,text text NOT NULL
,hidden boolean
, PRIMARY KEY (key, value, udap_id)
);

Expand Down
10 changes: 10 additions & 0 deletions db/migrations/902-rework_clauses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

CREATE TABLE clause_v2 (
id text NOT NULL PRIMARY KEY
,key text NOT NULL
,value text NOT NULL
,udap_id text
,text text NOT NULL
);

ALTER TABLE clause_v2 ENABLE ELECTRIC;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"db:migrate": "pnpm db:create",
"electric:up": "pnpm electric-sql with-config \"pnpm pg-migrations apply --database {{ELECTRIC_PROXY}} --ignore-error migration_file_edited --directory ./db/migrations\"",
"show-config": "pnpm electric-sql show-config",
"electric:migrate": "pnpm electric:up && pnpm electric-client generate:front",
"electric:migrate": "pnpm electric:up && pnpm electric-client generate:front && pnpm electric-client generate:back",
"client:json": "pnpm backend dev --create-only",
"client:ts": "typed-openapi ./packages/backend/openapi.json --output ./packages/frontend/src/api.gen.ts",
"client:generate": "pnpm client:json && pnpm client:ts",
Expand Down
3 changes: 2 additions & 1 deletion packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { onHmr, registerViteHmrServerRestart } from "./hmr";
import { ENV } from "./envVars";
import { generateOpenApi, initFastify } from "./router";
import { makeDebug } from "./features/debug";
import { initClauseV2 } from "./tmp";

const debug = makeDebug("index");

const start = async () => {
await registerViteHmrServerRestart();

await initClauseV2();
debug("Starting fastify server in", ENV.NODE_ENV, "mode");

const fastifyInstance = await initFastify();
Expand Down
23 changes: 23 additions & 0 deletions packages/backend/src/tmp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { db } from "./db/db";
import { v4 } from "uuid";

export const initClauseV2 = async () => {
const nbClausesV2 = await db.clause_v2.count();
if (nbClausesV2 > 0) return;

const clauses = await db.clause.findMany();

for (const clause of clauses) {
await db.clause_v2.create({
data: {
id: v4(),
key: clause.key,
text: clause.text,
value: clause.value,
udap_id: clause.udap_id,
},
});
}

return "done";
};
9 changes: 9 additions & 0 deletions packages/electric-client/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ model clause {
value String
udap_id String
text String
hidden Boolean?
@@id([key, value, udap_id])
}
Expand Down Expand Up @@ -121,3 +122,11 @@ model service_instructeurs {
tel String?
udap_id String?
}

model clause_v2 {
id String @id
key String
value String
udap_id String?
text String
}
526 changes: 423 additions & 103 deletions packages/electric-client/src/generated/client/index.ts

Large diffs are not rendered by default.

23 changes: 19 additions & 4 deletions packages/electric-client/src/generated/client/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ export default [
},
{
"statements": [
"CREATE TABLE \"clause\" (\n \"key\" TEXT NOT NULL,\n \"value\" TEXT NOT NULL,\n \"udap_id\" TEXT NOT NULL,\n \"text\" TEXT NOT NULL,\n CONSTRAINT \"clause_pkey\" PRIMARY KEY (\"key\", \"value\", \"udap_id\")\n) WITHOUT ROWID;\n",
"CREATE TABLE \"clause\" (\n \"key\" TEXT NOT NULL,\n \"value\" TEXT NOT NULL,\n \"udap_id\" TEXT NOT NULL,\n \"text\" TEXT NOT NULL,\n \"hidden\" INTEGER,\n CONSTRAINT \"clause_pkey\" PRIMARY KEY (\"key\", \"value\", \"udap_id\")\n) WITHOUT ROWID;\n",
"INSERT OR IGNORE INTO _electric_trigger_settings (namespace, tablename, flag) VALUES ('main', 'clause', 1);",
"DROP TRIGGER IF EXISTS update_ensure_main_clause_primarykey;",
"CREATE TRIGGER update_ensure_main_clause_primarykey\n BEFORE UPDATE ON \"main\".\"clause\"\nBEGIN\n SELECT\n CASE\n WHEN old.\"key\" != new.\"key\" THEN\n \t\tRAISE (ABORT, 'cannot change the value of column key as it belongs to the primary key')\n WHEN old.\"udap_id\" != new.\"udap_id\" THEN\n \t\tRAISE (ABORT, 'cannot change the value of column udap_id as it belongs to the primary key')\n WHEN old.\"value\" != new.\"value\" THEN\n \t\tRAISE (ABORT, 'cannot change the value of column value as it belongs to the primary key')\n END;\nEND;",
"DROP TRIGGER IF EXISTS insert_main_clause_into_oplog;",
"CREATE TRIGGER insert_main_clause_into_oplog\n AFTER INSERT ON \"main\".\"clause\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause', 'INSERT', json_patch('{}', json_object('key', new.\"key\", 'udap_id', new.\"udap_id\", 'value', new.\"value\")), json_object('key', new.\"key\", 'text', new.\"text\", 'udap_id', new.\"udap_id\", 'value', new.\"value\"), NULL, NULL);\nEND;",
"CREATE TRIGGER insert_main_clause_into_oplog\n AFTER INSERT ON \"main\".\"clause\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause', 'INSERT', json_patch('{}', json_object('key', new.\"key\", 'udap_id', new.\"udap_id\", 'value', new.\"value\")), json_object('hidden', new.\"hidden\", 'key', new.\"key\", 'text', new.\"text\", 'udap_id', new.\"udap_id\", 'value', new.\"value\"), NULL, NULL);\nEND;",
"DROP TRIGGER IF EXISTS update_main_clause_into_oplog;",
"CREATE TRIGGER update_main_clause_into_oplog\n AFTER UPDATE ON \"main\".\"clause\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause', 'UPDATE', json_patch('{}', json_object('key', new.\"key\", 'udap_id', new.\"udap_id\", 'value', new.\"value\")), json_object('key', new.\"key\", 'text', new.\"text\", 'udap_id', new.\"udap_id\", 'value', new.\"value\"), json_object('key', old.\"key\", 'text', old.\"text\", 'udap_id', old.\"udap_id\", 'value', old.\"value\"), NULL);\nEND;",
"CREATE TRIGGER update_main_clause_into_oplog\n AFTER UPDATE ON \"main\".\"clause\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause', 'UPDATE', json_patch('{}', json_object('key', new.\"key\", 'udap_id', new.\"udap_id\", 'value', new.\"value\")), json_object('hidden', new.\"hidden\", 'key', new.\"key\", 'text', new.\"text\", 'udap_id', new.\"udap_id\", 'value', new.\"value\"), json_object('hidden', old.\"hidden\", 'key', old.\"key\", 'text', old.\"text\", 'udap_id', old.\"udap_id\", 'value', old.\"value\"), NULL);\nEND;",
"DROP TRIGGER IF EXISTS delete_main_clause_into_oplog;",
"CREATE TRIGGER delete_main_clause_into_oplog\n AFTER DELETE ON \"main\".\"clause\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause', 'DELETE', json_patch('{}', json_object('key', old.\"key\", 'udap_id', old.\"udap_id\", 'value', old.\"value\")), NULL, json_object('key', old.\"key\", 'text', old.\"text\", 'udap_id', old.\"udap_id\", 'value', old.\"value\"), NULL);\nEND;"
"CREATE TRIGGER delete_main_clause_into_oplog\n AFTER DELETE ON \"main\".\"clause\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause', 'DELETE', json_patch('{}', json_object('key', old.\"key\", 'udap_id', old.\"udap_id\", 'value', old.\"value\")), NULL, json_object('hidden', old.\"hidden\", 'key', old.\"key\", 'text', old.\"text\", 'udap_id', old.\"udap_id\", 'value', old.\"value\"), NULL);\nEND;"
],
"version": "4"
},
Expand Down Expand Up @@ -132,5 +132,20 @@ export default [
"CREATE TRIGGER compensation_update_main_report_createdBy_into_oplog\n AFTER UPDATE ON \"main\".\"report\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'report') 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', 'user', 'COMPENSATION', json_patch('{}', json_object('id', \"id\")), json_object('id', \"id\"), NULL, NULL\n FROM \"main\".\"user\" WHERE \"id\" = new.\"createdBy\";\nEND;"
],
"version": "901"
},
{
"statements": [
"CREATE TABLE \"clause_v2\" (\n \"id\" TEXT NOT NULL,\n \"key\" TEXT NOT NULL,\n \"value\" TEXT NOT NULL,\n \"udap_id\" TEXT,\n \"text\" TEXT NOT NULL,\n CONSTRAINT \"clause_v2_pkey\" PRIMARY KEY (\"id\")\n) WITHOUT ROWID;\n",
"INSERT OR IGNORE INTO _electric_trigger_settings (namespace, tablename, flag) VALUES ('main', 'clause_v2', 1);",
"DROP TRIGGER IF EXISTS update_ensure_main_clause_v2_primarykey;",
"CREATE TRIGGER update_ensure_main_clause_v2_primarykey\n BEFORE UPDATE ON \"main\".\"clause_v2\"\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_clause_v2_into_oplog;",
"CREATE TRIGGER insert_main_clause_v2_into_oplog\n AFTER INSERT ON \"main\".\"clause_v2\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause_v2')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause_v2', 'INSERT', json_patch('{}', json_object('id', new.\"id\")), json_object('id', new.\"id\", 'key', new.\"key\", 'text', new.\"text\", 'udap_id', new.\"udap_id\", 'value', new.\"value\"), NULL, NULL);\nEND;",
"DROP TRIGGER IF EXISTS update_main_clause_v2_into_oplog;",
"CREATE TRIGGER update_main_clause_v2_into_oplog\n AFTER UPDATE ON \"main\".\"clause_v2\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause_v2')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause_v2', 'UPDATE', json_patch('{}', json_object('id', new.\"id\")), json_object('id', new.\"id\", 'key', new.\"key\", 'text', new.\"text\", 'udap_id', new.\"udap_id\", 'value', new.\"value\"), json_object('id', old.\"id\", 'key', old.\"key\", 'text', old.\"text\", 'udap_id', old.\"udap_id\", 'value', old.\"value\"), NULL);\nEND;",
"DROP TRIGGER IF EXISTS delete_main_clause_v2_into_oplog;",
"CREATE TRIGGER delete_main_clause_v2_into_oplog\n AFTER DELETE ON \"main\".\"clause_v2\"\n WHEN 1 = (SELECT flag from _electric_trigger_settings WHERE namespace = 'main' AND tablename = 'clause_v2')\nBEGIN\n INSERT INTO _electric_oplog (namespace, tablename, optype, primaryKey, newRow, oldRow, timestamp)\n VALUES ('main', 'clause_v2', 'DELETE', json_patch('{}', json_object('id', old.\"id\")), NULL, json_object('id', old.\"id\", 'key', old.\"key\", 'text', old.\"text\", 'udap_id', old.\"udap_id\", 'value', old.\"value\"), NULL);\nEND;"
],
"version": "902"
}
]
Loading

0 comments on commit 1629ad9

Please sign in to comment.