Skip to content

Commit

Permalink
Fix subscription drop queries (#2277)
Browse files Browse the repository at this point in the history
* prioritize trigger drop first, remove duplicated drop function queries

* update changelog

* add test for drop notificaiton functions and triggers

* add comments to test

* update extraQueries to be all unique
  • Loading branch information
bz888 authored Feb 28, 2024
1 parent 0e5ddff commit 3c97cb3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
- `scale-batch-size` flag as it had no use (#2275)

### Fixed
- Drop subscription triggers and notifiy_functions (#2277)

## [7.3.0] - 2024-02-23
### Added
- Schema Migration support for Enums, Relations, Subscription (#2251)
Expand Down
7 changes: 4 additions & 3 deletions packages/node-core/src/db/migration-service/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Transaction,
Utils,
} from '@subql/x-sequelize';
import {isEqual} from 'lodash';
import {isEqual, uniq} from 'lodash';
import {NodeConfig} from '../../configure/NodeConfig';
import {StoreService} from '../../indexer';
import {getLogger} from '../../logger';
Expand Down Expand Up @@ -113,7 +113,7 @@ export class Migration {
await this.sequelize.query(query, {transaction: effectiveTransaction});
}

for (const query of this.extraQueries) {
for (const query of uniq(this.extraQueries)) {
await this.sequelize.query(query, {transaction: effectiveTransaction});
}

Expand Down Expand Up @@ -202,7 +202,8 @@ export class Migration {
} else {
//TODO: DROP TRIGGER IF EXIST is not valid syntax for cockroach, better check trigger exist at first.
if (this.dbType !== SUPPORT_DB.cockRoach) {
this.extraQueries.push(syncHelper.dropNotifyTrigger(this.schemaName, sequelizeModel.tableName));
// trigger drop should be prioritized
this.extraQueries.unshift(syncHelper.dropNotifyTrigger(this.schemaName, sequelizeModel.tableName));
}
}

Expand Down
61 changes: 60 additions & 1 deletion packages/node/src/indexer/store.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

import { promisify } from 'util';
import { INestApplication } from '@nestjs/common';
import { DbOption } from '@subql/node-core';
import {
createNotifyTrigger,
createSendNotificationTriggerFunction,
DbOption,
getFunctions,
getTriggers,
} from '@subql/node-core';
import { QueryTypes, Sequelize } from '@subql/x-sequelize';
import rimraf from 'rimraf';
import { prepareApp } from '../utils/test.utils';
Expand Down Expand Up @@ -289,4 +295,57 @@ ORDER BY t.typname, e.enumsortorder;`,
['65c7fd4e5d', '65c7fd4e5d', '65c7fd4e5d'],
);
});
it('Able to drop notification triggers and functions', async () => {
// if subscription is no longer enabled should be able to drop all prior triggers and functions related to subscription
const cid = 'Qma3HraGKnH5Gte2WVs4sAAY6z5nBSqVuVq7Ef3eVQQPvz';
schemaName = 'sync-schema-5';

// simulate start with subscription then without subscription
const initQueries = [
`CREATE SCHEMA IF NOT EXISTS "${schemaName}";`,
` CREATE TABLE IF NOT EXISTS "${schemaName}"."transfers" ("id" text NOT NULL,
"amount" numeric NOT NULL,
"block_number" integer NOT NULL,
"date" timestamp NOT NULL,
"from_id" text NOT NULL,
"to_id" text NOT NULL,
"_id" UUID NOT NULL,
"_block_range" int8range NOT NULL, PRIMARY KEY ("_id"));`,
`CREATE TABLE IF NOT EXISTS "${schemaName}"."accounts" ("id" text NOT NULL,
"public_key" text NOT NULL,
"first_transfer_block" integer,
"last_transfer_block" integer,
"_id" UUID NOT NULL,
"_block_range" int8range NOT NULL, PRIMARY KEY ("_id"));`,
createSendNotificationTriggerFunction(schemaName),
createNotifyTrigger(schemaName, 'transfers'),
createNotifyTrigger(schemaName, 'accounts'),
];
for (const q of initQueries) {
await sequelize.query(q);
}

app = await prepareApp(schemaName, cid, false, false);

projectService = app.get('IProjectService');
const apiService = app.get(ApiService);

await apiService.init();
await projectService.init(1);

tempDir = (projectService as any).project.root;

const accountTrigger = await getTriggers(sequelize, '0xe2ae28f317df40c5');
const transferTrigger = await getTriggers(sequelize, '0x2416ab4b88a0cdac');

const functions = await getFunctions(
sequelize,
schemaName,
'send_notification',
);

expect(accountTrigger.length).toBe(0);
expect(transferTrigger.length).toBe(0);
expect(functions.length).toBe(0);
});
});

0 comments on commit 3c97cb3

Please sign in to comment.