Skip to content

Commit

Permalink
Fix admin api db size (#2481)
Browse files Browse the repository at this point in the history
* Fix admin api db size

* add test

* update test
  • Loading branch information
jiqiang90 authored Jul 9, 2024
1 parent 0377617 commit 45fdd01
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions packages/node-core/src/db/sync-helper.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
24 changes: 12 additions & 12 deletions packages/node-core/src/db/sync-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}[];
Expand Down
3 changes: 3 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 45fdd01

Please sign in to comment.