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-06-19 19:02:36 +02: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
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-06-19 19:02:36 +02:00
return client
. raw ( selectQueries [ settings . client ] )
. then ( tables => {
if ( tables . rows && tables . rows . length === 0 ) {
return ;
}
2019-02-13 18:56:04 +01:00
2019-06-19 19:02:36 +02:00
if ( scope . dbforce ) {
return ;
}
2018-04-23 14:32:56 +02:00
2019-06-19 19:02:36 +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
2019-06-19 19:02:36 +02:00
return inquirer
. prompt ( [
{
2018-12-24 15:39:51 +05:30
type : 'confirm' ,
name : 'confirm' ,
2019-06-19 19:02:36 +02:00
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 ) ;
2018-01-10 15:31:54 +01:00
} ;