2010-04-08 00:47:42 -03:00
< ? php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* " AS IS " AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT
* LIMITED TO , THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL ,
* SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT
* LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*
* This software consists of voluntary contributions made by many individuals
2012-05-26 14:37:00 +02:00
* and is licensed under the MIT license . For more information , see
2010-04-08 00:47:42 -03:00
* < http :// www . doctrine - project . org >.
*/
namespace Doctrine\ORM\Tools\Console\Command\SchemaTool ;
2017-11-23 11:18:08 +01:00
use Doctrine\ORM\Tools\SchemaTool ;
2012-12-14 12:49:05 +00:00
use Symfony\Component\Console\Input\InputInterface ;
2017-11-23 11:18:08 +01:00
use Symfony\Component\Console\Input\InputOption ;
2012-12-14 12:49:05 +00:00
use Symfony\Component\Console\Output\OutputInterface ;
2010-04-08 00:47:42 -03:00
/**
* Command to drop the database schema for a set of classes based on their mappings .
*
* @ link www . doctrine - project . org
* @ since 2.0
* @ author Benjamin Eberlei < kontakt @ beberlei . de >
* @ author Guilherme Blanco < guilhermeblanco @ hotmail . com >
* @ author Jonathan Wage < jonwage @ gmail . com >
* @ author Roman Borschel < roman @ code - factory . org >
*/
2010-04-10 11:09:22 +02:00
class DropCommand extends AbstractCommand
2010-04-08 00:47:42 -03:00
{
/**
2012-12-14 12:49:05 +00:00
* { @ inheritdoc }
2010-04-08 00:47:42 -03:00
*/
protected function configure ()
{
2017-11-23 11:18:08 +01:00
$this -> setName ( 'orm:schema-tool:drop' )
-> setDescription ( 'Drop the complete database schema of EntityManager Storage Connection or generate the corresponding SQL output' )
-> addOption ( 'dump-sql' , null , InputOption :: VALUE_NONE , 'Instead of trying to apply generated SQLs into EntityManager Storage Connection, output them.' )
-> addOption ( 'force' , 'f' , InputOption :: VALUE_NONE , " Don't ask for the deletion of the database, but force the operation to run. " )
-> addOption ( 'full-database' , null , InputOption :: VALUE_NONE , 'Instead of using the Class Metadata to detect the database table schema, drop ALL assets that the database contains.' )
-> setHelp ( <<< EOT
2010-04-08 00:47:42 -03:00
Processes the schema and either drop the database schema of EntityManager Storage Connection or generate the SQL output .
Beware that the complete database is dropped by this command , even tables that are not relevant to your metadata model .
2013-05-09 16:24:19 +02:00
< comment > Hint :</ comment > If you have a database with tables that should not be managed
by the ORM , you can use a DBAL functionality to filter the tables and sequences down
on a global level :
\ $config -> setFilterSchemaAssetsExpression ( \ $regexp );
2010-04-08 00:47:42 -03:00
EOT
2017-11-23 11:18:08 +01:00
);
2010-04-08 00:47:42 -03:00
}
2012-12-14 12:49:05 +00:00
/**
* { @ inheritdoc }
*/
2010-04-10 11:09:22 +02:00
protected function executeSchemaCommand ( InputInterface $input , OutputInterface $output , SchemaTool $schemaTool , array $metadatas )
2010-04-08 00:47:42 -03:00
{
2012-10-22 01:58:31 +02:00
$isFullDatabaseDrop = $input -> getOption ( 'full-database' );
2010-11-16 21:31:54 +01:00
2012-10-22 01:58:31 +02:00
if ( $input -> getOption ( 'dump-sql' )) {
2010-11-16 21:31:54 +01:00
if ( $isFullDatabaseDrop ) {
$sqls = $schemaTool -> getDropDatabaseSQL ();
} else {
$sqls = $schemaTool -> getDropSchemaSQL ( $metadatas );
}
2012-10-22 01:58:31 +02:00
$output -> writeln ( implode ( ';' . PHP_EOL , $sqls ));
return 0 ;
}
if ( $input -> getOption ( 'force' )) {
$output -> writeln ( 'Dropping database schema...' );
2010-11-16 21:31:54 +01:00
if ( $isFullDatabaseDrop ) {
$schemaTool -> dropDatabase ();
} else {
$schemaTool -> dropSchema ( $metadatas );
}
2012-10-22 01:58:31 +02:00
$output -> writeln ( 'Database schema dropped successfully!' );
return 0 ;
}
2013-07-22 17:11:53 +02:00
$output -> writeln ( '<comment>ATTENTION</comment>: This operation should not be executed in a production environment.' . PHP_EOL );
2012-10-22 01:58:31 +02:00
if ( $isFullDatabaseDrop ) {
$sqls = $schemaTool -> getDropDatabaseSQL ();
2010-08-31 23:42:27 +02:00
} else {
2012-10-22 01:58:31 +02:00
$sqls = $schemaTool -> getDropSchemaSQL ( $metadatas );
}
2010-09-25 12:12:19 +02:00
2012-10-22 01:58:31 +02:00
if ( count ( $sqls )) {
2013-07-19 11:33:35 +02:00
$output -> writeln ( sprintf ( 'The Schema-Tool would execute <info>"%s"</info> queries to update the database.' , count ( $sqls )));
$output -> writeln ( 'Please run the operation by passing one - or both - of the following options:' );
$output -> writeln ( sprintf ( ' <info>%s --force</info> to execute the command' , $this -> getName ()));
$output -> writeln ( sprintf ( ' <info>%s --dump-sql</info> to dump the SQL statements to the screen' , $this -> getName ()));
2010-08-31 23:42:27 +02:00
2012-10-22 01:58:31 +02:00
return 1 ;
2010-04-08 00:47:42 -03:00
}
2012-10-22 01:58:31 +02:00
$output -> writeln ( 'Nothing to drop. The database is empty!' );
2013-02-03 01:31:56 +00:00
return 0 ;
2010-04-08 00:47:42 -03:00
}
2010-08-23 08:21:41 +02:00
}