Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hot schema #1404

Merged
merged 2 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/node-core/src/indexer/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,19 @@ export class StoreService {

/* These SQL queries are to allow hot-schema reload on query service */
extraQueries.push(createSchemaTriggerFunction(schema));
extraQueries.push(createSchemaTrigger(schema));
const schemaTriggerName = makeTriggerName(schema, '_metadata', 'schema');

const schemaNotifyTrigger = await this.sequelize.query(getNotifyTriggers(), {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe rename getNotifyTriggers() to getTriggers(), since both notify and schema trigger are using it

replacements: {triggerName: `${schemaTriggerName}`},
type: QueryTypes.SELECT,
});

if (schemaNotifyTrigger.length === 0) {
extraQueries.push(createSchemaTrigger(schema));
}

if (this.config.subscription) {
const triggerName = makeTriggerName(schema, sequelizeModel.tableName);
const triggerName = makeTriggerName(schema, sequelizeModel.tableName, 'notify');
const triggers = await this.sequelize.query(getNotifyTriggers(), {
replacements: {triggerName},
type: QueryTypes.SELECT,
Expand Down
11 changes: 6 additions & 5 deletions packages/node-core/src/utils/sync-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ END;
$$ LANGUAGE plpgsql;`;

export function dropNotifyTrigger(schema: string, table: string): string {
const triggerName = makeTriggerName(schema, table);
const triggerName = makeTriggerName(schema, table, 'notify');
return `DROP TRIGGER IF EXISTS "${triggerName}"
ON "${schema}"."${table}";`;
}
Expand All @@ -132,22 +132,23 @@ export function getNotifyTriggers(): string {
WHERE trigger_name = :triggerName`;
}
export function createNotifyTrigger(schema: string, table: string): string {
const triggerName = makeTriggerName(schema, table);
const triggerName = makeTriggerName(schema, table, 'notify');
return `
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 {
export function makeTriggerName(schema: string, tableName: string, triggerType: string): string {
// max name length is 63 bytes in Postgres
return blake2AsHex(`${schema}_${tableName}_notify_trigger`).substr(2, 10);
return blake2AsHex(`${schema}_${tableName}_${triggerType}_trigger`).substr(2, 10);
}

export function createSchemaTrigger(schema: string): string {
const triggerName = makeTriggerName(schema, '_metadata', 'schema');
return `
CREATE OR REPLACE TRIGGER "${schema}_metadata_schema_trigger"
CREATE TRIGGER "${triggerName}"
AFTER UPDATE
ON "${schema}"."_metadata"
FOR EACH ROW
Expand Down