72 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
// Public node modules
const inquirer = require('inquirer');
2019-01-31 14:05:59 +01:00
const selectQueries = {
postgres: "SELECT tablename FROM pg_tables WHERE schemaname='public'",
mysql: 'SELECT * FROM information_schema.tables',
sqlite: 'select * from sqlite_master',
};
2018-12-28 16:51:02 +01:00
module.exports = async ({ scope, connection }) => {
const knex = require('knex');
2018-12-29 21:35:07 +01:00
const { settings } = connection;
2019-04-09 21:51:28 +02:00
const client = knex({
client: settings.client,
connection: Object.assign({}, settings, {
user: settings.username,
2018-12-29 21:35:07 +01:00
}),
useNullAsDefault: true,
});
const destroyClientAndThrow = err => {
return client.destroy().then(
() => {
throw err;
},
() => {
throw err;
}
);
};
2018-12-29 21:35:07 +01:00
await client.raw('select 1+1 as result').catch(destroyClientAndThrow);
return client
.raw(selectQueries[settings.client])
.then(tables => {
if (tables.rows && tables.rows.length === 0) {
return;
}
if (scope.dbforce) {
return;
}
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.'
);
return inquirer
.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Are you sure you want to continue with the ${
settings.database
} database:`,
},
])
.then(({ confirm }) => {
if (!confirm) {
// TODO: cancel somehow
throw new Error('Not confirmed');
}
});
})
.then(() => client.destroy())
.catch(destroyClientAndThrow);
};