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

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'; 'use strict';
const path = require('path');
const jetpack = require('fs-jetpack');
const AbstractConnectionManager = require('../abstract/connection-manager'); const AbstractConnectionManager = require('../abstract/connection-manager');
const Promise = require('../../promise'); const Promise = require('../../promise');
const { logger } = require('../../utils/logger'); const { logger } = require('../../utils/logger');
...@@ -44,19 +46,26 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -44,19 +46,26 @@ class ConnectionManager extends AbstractConnectionManager {
getConnection(options) { getConnection(options) {
options = options || {}; options = options || {};
options.uuid = options.uuid || 'default'; 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; 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]) { if (this.connections[options.inMemory || options.uuid]) {
return Promise.resolve(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) => { return new Promise((resolve, reject) => {
this.connections[options.inMemory || options.uuid] = new this.lib.Database( this.connections[options.inMemory || options.uuid] = new this.lib.Database(
this.sequelize.options.storage || this.sequelize.options.host || ':memory:', options.storage,
options.readWriteMode || this.lib.OPEN_READWRITE | this.lib.OPEN_CREATE, // default mode options.readWriteMode,
err => { err => {
if (err) return reject(new sequelizeErrors.ConnectionError(err)); if (err) return reject(new sequelizeErrors.ConnectionError(err));
debug(`connection acquired ${options.uuid}`); debug(`connection acquired ${options.uuid}`);
......
...@@ -1635,8 +1635,7 @@ ...@@ -1635,8 +1635,7 @@
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz",
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
"dev": true
}, },
"base": { "base": {
"version": "0.11.2", "version": "0.11.2",
...@@ -1751,7 +1750,6 @@ ...@@ -1751,7 +1750,6 @@
"version": "1.1.11", "version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
"integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==",
"dev": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
...@@ -2287,8 +2285,7 @@ ...@@ -2287,8 +2285,7 @@
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s="
"dev": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
...@@ -4145,7 +4142,6 @@ ...@@ -4145,7 +4142,6 @@
"version": "2.2.3", "version": "2.2.3",
"resolved": "https://registry.npmjs.org/fs-jetpack/-/fs-jetpack-2.2.3.tgz", "resolved": "https://registry.npmjs.org/fs-jetpack/-/fs-jetpack-2.2.3.tgz",
"integrity": "sha512-MldfoKMz2NwpvP3UFfVXLp4NCncy9yxGamgBK6hofFaisnWoGvgkAyTtKwcq++leztgZuM4ywrZEaUtiyVfWgA==", "integrity": "sha512-MldfoKMz2NwpvP3UFfVXLp4NCncy9yxGamgBK6hofFaisnWoGvgkAyTtKwcq++leztgZuM4ywrZEaUtiyVfWgA==",
"dev": true,
"requires": { "requires": {
"minimatch": "^3.0.2", "minimatch": "^3.0.2",
"rimraf": "^2.6.3" "rimraf": "^2.6.3"
...@@ -4173,8 +4169,7 @@ ...@@ -4173,8 +4169,7 @@
"fs.realpath": { "fs.realpath": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
"integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8="
"dev": true
}, },
"function-bind": { "function-bind": {
"version": "1.1.1", "version": "1.1.1",
...@@ -4353,7 +4348,6 @@ ...@@ -4353,7 +4348,6 @@
"version": "7.1.6", "version": "7.1.6",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz",
"integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==",
"dev": true,
"requires": { "requires": {
"fs.realpath": "^1.0.0", "fs.realpath": "^1.0.0",
"inflight": "^1.0.4", "inflight": "^1.0.4",
...@@ -4913,7 +4907,6 @@ ...@@ -4913,7 +4907,6 @@
"version": "1.0.6", "version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
"integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=",
"dev": true,
"requires": { "requires": {
"once": "^1.3.0", "once": "^1.3.0",
"wrappy": "1" "wrappy": "1"
...@@ -4922,8 +4915,7 @@ ...@@ -4922,8 +4915,7 @@
"inherits": { "inherits": {
"version": "2.0.4", "version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
"dev": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
...@@ -6530,7 +6522,6 @@ ...@@ -6530,7 +6522,6 @@
"version": "3.0.4", "version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
...@@ -10926,7 +10917,6 @@ ...@@ -10926,7 +10917,6 @@
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=",
"dev": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
...@@ -11161,8 +11151,7 @@ ...@@ -11161,8 +11151,7 @@
"path-is-absolute": { "path-is-absolute": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18="
"dev": true
}, },
"path-is-inside": { "path-is-inside": {
"version": "1.0.2", "version": "1.0.2",
...@@ -11846,7 +11835,6 @@ ...@@ -11846,7 +11835,6 @@
"version": "2.7.1", "version": "2.7.1",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz",
"integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==",
"dev": true,
"requires": { "requires": {
"glob": "^7.1.3" "glob": "^7.1.3"
} }
...@@ -13738,8 +13726,7 @@ ...@@ -13738,8 +13726,7 @@
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8="
"dev": true
}, },
"write": { "write": {
"version": "1.0.3", "version": "1.0.3",
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
"bluebird": "^3.7.1", "bluebird": "^3.7.1",
"debug": "^4.1.1", "debug": "^4.1.1",
"dottie": "^2.0.0", "dottie": "^2.0.0",
"fs-jetpack": "^2.2.3",
"inflection": "1.12.0", "inflection": "1.12.0",
"lodash": "^4.17.15", "lodash": "^4.17.15",
"moment": "^2.24.0", "moment": "^2.24.0",
...@@ -64,7 +65,6 @@ ...@@ -64,7 +65,6 @@
"eslint": "^6.8.0", "eslint": "^6.8.0",
"eslint-plugin-jsdoc": "^4.1.1", "eslint-plugin-jsdoc": "^4.1.1",
"eslint-plugin-mocha": "^6.2.2", "eslint-plugin-mocha": "^6.2.2",
"fs-jetpack": "^2.2.2",
"husky": "^1.3.1", "husky": "^1.3.1",
"js-combinatorics": "^0.5.5", "js-combinatorics": "^0.5.5",
"lcov-result-merger": "^3.0.0", "lcov-result-merger": "^3.0.0",
......
...@@ -17,12 +17,31 @@ if (dialect === 'sqlite') { ...@@ -17,12 +17,31 @@ if (dialect === 'sqlite') {
describe(Support.getTestDialectTeaser('Configuration'), () => { describe(Support.getTestDialectTeaser('Configuration'), () => {
describe('Connections problems should fail with a nice message', () => { describe('Connections problems should fail with a nice message', () => {
it('when we don\'t have the correct server details', () => { 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') { 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. // 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', () => { it('when we don\'t have the correct login information', () => {
......
'use strict'; 'use strict';
const chai = require('chai'); const chai = require('chai');
const fs = require('fs'); const jetpack = require('fs-jetpack').cwd(__dirname);
const path = require('path');
const expect = chai.expect; const expect = chai.expect;
const Support = require('../../support'); const Support = require('../../support');
const dialect = Support.getTestDialect(); const dialect = Support.getTestDialect();
const DataTypes = require('../../../../lib/data-types'); const DataTypes = require('../../../../lib/data-types');
const fileName = `${Math.random()}_test.sqlite`; const fileName = `${Math.random()}_test.sqlite`;
const folderName = `${Math.random()}_test_folder`;
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
describe('[SQLITE Specific] Connection Manager', () => { describe('[SQLITE Specific] Connection Manager', () => {
after(() => { after(() => {
fs.unlinkSync(path.join(__dirname, fileName)); jetpack.remove(fileName);
jetpack.remove(folderName);
}); });
it('close connection and remove journal and wal files', function() { it('close connection and remove journal and wal files', function() {
const sequelize = Support.createSequelizeInstance({ const sequelize = Support.createSequelizeInstance({
storage: path.join(__dirname, fileName) storage: jetpack.path(fileName)
}); });
const User = sequelize.define('User', { username: DataTypes.STRING }); const User = sequelize.define('User', { username: DataTypes.STRING });
...@@ -32,19 +33,28 @@ if (dialect === 'sqlite') { ...@@ -32,19 +33,28 @@ if (dialect === 'sqlite') {
}); });
}) })
.then(() => { .then(() => {
expect(fs.existsSync(path.join(__dirname, fileName))).to.be.true; expect(jetpack.exists(fileName)).to.be.equal('file');
expect(fs.existsSync(path.join(__dirname, `${fileName}-shm`)), 'shm file should exists').to.be.true; expect(jetpack.exists(`${fileName}-shm`), 'shm file should exists').to.be.equal('file');
expect(fs.existsSync(path.join(__dirname, `${fileName}-wal`)), 'wal file should exists').to.be.true; expect(jetpack.exists(`${fileName}-wal`), 'wal file should exists').to.be.equal('file');
return sequelize.close(); return sequelize.close();
}) })
.then(() => { .then(() => {
expect(fs.existsSync(path.join(__dirname, fileName))).to.be.true; expect(jetpack.exists(fileName)).to.be.equal('file');
expect(fs.existsSync(path.join(__dirname, `${fileName}-shm`)), 'shm file exists').to.be.false; expect(jetpack.exists(`${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}-wal`), 'wal file exists').to.be.false;
return this.sequelize.query('PRAGMA journal_mode = DELETE'); 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!