不要怂,就是干,撸起袖子干!

Commit cfc9685b by Andrew Vereshchak Committed by Pedro Augusto de Paula Barbosa

feat(sqlite): automatic path provision for 'options.storage' (#11853)

1 parent 12700ca0
'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');
......@@ -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}`);
......
......@@ -1635,8 +1635,7 @@
"balanced-match": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
},
"base": {
"version": "0.11.2",
......@@ -1751,7 +1750,6 @@
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
......@@ -2287,8 +2285,7 @@
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
"dev": true
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
},
"console-control-strings": {
"version": "1.1.0",
......@@ -4145,7 +4142,6 @@
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/fs-jetpack/-/fs-jetpack-2.2.3.tgz",
"integrity": "sha512-MldfoKMz2NwpvP3UFfVXLp4NCncy9yxGamgBK6hofFaisnWoGvgkAyTtKwcq++leztgZuM4ywrZEaUtiyVfWgA==",
"dev": true,
"requires": {
"minimatch": "^3.0.2",
"rimraf": "^2.6.3"
......@@ -4173,8 +4169,7 @@
"fs.realpath": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=",
"dev": true
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
},
"function-bind": {
"version": "1.1.1",
......@@ -4353,7 +4348,6 @@
"version": "7.1.6",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz",
"integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==",
"dev": true,
"requires": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
......@@ -4913,7 +4907,6 @@
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
"dev": true,
"requires": {
"once": "^1.3.0",
"wrappy": "1"
......@@ -4922,8 +4915,7 @@
"inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"dev": true
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"ini": {
"version": "1.3.5",
......@@ -6530,7 +6522,6 @@
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": {
"brace-expansion": "^1.1.7"
}
......@@ -10926,7 +10917,6 @@
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"dev": true,
"requires": {
"wrappy": "1"
}
......@@ -11161,8 +11151,7 @@
"path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=",
"dev": true
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
},
"path-is-inside": {
"version": "1.0.2",
......@@ -11846,7 +11835,6 @@
"version": "2.7.1",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
"integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==",
"dev": true,
"requires": {
"glob": "^7.1.3"
}
......@@ -13738,8 +13726,7 @@
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=",
"dev": true
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
},
"write": {
"version": "1.0.3",
......
......@@ -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",
......@@ -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",
......
......@@ -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', () => {
......
'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 });
......@@ -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');
});
});
});
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!