From 45fdd0176faf0d71815f7372c60f086076054f11 Mon Sep 17 00:00:00 2001 From: Jay Ji Date: Wed, 10 Jul 2024 11:03:17 +1200 Subject: [PATCH] Fix admin api db size (#2481) * Fix admin api db size * add test * update test --- packages/node-core/CHANGELOG.md | 3 ++ packages/node-core/src/db/sync-helper.test.ts | 45 +++++++++++++++++++ packages/node-core/src/db/sync-helper.ts | 24 +++++----- packages/node/CHANGELOG.md | 3 ++ 4 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 packages/node-core/src/db/sync-helper.test.ts diff --git a/packages/node-core/CHANGELOG.md b/packages/node-core/CHANGELOG.md index ef33ac214a..5e52030cf9 100644 --- a/packages/node-core/CHANGELOG.md +++ b/packages/node-core/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- Fix issue admin api can not get `dbSize` due to it not been set in _metadata table + ## [10.10.1] - 2024-07-09 ### Added - Enable ts strict setting diff --git a/packages/node-core/src/db/sync-helper.test.ts b/packages/node-core/src/db/sync-helper.test.ts new file mode 100644 index 0000000000..588b33849a --- /dev/null +++ b/packages/node-core/src/db/sync-helper.test.ts @@ -0,0 +1,45 @@ +// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors +// SPDX-License-Identifier: GPL-3.0 + +import {INestApplication} from '@nestjs/common'; +import {Test} from '@nestjs/testing'; +import {getDbSizeAndUpdateMetadata} from '@subql/node-core'; +import {Sequelize} from '@subql/x-sequelize'; +import {NodeConfig} from '../configure/NodeConfig'; +import {DbModule} from './db.module'; + +const nodeConfig = new NodeConfig({subquery: 'packages/node-core/test/v1.0.0', subqueryName: 'test'}); + +describe('sync helper test', () => { + let app: INestApplication; + let sequelize: Sequelize; + let schema: string; + + afterEach(async () => { + await sequelize.dropSchema(schema, {}); + await sequelize.close(); + return app?.close(); + }); + + it('can check project db size', async () => { + const module = await Test.createTestingModule({ + imports: [DbModule.forRootWithConfig(nodeConfig)], + }).compile(); + + app = module.createNestApplication(); + await app.init(); + sequelize = app.get(Sequelize); + schema = 'admin-test'; + await sequelize.createSchema(schema, {}); + // mock create metadata table + await sequelize.query(` + CREATE TABLE IF NOT EXISTS "${schema}"._metadata ( + key VARCHAR(255) NOT NULL PRIMARY KEY, + value JSONB, + "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, + "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL + )`); + const dbSize = await getDbSizeAndUpdateMetadata(sequelize, schema); + expect(dbSize).not.toBeUndefined(); + }, 50000); +}); diff --git a/packages/node-core/src/db/sync-helper.ts b/packages/node-core/src/db/sync-helper.ts index dcbe03249a..2545ac12e3 100644 --- a/packages/node-core/src/db/sync-helper.ts +++ b/packages/node-core/src/db/sync-helper.ts @@ -280,20 +280,20 @@ export async function getDbSizeAndUpdateMetadata(sequelize: Sequelize, schema: s const [result] = ( await sequelize.query( ` - WITH schema_size AS ( - SELECT sum(pg_total_relation_size(quote_ident(schemaname) || '.' || quote_ident(tablename)))::bigint AS size - FROM pg_tables - WHERE schemaname = :schema - ) - UPDATE "${schema}"._metadata - SET value = to_jsonb((SELECT size FROM schema_size)), - "updatedAt" = now() - WHERE key = 'dbSize' - RETURNING (SELECT size FROM schema_size) AS size; - `, + WITH schema_size AS ( + SELECT sum(pg_total_relation_size(quote_ident(schemaname) || '.' || quote_ident(tablename)))::bigint AS size + FROM pg_tables + WHERE schemaname = :schema + ) + INSERT INTO "${schema}"._metadata (key, value, "createdAt", "updatedAt") + VALUES ('dbSize', to_jsonb((SELECT size FROM schema_size)), now(), now()) + ON CONFLICT (key) + DO UPDATE SET value = to_jsonb((SELECT size FROM schema_size)), "updatedAt" = now() + RETURNING (SELECT size FROM schema_size) AS size; + `, { replacements: {schema}, - type: QueryTypes.UPDATE, + type: QueryTypes.INSERT, } ) )[0] as unknown as {size: number}[]; diff --git a/packages/node/CHANGELOG.md b/packages/node/CHANGELOG.md index 65b8f195be..e24dc257b9 100644 --- a/packages/node/CHANGELOG.md +++ b/packages/node/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- Bump with `@subql/node-core`, fix admin api `dbSize` issue + ## [4.7.1] - 2024-07-09 ### Added - Enable ts strict model