2018-01-10 15:31:54 +01:00
'use strict' ;
2018-04-23 14:32:56 +02:00
// Public node modules
const inquirer = require ( 'inquirer' ) ;
2019-01-31 14:05:59 +01:00
2019-07-08 15:39:08 +02:00
const hasResults = rows => {
if ( ! rows || rows . length === 0 ) return true ;
return false ;
} ;
const checkDatabaseIsEmpty = {
postgres : client =>
client
. select ( 'tablename' )
. from ( 'pg_tables' )
. where ( 'schemaname' , 'public' )
. then ( hasResults ) ,
mysql : ( client , { database } ) =>
client
. select ( )
. from ( 'information_schema.tables' )
. where ( 'table_schema' , database )
. then ( hasResults ) ,
sqlite : client =>
client
. select ( )
. from ( 'sqlite_master' )
. then ( hasResults ) ,
2019-06-19 19:02:36 +02:00
} ;
2018-12-28 16:51:02 +01:00
2019-06-19 19:02:36 +02:00
module . exports = async ( { scope , connection } ) => {
const knex = require ( 'knex' ) ;
2018-12-29 21:35:07 +01:00
2019-06-19 19:02:36 +02:00
const { settings } = connection ;
2019-04-09 21:51:28 +02:00
const client = knex ( {
2019-06-19 19:02:36 +02:00
client : settings . client ,
connection : Object . assign ( { } , settings , {
user : settings . username ,
2018-12-29 21:35:07 +01:00
} ) ,
2019-06-19 19:02:36 +02:00
useNullAsDefault : true ,
2018-01-10 15:31:54 +01:00
} ) ;
2019-06-19 19:02:36 +02:00
const destroyClientAndThrow = err => {
return client . destroy ( ) . then (
( ) => {
throw err ;
} ,
( ) => {
throw err ;
}
) ;
} ;
2018-12-29 21:35:07 +01:00
2019-06-19 19:02:36 +02:00
await client . raw ( 'select 1+1 as result' ) . catch ( destroyClientAndThrow ) ;
2018-04-23 14:32:56 +02:00
2019-07-08 15:39:08 +02:00
return checkDatabaseIsEmpty [ settings . client ] ( client , settings )
. then ( isEmpty => {
if ( isEmpty ) return ;
if ( scope . dbforce ) return ;
2018-04-23 14:32:56 +02:00
2019-07-08 15:39:08 +02:00
console . log ( ) ;
console . error (
'It seems that your database is not empty.\nStrapi automatically creates tables and columns which might corrupt the data already present in your database.'
2019-06-19 19:02:36 +02:00
) ;
2018-01-10 15:31:54 +01:00
2019-06-19 19:02:36 +02:00
return inquirer
. prompt ( [
{
2018-12-24 15:39:51 +05:30
type : 'confirm' ,
name : 'confirm' ,
2019-07-08 15:39:08 +02:00
message : ` Are you sure you want to continue with the ${ settings . database } database: ` ,
2019-06-19 19:02:36 +02:00
} ,
] )
. then ( ( { confirm } ) => {
2019-07-08 15:39:08 +02:00
// send restart flag to retry
if ( ! confirm ) return { shouldRetry : true } ;
2019-06-19 19:02:36 +02:00
} ) ;
} )
2019-07-08 15:39:08 +02:00
. then ( res => client . destroy ( ) . then ( ( ) => res ) )
2019-06-19 19:02:36 +02:00
. catch ( destroyClientAndThrow ) ;
2018-01-10 15:31:54 +01:00
} ;