Skip to content

Commit

Permalink
fix unsubscribe disables triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed May 16, 2024
1 parent e002e97 commit bdf02a6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
8 changes: 5 additions & 3 deletions lib/PubSubManager/getInitQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ BEGIN
)
||
COALESCE((
SELECT ', ' || string_agg(format(E' %I AS ( \n %s \n ) ', related_view_name, related_view_def), ', ')
SELECT ', ' || string_agg(format(E' %s AS ( \n %s \n ) ', related_view_name, related_view_def), ', ')
FROM (
SELECT DISTINCT related_view_name, related_view_def
FROM prostgles.v_triggers
Expand Down Expand Up @@ -356,7 +356,9 @@ BEGIN
--RAISE NOTICE 'DELETE trigger_add_remove_func table: % ', ' ' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM prostgles.app_triggers) , ' 0 ');
--RAISE NOTICE 'DELETE trigger_add_remove_func old_table: % ', '' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM old_table), ' 0 ');
select count(*) from old_table into changed_triggers_count;
SELECT count(*)
FROM old_table
INTO changed_triggers_count;
/* Disable actual triggers if needed */
FOR trw IN
Expand Down Expand Up @@ -427,7 +429,7 @@ BEGIN
'prostgles_triggers_' || trw.table_name || '_update',
'prostgles_triggers_' || trw.table_name || '_delete'
)
) = 3 AND false
) = 3
THEN
query := concat_ws(E'\n',
format(' ALTER TABLE %s ENABLE TRIGGER %I ;', trw.table_name, 'prostgles_triggers_' || trw.table_name || '_insert'),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.65",
"version": "4.2.66",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
30 changes: 24 additions & 6 deletions tests/isomorphicQueries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
//@ts-ignore
describe
} from "node:test";
import { pickKeys } from "prostgles-types";
import { SubscriptionHandler, pickKeys } from "prostgles-types";


export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log: (msg: string, extra?: any) => void) => {
Expand Down Expand Up @@ -466,6 +466,27 @@ export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log
assert.equal(+res, 1)
});


const testToEnsureTriggersAreDisabled = async (sub: SubscriptionHandler, table_name: string) => {
const getTableTriggers = async (table_name: string) => {
return await db.sql?.(`
SELECT tgname, tgenabled = 'O' as enabled
FROM pg_catalog.pg_trigger
WHERE tgname like format('prostgles_triggers_%s_', \${table_name}) || '%'
`,
{ table_name },
{ returnType: "rows" }
) as { tgname: string; enabled: boolean; }[];
}
await sub.unsubscribe();
let validTriggers = await getTableTriggers(table_name)
assert.equal(validTriggers.filter(t => t.enabled).length, 3);
await db.sql?.(`DELETE FROM prostgles.app_triggers`, []); // WHERE table_name = $1
validTriggers = await getTableTriggers(table_name)
assert.equal(validTriggers.length, 3);
assert.equal(validTriggers.filter(t => t.enabled).length, 0);
}

await test("subscribe", async () => {
await tryRunP("subscribe", async (resolve, reject) => {
await db.various.insert!({ id: 99 });
Expand All @@ -474,7 +495,7 @@ export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log

if(item && item.name === "zz3zz3"){
await db.various.delete!({ name: "zz3zz3" });
await sub.unsubscribe();
await testToEnsureTriggersAreDisabled(sub, "various");
resolve(true)
}
});
Expand All @@ -490,9 +511,6 @@ export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log
await db.various.insert!({ id: 99 });
const start = Date.now(); // name: "zz3zz"
const sub = await db.various.subscribeOne!({ id: 99 }, { throttle: 1700 }, async item => {
// const item = items[0]
// console.log(item)

const now = Date.now();
if(item && item.name === "zz3zz2" && now - start > 1600 && now - start < 1800){
await db.various.delete!({ name: "zz3zz2" });
Expand Down Expand Up @@ -1005,7 +1023,7 @@ export const isomorphicQueries = async (db: DBOFullyTyped | DBHandlerClient, log
if(runs < 2){
return;
}
await sub.unsubscribe();
await testToEnsureTriggersAreDisabled(sub, `"""quoted0"""`);
resolve(true);
}
});
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bdf02a6

Please sign in to comment.