Skip to content

Commit

Permalink
Fix trigger name (#1171)
Browse files Browse the repository at this point in the history
* fix oversize notification trigger name

* drop
  • Loading branch information
jiqiang90 authored Jul 7, 2022
1 parent ad602a9 commit 4b9d688
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/node/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
addTagsToForeignKeyMap,
createExcludeConstraintQuery,
BTREE_GIST_EXTENSION_EXIST_QUERY,
makeTriggerName,
} from '../utils/sync-helper';
import { getYargsOption } from '../yargs';
import {
Expand Down Expand Up @@ -211,7 +212,7 @@ export class StoreService {
);
}
if (argv.subscription) {
const triggerName = `${schema}_${sequelizeModel.tableName}_notify_trigger`;
const triggerName = makeTriggerName(schema, sequelizeModel.tableName);
const triggers = await this.sequelize.query(getNotifyTriggers(), {
replacements: { triggerName },
type: QueryTypes.SELECT,
Expand Down
12 changes: 10 additions & 2 deletions packages/node/src/utils/sync-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2022 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { blake2AsHex } from '@polkadot/util-crypto';
import { underscoredIf } from 'sequelize/lib/utils';

export interface SmartTags {
Expand Down Expand Up @@ -135,7 +136,8 @@ END;
$$ LANGUAGE plpgsql;`;

export function dropNotifyTrigger(schema: string, table: string): string {
return `DROP TRIGGER IF EXISTS "${schema}_${table}_notify_trigger"
const triggerName = makeTriggerName(schema, table);
return `DROP TRIGGER IF EXISTS "${triggerName}"
ON "${schema}"."${table}";`;
}

Expand All @@ -144,9 +146,15 @@ export function getNotifyTriggers(): string {
WHERE trigger_name = :triggerName`;
}
export function createNotifyTrigger(schema: string, table: string): string {
const triggerName = makeTriggerName(schema, table);
return `
CREATE TRIGGER "${schema}_${table}_notify_trigger"
CREATE TRIGGER "${triggerName}"
AFTER INSERT OR UPDATE OR DELETE
ON "${schema}"."${table}"
FOR EACH ROW EXECUTE FUNCTION send_notification();`;
}

export function makeTriggerName(schema: string, tableName: string): string {
// max name length is 63 bytes in Postgres
return blake2AsHex(`${schema}_${tableName}_notify_trigger`).substr(2, 10);
}

0 comments on commit 4b9d688

Please sign in to comment.