Handle different databases names standards

This commit is contained in:
Alexandre Bodin 2019-04-18 16:01:31 +02:00
parent ae943d7492
commit 187700e1c9
3 changed files with 38 additions and 8 deletions

View File

@ -43,7 +43,8 @@
"shelljs": "^0.7.7",
"snyk": "^1.99.0",
"strapi-lint": "file:packages/strapi-lint",
"wait-on": "^3.2.0"
"wait-on": "^3.2.0",
"yargs": "^13.2.2"
},
"scripts": {
"clean": "npm run removesymlinkdependencies && npx rimraf package-lock.json && npx rimraf packages/*/package-lock.json",

View File

@ -23,6 +23,20 @@ const buildQuery = require('./buildQuery');
const PIVOT_PREFIX = '_pivot_';
const GLOBALS = {};
const getDatabaseName = connection => {
const dbName = _.get(connection.settings, 'database');
switch (_.get(connection.settings, 'client')) {
case 'sqlite3':
return 'main';
case 'pg':
return `${dbName}.public`;
case 'mysql':
return dbName;
default:
return dbName;
}
};
/**
* Bookshelf hook
*/
@ -83,7 +97,7 @@ module.exports = function(strapi) {
// Add some informations about ORM & client connection & tableName
definition.orm = 'bookshelf';
definition.databaseName = _.get(connection.settings, 'database');
definition.databaseName = getDatabaseName(connection);
definition.client = _.get(connection.settings, 'client');
_.defaults(definition, {
primaryKey: 'id',

View File

@ -1,7 +1,12 @@
const path = require('path');
const { cleanTestApp, generateTestApp, startTestApp } = require('./helpers/testAppGenerator');
const {
cleanTestApp,
generateTestApp,
startTestApp,
} = require('./helpers/testAppGenerator');
const execa = require('execa');
const waitOn = require('wait-on');
const yargs = require('yargs');
const appName = 'testApp';
@ -25,9 +30,7 @@ const test = async () => {
});
};
const main = async () => {
const database = process.argv.length > 2 ? process.argv.slice(2).join(' ') : databases.sqlite;
const main = async database => {
try {
await cleanTestApp(appName);
await generateTestApp({ appName, database });
@ -45,11 +48,23 @@ const main = async () => {
testAppProcess.kill();
process.exit(0);
} catch (error) {
console.log(error)
console.log(error);
process.stdout.write('Tests failed\n', () => {
process.exit(1);
});
}
};
main();
yargs
.command(
'$0 [databaseName]',
'run end to end tests',
yargs => {
yargs.positional('databaseName', {
default: 'sqlite',
choices: Object.keys(databases),
});
},
({ databaseName }) => main(databases[databaseName])
)
.help().argv;