93 lines
2.5 KiB
JavaScript
Raw Normal View History

'use strict';
2018-12-29 21:35:07 +01:00
// Node.js core.
const path = require('path');
// Public node modules
const inquirer = require('inquirer');
2018-09-12 22:21:26 -05:00
const rimraf = require('rimraf');
module.exports = (scope, success, error) => {
2019-01-31 14:05:59 +01:00
if (scope.client.database === 'sqlite') {
return success();
2018-12-29 21:35:07 +01:00
}
2019-01-31 14:05:59 +01:00
2018-12-28 16:51:02 +01:00
let knex;
try {
// eslint-disable-next-line import/no-unresolved
knex = require('knex');
} catch (err) {
// eslint-disable-next-line import/no-unresolved
knex = require(path.resolve(scope.tmpPath, 'node_modules', 'knex'));
}
2018-12-29 21:35:07 +01:00
// eslint-disable-next-line import/no-unresolved
2018-12-28 16:51:02 +01:00
knex = knex({
client: scope.client.module,
connection: Object.assign({}, scope.database.settings, {
user: scope.database.settings.username
2018-12-29 21:35:07 +01:00
}),
useNullAsDefault: true
});
knex.raw('select 1+1 as result').then(() => {
2018-12-29 21:35:07 +01:00
const selectQueries = {
2019-02-04 15:39:47 +01:00
postgres: 'SELECT tablename FROM pg_tables WHERE schemaname=\'public\'',
mysql: 'SELECT * FROM information_schema.tables',
2018-12-29 21:35:07 +01:00
sqlite: 'select * from sqlite_master'
};
knex.raw(selectQueries[scope.client.database]).then((tables) => {
knex.destroy();
const next = () => {
rimraf(scope.tmpPath, (err) => {
if (err) {
console.log(`Error removing connection test folder: ${scope.tmpPath}`);
}
success();
});
};
if (tables.rows && tables.rows.length !== 0) {
if (scope.dbforce) {
next();
} else {
console.log('🤔 It seems that your database is not empty. Be aware that Strapi is going to automatically creates tables & columns, and might update columns which can corrupt data or cause data loss.');
inquirer.prompt([{
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to continue with the ${scope.database.settings.database} database:`,
}])
.then(({ confirm }) => {
if (confirm) {
next();
} else {
error();
}
});
}
} else {
next();
}
});
})
2018-05-16 12:07:02 +02:00
.catch((err) => {
if (err.sql) {
2018-12-28 16:51:02 +01:00
console.log('⚠️ Server connection has failed! Make sure your database server is running.');
2018-05-16 12:07:02 +02:00
} else {
2018-12-28 16:51:02 +01:00
console.log(`⚠️ Database connection has failed! Make sure your "${scope.database.settings.database}" database exist.`);
2018-05-16 12:07:02 +02:00
}
2018-12-28 16:51:02 +01:00
if (scope.debug) {
console.log('🐛 Full error log:');
console.log(err);
}
2018-05-16 12:07:02 +02:00
error();
});
};