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

Commit

Permalink
🐛 Updated sqlite to have one connection per db
Browse files Browse the repository at this point in the history
  • Loading branch information
vardan10 committed Oct 19, 2023
1 parent 64497d7 commit 9e278b6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
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.6.0",
"version": "1.6.1",
"description": "Lisk Service Framework",
"keywords": [
"lisk",
Expand Down
14 changes: 7 additions & 7 deletions framework/src/database/sqlite3.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ const createDBConnection = async (dbDataDir, tableName) => {
return knex;
};

const getDBConnection = async (tableName, dbDataDir) => {
if (!connectionPool[tableName]) {
const getDBConnection = async (tableName, dbDataDir = DB_DATA_DIR) => {
if (!connectionPool[dbDataDir]) {
if (!(await exists(dbDataDir))) {
await mkdir(dbDataDir, { recursive: true });
}
connectionPool[tableName] = await createDBConnection(dbDataDir, tableName);
connectionPool[dbDataDir] = await createDBConnection(dbDataDir, tableName);
}

const knex = connectionPool[tableName];
const knex = connectionPool[dbDataDir];
return knex;
};

const createTableIfNotExists = async tableConfig => {
const createTableIfNotExists = async (tableConfig, dbDataDir = DB_DATA_DIR) => {
const { tableName } = tableConfig;

if (!tablePool[tableName]) {
logger.info(`Creating schema for ${tableName}`);
const knex = await getDBConnection(tableName);
const knex = await getDBConnection(tableName, dbDataDir);
await util.loadSchema(knex, tableName, tableConfig);
tablePool[tableName] = true;
}
Expand All @@ -91,7 +91,7 @@ const getTableInstance = async (tableConfig, dbDataDir = DB_DATA_DIR) => {

const createDefaultTransaction = async connection => util.startDBTransaction(connection);

await createTableIfNotExists(tableConfig);
await createTableIfNotExists(tableConfig, dbDataDir);

const dbOperations = util.getTableInstance(tableConfig, knex);

Expand Down
14 changes: 7 additions & 7 deletions framework/tests/unit/database/sqlite3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('Test sqlite3 implementation', () => {
it('should insert row', async () => {
const preUpsertResult = await testTable.find();
expect(preUpsertResult.length).toBe(0);
const connection = await getDBConnection(tableName);
const connection = await getDBConnection(tableName, testDir);
const trx = await startDBTransaction(connection);
await testTable.upsert([blockWithoutTransaction.header], trx);
await commitDBTransaction(trx);
Expand All @@ -287,7 +287,7 @@ describe('Test sqlite3 implementation', () => {
});

it('should update row', async () => {
const connection = await getDBConnection(tableName);
const connection = await getDBConnection(tableName, testDir);
const trx = await startDBTransaction(connection);
const { id } = blockWithTransaction.header;
await testTable.upsert([{ ...blockWithTransaction.header, height: 20 }], trx);
Expand All @@ -310,7 +310,7 @@ describe('Test sqlite3 implementation', () => {
});

it('should increment column value', async () => {
const connection = await getDBConnection(tableName);
const connection = await getDBConnection(tableName, testDir);
const trx = await startDBTransaction(connection);
const { id } = blockWithoutTransaction.header;
await testTable.increment({
Expand All @@ -324,7 +324,7 @@ describe('Test sqlite3 implementation', () => {
});

it('should delete row by primary key', async () => {
const connection = await getDBConnection(tableName);
const connection = await getDBConnection(tableName, testDir);
const trx = await startDBTransaction(connection);
const [existingBlock] = await testTable.find();
const existingBlockId = existingBlock[`${schema.primaryKey}`];
Expand All @@ -339,7 +339,7 @@ describe('Test sqlite3 implementation', () => {
});

it('should delete rows', async () => {
const connection = await getDBConnection(tableName);
const connection = await getDBConnection(tableName, testDir);
const trx = await startDBTransaction(connection);

await testTable.upsert([blockWithoutTransaction.header, blockWithTransaction.header]);
Expand All @@ -357,7 +357,7 @@ describe('Test sqlite3 implementation', () => {
});

it('should delete row', async () => {
const connection = await getDBConnection(tableName);
const connection = await getDBConnection(tableName, testDir);
const trx = await startDBTransaction(connection);

await testTable.upsert([blockWithoutTransaction.header]);
Expand All @@ -375,7 +375,7 @@ describe('Test sqlite3 implementation', () => {
});

it('should insert rows in a batch', async () => {
const connection = await getDBConnection(tableName);
const connection = await getDBConnection(tableName, testDir);
const trx = await startDBTransaction(connection);
const preUpsertResult = await testTable.find();
expect(preUpsertResult.length).toBe(0);
Expand Down

0 comments on commit 9e278b6

Please sign in to comment.