Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1837 from LiskHQ/1822-Timeout-when-querying-event…
Browse files Browse the repository at this point in the history
…s-with-blockID

Timeout when querying events with blockID
  • Loading branch information
sameersubudhi authored Sep 13, 2023
2 parents d617f75 + 8a01da5 commit 405e26e
Show file tree
Hide file tree
Showing 35 changed files with 8,567 additions and 7,931 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ build-local:
cd ./services/export && yarn install --frozen-lockfile
cd ./services/template && yarn install --frozen-lockfile
cd ./tests && yarn install --frozen-lockfile

clean: clean-local clean-images

clean-local:
Expand Down
Binary file added framework/dist/lisk-service-framework-1.5.0.tgz
Binary file not shown.
2 changes: 1 addition & 1 deletion framework/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lisk-service-framework",
"version": "1.4.23",
"version": "1.5.0",
"description": "Lisk Service Framework",
"keywords": [
"lisk",
Expand Down
49 changes: 44 additions & 5 deletions framework/src/database/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const escapeUserInput = input => {
};

const loadSchema = async (knex, tableName, tableConfig) => {
const { primaryKey, charset, schema, indexes } = tableConfig;
const { primaryKey, charset, schema, indexes, compositeIndexes } = tableConfig;

if (await knex.schema.hasTable(tableName)) return knex;

Expand All @@ -50,6 +50,18 @@ const loadSchema = async (knex, tableName, tableConfig) => {
throw err;
});

// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const key in compositeIndexes) {
const directions = compositeIndexes[key];
const indexName = `${tableName}_index_${key}`;
const indexColumns = directions.map(dir => `\`${dir.key}\` ${dir.direction}`).join(', ');

const sqlStatement = `ALTER TABLE ${tableName} ADD INDEX ${indexName} (${indexColumns})`;

// eslint-disable-next-line no-await-in-loop
await knex.raw(sqlStatement);
}

return knex;
};

