Skip to content

Commit

Permalink
Handle different databases names standards
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Apr 18, 2019
1 parent ae943d7 commit 187700e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion packages/strapi-hook-bookshelf/lib/index.js
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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',
Expand Down
27 changes: 21 additions & 6 deletions test/e2e.js
@@ -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';

Expand All @@ -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 });
Expand All @@ -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;

0 comments on commit 187700e

Please sign in to comment.