Skip to content

Commit

Permalink
fix: check if the connection is closed before executing a query. This…
Browse files Browse the repository at this point in the history
… prevents SQLITE_MISUSE errors (https://sqlite.org/rescode.html#misuse) originating from sqlite itself (#6975)
  • Loading branch information
jjhbw committed Oct 26, 2020
1 parent fa86413 commit 5f6bbec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/driver/sqlite/SqliteQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {AbstractSqliteQueryRunner} from "../sqlite-abstract/AbstractSqliteQueryR
import {SqliteConnectionOptions} from "./SqliteConnectionOptions";
import {SqliteDriver} from "./SqliteDriver";
import {Broadcaster} from "../../subscriber/Broadcaster";
import { ConnectionIsNotSetError } from '../../error/ConnectionIsNotSetError';

/**
* Runs queries on a single sqlite database connection.
Expand Down Expand Up @@ -39,6 +40,10 @@ export class SqliteQueryRunner extends AbstractSqliteQueryRunner {
const connection = this.driver.connection;
const options = connection.options as SqliteConnectionOptions;

if (!connection.isConnected){
throw new ConnectionIsNotSetError('sqlite')
}

return new Promise<any[]>(async (ok, fail) => {

const databaseConnection = await this.connect();
Expand Down
21 changes: 21 additions & 0 deletions test/functional/sqlite/error-on-query-after-close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "reflect-metadata";
import {expect} from "chai";
import {Connection} from "../../../src/connection/Connection";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";

describe("sqlite driver > throws an error when queried after closing connection", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [],
enabledDrivers: ["sqlite"],
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should throw", () => Promise.all(connections.map(async connection => {
await connection.close()
await expect(connection.query('select * from sqlite_master;')).to.rejectedWith(
'Connection with sqlite database is not established. Check connection configuration.'
);
})));
});

0 comments on commit 5f6bbec

Please sign in to comment.