Skip to content

Commit

Permalink
cvr -> client_view
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Nov 30, 2023
1 parent 5533cd0 commit 4f9e1a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
15 changes: 8 additions & 7 deletions server/src/pull/cvr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getCVR(
order: number,
): Promise<CVR | undefined> {
const result = await executor(
/*sql*/ `SELECT "client_version" FROM "cvr" WHERE "client_group_id" = $1 AND "order" = $2`,
/*sql*/ `SELECT "client_version" FROM "client_view" WHERE "client_group_id" = $1 AND "order" = $2`,
[clientGroupID, order],
);
if (result.rowCount === 0) {
Expand Down Expand Up @@ -65,18 +65,19 @@ export function findUnsentItems(
limit: number,
) {
// sql = /*sql*/ `SELECT * FROM "${table}" t WHERE NOT EXISTS (
// SELECT 1 FROM "cvr_entry" WHERE "cvr_entry"."row_id" = t."id" AND
// SELECT 1 FROM "client_view_entry" WHERE "cvr_entry"."row_id" = t."id" AND
// "cvr_entry"."row_version" = t."version" AND
// "cvr_entry"."client_group_id" = $1 AND
// "cvr_entry"."order" <= $2 AND
// "cvr_entry"."tbl" = $3
// ) LIMIT $4`;
// The below query runs in ~40ms for issues vs the above takes 16 seconds for issues.
// TODO: test EXCEPT
const sql = /*sql*/ `SELECT *
FROM "${table}" t
WHERE (t."id", t."version") NOT IN (
SELECT "row_id", "row_version"
FROM "cvr_entry"
FROM "client_view_entry"
WHERE "client_group_id" = $1
AND "order" <= $2
AND "tbl" = $3
Expand Down Expand Up @@ -107,14 +108,14 @@ export function findDeletions(
// current CVR rather than next CVR. If a request comes in for that prior CVR,
// we return the stored delete records and do not compute deletes.
return executor(
/*sql*/ `SELECT "row_id" FROM "cvr_entry"
/*sql*/ `SELECT "row_id" FROM "client_view_entry"
WHERE "cvr_entry"."tbl" = $1 AND NOT EXISTS (
SELECT 1 FROM "${table}" WHERE id = "cvr_entry"."row_id"
) AND
"cvr_entry"."client_group_id" = $2 AND
"cvr_entry"."order" <= $3
AND NOT EXISTS (
SELECT 1 FROM "cvr_delete_entry" WHERE "cvr_delete_entry"."tbl" = $1 AND "cvr_delete_entry"."row_id" = "cvr_entry"."row_id"
SELECT 1 FROM "client_view_delete_entry" WHERE "cvr_delete_entry"."tbl" = $1 AND "cvr_delete_entry"."row_id" = "cvr_entry"."row_id"
AND "cvr_delete_entry"."client_group_id" = $2 AND "cvr_delete_entry"."order" <= $3
) LIMIT $4`,
[TableOrdinal[table], clientGroupID, order, limit],
Expand All @@ -128,11 +129,11 @@ export async function dropCVREntries(
) {
await Promise.all([
executor(
/*sql*/ `DELETE FROM "cvr_entry" WHERE "client_group_id" = $1 AND "order" > $2`,
/*sql*/ `DELETE FROM "client_view_entry" WHERE "client_group_id" = $1 AND "order" > $2`,
[clientGroupID, order],
),
executor(
/*sql*/ `DELETE FROM "cvr_delete_entry" WHERE "client_group_id" = $1 AND "order" > $2`,
/*sql*/ `DELETE FROM "client_view_delete_entry" WHERE "client_group_id" = $1 AND "order" > $2`,
[clientGroupID, order],
),
]);
Expand Down
6 changes: 3 additions & 3 deletions server/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export async function createSchemaVersion1(executor: Executor) {
"version" INTEGER NOT NULL
)`);

await executor(/*sql*/ `CREATE TABLE "cvr" (
await executor(/*sql*/ `CREATE TABLE "client_view" (
"client_group_id" VARCHAR(36) NOT NULL,
"order" INTEGER NOT NULL,
"client_version" INTEGER NOT NULL,
PRIMARY KEY ("client_group_id", "order")
)`);

await executor(/*sql*/ `CREATE TABLE "cvr_entry" (
await executor(/*sql*/ `CREATE TABLE "client_view_entry" (
"client_group_id" VARCHAR(36) NOT NULL,
"order" INTEGER NOT NULL,
"tbl" INTEGER NOT NULL,
Expand All @@ -89,7 +89,7 @@ export async function createSchemaVersion1(executor: Executor) {
PRIMARY KEY ("client_group_id", "tbl", "row_id")
)`);

await executor(/*sql*/ `CREATE TABLE "cvr_delete_entry" (
await executor(/*sql*/ `CREATE TABLE "client_view_delete_entry" (
"client_group_id" VARCHAR(36) NOT NULL,
"order" INTEGER NOT NULL,
"tbl" INTEGER NOT NULL,
Expand Down

0 comments on commit 4f9e1a7

Please sign in to comment.