Skip to content

Commit

Permalink
feat(sqlite): automatic path provision for 'options.storage' (#11853)
Browse files Browse the repository at this point in the history
  • Loading branch information
multum authored and papb committed Jan 22, 2020
1 parent 12700ca commit cfc9685
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 38 deletions.
17 changes: 13 additions & 4 deletions lib/dialects/sqlite/connection-manager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const path = require('path');
const jetpack = require('fs-jetpack');
const AbstractConnectionManager = require('../abstract/connection-manager');
const Promise = require('../../promise');
const { logger } = require('../../utils/logger');
Expand Down Expand Up @@ -44,19 +46,26 @@ class ConnectionManager extends AbstractConnectionManager {
getConnection(options) {
options = options || {};
options.uuid = options.uuid || 'default';
options.inMemory = (this.sequelize.options.storage || this.sequelize.options.host || ':memory:') === ':memory:' ? 1 : 0;
options.storage = this.sequelize.options.storage || this.sequelize.options.host || ':memory:';
options.inMemory = options.storage === ':memory:' ? 1 : 0;

const dialectOptions = this.sequelize.options.dialectOptions;
options.readWriteMode = dialectOptions && dialectOptions.mode;
const defaultReadWriteMode = this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE;

options.readWriteMode = dialectOptions && dialectOptions.mode || defaultReadWriteMode;

if (this.connections[options.inMemory || options.uuid]) {
return Promise.resolve(this.connections[options.inMemory || options.uuid]);
}

if (!options.inMemory && (options.readWriteMode & this.lib.OPEN_CREATE) !== 0) {
jetpack.dir(path.dirname(options.storage)); // automatic path provision for `options.storage`
}

return new Promise((resolve, reject) => {
this.connections[options.inMemory || options.uuid] = new this.lib.Database(
this.sequelize.options.storage || this.sequelize.options.host || ':memory:',
options.readWriteMode || this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE, // default mode
options.storage,
options.readWriteMode,
err => {
if (err) return reject(new sequelizeErrors.ConnectionError(err));
debug(`connection acquired ${options.uuid}`);
Expand Down
25 changes: 6 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"bluebird": "^3.7.1",
"debug": "^4.1.1",
"dottie": "^2.0.0",
"fs-jetpack": "^2.2.3",
"inflection": "1.12.0",
"lodash": "^4.17.15",
"moment": "^2.24.0",
Expand Down Expand Up @@ -64,7 +65,6 @@
"eslint": "^6.8.0",
"eslint-plugin-jsdoc": "^4.1.1",
"eslint-plugin-mocha": "^6.2.2",
"fs-jetpack": "^2.2.2",
"husky": "^1.3.1",
"js-combinatorics": "^0.5.5",
"lcov-result-merger": "^3.0.0",
Expand Down
27 changes: 23 additions & 4 deletions test/integration/configuration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@ if (dialect === 'sqlite') {
describe(Support.getTestDialectTeaser('Configuration'), () => {
describe('Connections problems should fail with a nice message', () => {
it('when we don\'t have the correct server details', () => {
const seq = new Sequelize(config[dialect].database, config[dialect].username, config[dialect].password, { storage: '/path/to/no/where/land', logging: false, host: '0.0.0.1', port: config[dialect].port, dialect });
const options = {
logging: false,
host: '0.0.0.1',
port: config[dialect].port,
dialect
};

const constructorArgs = [
config[dialect].database,
config[dialect].username,
config[dialect].password,
options
];

let willBeRejectedWithArgs = [[Sequelize.HostNotReachableError, Sequelize.InvalidConnectionError]];

if (dialect === 'sqlite') {
options.storage = '/path/to/no/where/land';
options.dialectOptions = { mode: sqlite3.OPEN_READONLY };
// SQLite doesn't have a breakdown of error codes, so we are unable to discern between the different types of errors.
return expect(seq.query('select 1 as hello')).to.eventually.be.rejectedWith(Sequelize.ConnectionError, 'SQLITE_CANTOPEN: unable to open database file');
willBeRejectedWithArgs = [Sequelize.ConnectionError, 'SQLITE_CANTOPEN: unable to open database file'];
}
return expect(seq.query('select 1 as hello')).to.eventually.be.rejectedWith([Sequelize.HostNotReachableError, Sequelize.InvalidConnectionError]);

const seq = new Sequelize(...constructorArgs);
return expect(seq.query('select 1 as hello')).to.eventually.be.rejectedWith(...willBeRejectedWithArgs);
});

it('when we don\'t have the correct login information', () => {
Expand Down Expand Up @@ -88,7 +107,7 @@ describe(Support.getTestDialectTeaser('Configuration'), () => {
);
})
.then(() => {
// By default, sqlite creates a connection that's READWRITE | CREATE
// By default, sqlite creates a connection that's READWRITE | CREATE
const sequelize = new Sequelize('sqlite://foo', {
storage: p
});
Expand Down
30 changes: 20 additions & 10 deletions test/integration/dialects/sqlite/connection-manager.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
'use strict';

const chai = require('chai');
const fs = require('fs');
const path = require('path');
const jetpack = require('fs-jetpack').cwd(__dirname);
const expect = chai.expect;
const Support = require('../../support');
const dialect = Support.getTestDialect();
const DataTypes = require('../../../../lib/data-types');

const fileName = `${Math.random()}_test.sqlite`;
const folderName = `${Math.random()}_test_folder`;

if (dialect === 'sqlite') {
describe('[SQLITE Specific] Connection Manager', () => {
after(() => {
fs.unlinkSync(path.join(__dirname, fileName));
jetpack.remove(fileName);
jetpack.remove(folderName);
});

it('close connection and remove journal and wal files', function() {
const sequelize = Support.createSequelizeInstance({
storage: path.join(__dirname, fileName)
storage: jetpack.path(fileName)
});
const User = sequelize.define('User', { username: DataTypes.STRING });

Expand All @@ -32,19 +33,28 @@ if (dialect === 'sqlite') {
});
})
.then(() => {
expect(fs.existsSync(path.join(__dirname, fileName))).to.be.true;
expect(fs.existsSync(path.join(__dirname, `${fileName}-shm`)), 'shm file should exists').to.be.true;
expect(fs.existsSync(path.join(__dirname, `${fileName}-wal`)), 'wal file should exists').to.be.true;
expect(jetpack.exists(fileName)).to.be.equal('file');
expect(jetpack.exists(`${fileName}-shm`), 'shm file should exists').to.be.equal('file');
expect(jetpack.exists(`${fileName}-wal`), 'wal file should exists').to.be.equal('file');

return sequelize.close();
})
.then(() => {
expect(fs.existsSync(path.join(__dirname, fileName))).to.be.true;
expect(fs.existsSync(path.join(__dirname, `${fileName}-shm`)), 'shm file exists').to.be.false;
expect(fs.existsSync(path.join(__dirname, `${fileName}-wal`)), 'wal file exists').to.be.false;
expect(jetpack.exists(fileName)).to.be.equal('file');
expect(jetpack.exists(`${fileName}-shm`), 'shm file exists').to.be.false;
expect(jetpack.exists(`${fileName}-wal`), 'wal file exists').to.be.false;

return this.sequelize.query('PRAGMA journal_mode = DELETE');
});
});

it('automatic path provision for `options.storage`', () => {
const p = jetpack.path(folderName, fileName);
return Support.createSequelizeInstance({ storage: p })
.define('User', { username: DataTypes.STRING })
.sync({ force: true }).then(() => {
expect(jetpack.exists(p)).to.be.equal('file');
});
});
});
}

0 comments on commit cfc9685

Please sign in to comment.