2018-01-10 15:31:54 +01:00
'use strict' ;
// Node.js core.
const execSync = require ( 'child_process' ) . execSync ;
2018-01-11 13:22:49 +01:00
const path = require ( 'path' ) ;
2018-01-10 15:31:54 +01:00
2018-04-23 14:32:56 +02:00
// Public node modules
const inquirer = require ( 'inquirer' ) ;
2018-01-10 15:31:54 +01:00
module . exports = ( scope , success , error ) => {
2018-02-12 19:14:36 +01:00
const knex = require ( path . resolve ( ` ${ scope . tmpPath } /node_modules/knex ` ) ) ( {
2018-01-10 15:31:54 +01:00
client : scope . client . module ,
2018-02-06 16:00:04 +01:00
connection : Object . assign ( { } , scope . database . settings , {
2018-01-17 09:14:34 +01:00
user : scope . database . settings . username
2018-01-10 18:08:43 +01:00
} )
2018-01-10 15:31:54 +01:00
} ) ;
knex . raw ( 'select 1+1 as result' ) . then ( ( ) => {
2018-04-23 14:32:56 +02:00
knex . raw ( scope . client . database === 'postgres' ? "SELECT tablename FROM pg_tables WHERE schemaname='public'" : 'SELECT * FROM information_schema.tables' ) . then ( ( tables ) => {
knex . destroy ( ) ;
const next = ( ) => {
execSync ( ` rm -r " ${ scope . tmpPath } " ` ) ;
success ( ) ;
} ;
2018-05-03 12:45:02 +02:00
if ( tables . rows && tables . rows . length !== 0 ) {
2018-06-22 15:25:15 +02:00
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.' ) ;
2018-01-10 15:31:54 +01:00
2018-04-23 14:32:56 +02:00
inquirer . prompt ( [ {
type : 'confirm' ,
name : 'confirm' ,
message : ` Are you sure you want to continue with the ${ scope . database . settings . database } database: ` ,
} ] )
2018-05-16 12:07:02 +02:00
. then ( ( { confirm } ) => {
if ( confirm ) {
next ( ) ;
} else {
error ( ) ;
}
} ) ;
2018-04-23 14:32:56 +02:00
} else {
next ( ) ;
}
} ) ;
2018-01-10 15:31:54 +01:00
} )
2018-05-16 12:07:02 +02:00
. catch ( ( err ) => {
if ( err . sql ) {
2018-06-22 15:25:15 +02:00
console . log ( '⚠️ Server connection has failed! Make sure your database server is running.' ) ;
2018-05-16 12:07:02 +02:00
} else {
2018-06-22 15:25:15 +02:00
console . log ( ` ⚠️ Database connection has failed! Make sure your " ${ scope . database . settings . database } " database exist. ` ) ;
2018-05-16 12:07:02 +02:00
}
error ( ) ;
} ) ;
2018-01-10 15:31:54 +01:00
} ;