Expand All @@ -66,10 +78,10 @@ const cast = (val, type) => {

const resolveQueryParams = params => {
const KNOWN_QUERY_PARAMS = [
'sort', 'limit', 'offset', 'propBetweens', 'andWhere', 'orWhere', 'orWhereWith',
'whereIn', 'orWhereIn', 'whereJsonSupersetOf', 'search', 'aggregate', 'distinct',
'order', 'orSearch', 'whereNull', 'whereNotNull', 'leftOuterJoin', 'rightOuterJoin',
'innerJoin',
'sort', 'limit', 'offset', 'propBetweens', 'andWhere', 'orWhere', 'orWhereWith', 'whereNot',
'whereIn', 'whereNotIn', 'orWhereIn', 'whereBetween', 'whereJsonSupersetOf', 'search', 'aggregate',
'distinct', 'order', 'orSearch', 'whereNull', 'whereNotNull', 'leftOuterJoin', 'rightOuterJoin',
'innerJoin', 'groupBy', 'orderByRaw',
];
const queryParams = Object.keys(params)
.filter(key => !KNOWN_QUERY_PARAMS.includes(key))
Expand Down Expand Up @@ -163,6 +175,10 @@ const getTableInstance = (tableConfig, knex) => {
query.distinct(distinctParams);
}

if (params.groupBy) {
query.groupBy(params.groupBy);
}

if (params.sort) {
const [sortColumn, sortDirection] = params.sort.split(':');
query.whereNotNull(sortColumn);
Expand All @@ -175,6 +191,14 @@ const getTableInstance = (tableConfig, knex) => {
query.select(orderColumn).orderBy(orderColumn, orderDirection);
}

if (params.orderByRaw) {
params.orderByRaw.forEach(
orderBy => {
const [col] = orderBy.split(' ');
query.select(knex.raw(col)).orderByRaw(orderBy);
});
}

if (params.aggregate) {
query.sum(`${params.aggregate} as total`);
}
Expand Down Expand Up @@ -228,6 +252,21 @@ const getTableInstance = (tableConfig, knex) => {
});
}

if (params.whereNotIn) {
const { column, values } = params.whereNotIn;
query.whereNotIn(column, values);
}

if (params.whereNot) {
const { column, value } = params.whereNot;
query.whereNot(column, value);
}

if (params.whereBetween) {
const { column, values } = params.whereBetween;
query.whereBetween(column, values);
}

if (params.andWhere) {
const { andWhere } = params;
query.where(function () {
Expand Down
96 changes: 96 additions & 0 deletions framework/tests/functional/database/mysql/mysql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ describe('Test MySQL', () => {
expect(result[0].height).toBeGreaterThanOrEqual(result[1].height);
});

it('should order the rows in ascending order based on their height using raw query', async () => {
await blocksTable.upsert([emptyBlock, nonEmptyBlock]);
const result = await blocksTable.find({ orderByRaw: ['height asc'] });
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(2);
expect(result[1].height).toBeGreaterThanOrEqual(result[0].height);
});

it('should order the rows in descending order based on their height using raw query', async () => {
await blocksTable.upsert([emptyBlock, nonEmptyBlock]);
const result = await blocksTable.find({ orderByRaw: ['height desc'] });
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(2);
expect(result[0].height).toBeGreaterThanOrEqual(result[1].height);
});

it('should get row count', async () => {
const count = await blocksTable.count();
expect(count).toBe(2);
Expand Down Expand Up @@ -234,6 +250,45 @@ describe('Test MySQL', () => {
expect(result).toBe(2);
});

it('should get row count using whereNot', async () => {
await blocksTable.upsert([emptyBlock, nonEmptyBlock]);
const params = {
whereNot: {
column: 'id',
value: emptyBlock.id,
},
};
const result = await blocksTable.count(params);
expect(result).toBe(1);
});

it('should get row count using whereNotIn', async () => {
await blocksTable.upsert([emptyBlock, nonEmptyBlock]);
const params = {
whereNotIn: {
column: 'id',
values: [emptyBlock.id, nonEmptyBlock.id],
},
};
const result = await blocksTable.count(params);
expect(result).toBe(0);
});

it('should get row count using whereBetween', async () => {
await blocksTable.upsert([emptyBlock, nonEmptyBlock]);
const params = {
whereBetween: {
column: 'id',
values: [
Math.min(emptyBlock.id, nonEmptyBlock.id),
Math.max(emptyBlock.id, nonEmptyBlock.id),
],
},
};
const result = await blocksTable.count(params);
expect(result).toBe(2);
});

it('should get row count using whereIn and whereNull', async () => {
await blocksTable.upsert([emptyBlock, nonEmptyBlock]);
const params = {
Expand Down Expand Up @@ -335,6 +390,13 @@ describe('Test MySQL', () => {
expect(result.length).toBeGreaterThan(distinctResult.length);
});

it('should execute group by query', async () => {
await blocksTable.upsert([emptyBlock, { ...nonEmptyBlock, id: emptyBlock.id }]);
const result = await blocksTable.find();
const groupByResult = await blocksTable.find({ groupBy: 'id' }, 'id');
expect(result.length).toBeGreaterThan(groupByResult.length);
});

it('should execute update method', async () => {
const [retrievedBlock] = await blocksTable.find({ id: emptyBlock.id }, ['timestamp']);
expect(retrievedBlock.timestamp).toBe(emptyBlock.timestamp);
Expand Down Expand Up @@ -807,6 +869,30 @@ describe('Test MySQL', () => {
expect(result[0].height).toBeGreaterThanOrEqual(result[1].height);
});

it('should order the rows in ascending order based on their height using orderByRaw query', async () => {
const connection = await getDBConnection();
const trx = await startDBTransaction(connection);
await blocksTable.upsert([{ ...emptyBlock, size: 50 }], trx);
await commitDBTransaction(trx);

const result = await blocksTable.find({ orderByRaw: ['height asc'] });
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(2);
expect(result[1].height).toBeGreaterThanOrEqual(result[0].height);
});

it('should order the rows in descending order based on their height', async () => {
const connection = await getDBConnection();
const trx = await startDBTransaction(connection);
await blocksTable.upsert([{ ...emptyBlock, size: 50 }], trx);
await commitDBTransaction(trx);

const result = await blocksTable.find({ orderByRaw: ['height desc'] });
expect(result).toBeInstanceOf(Array);
expect(result.length).toBe(2);
expect(result[0].height).toBeGreaterThanOrEqual(result[1].height);
});

it('should get row count', async () => {
const count = await blocksTable.count();
expect(count).toBe(2);
Expand Down Expand Up @@ -968,6 +1054,16 @@ describe('Test MySQL', () => {
expect(result.length).toBeGreaterThan(distinctResult.length);
});

it('should perform group by query', async () => {
const connection = await getDBConnection();
const trx = await startDBTransaction(connection);
await blocksTable.upsert([emptyBlock, { ...nonEmptyBlock, id: emptyBlock.id }], trx);
await commitDBTransaction(trx);
const result = await blocksTable.find();
const groupByResult = await blocksTable.find({ groupBy: 'id' }, 'id');
expect(result.length).toBeGreaterThan(groupByResult.length);
});

it('should perform update method', async () => {
const [retrievedBlock] = await blocksTable.find({ id: emptyBlock.id }, ['timestamp']);
expect(retrievedBlock.timestamp).toBe(emptyBlock.timestamp);
Expand Down
Loading

0 comments on commit 405e26e

Please sign in to comment.