2009-08-31 20:21:29 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools\Cli\Tasks;
|
|
|
|
|
2009-09-09 00:13:09 +04:00
|
|
|
use Doctrine\Common\DoctrineException,
|
|
|
|
Doctrine\ORM\Tools\SchemaTool,
|
2009-08-31 20:21:29 +04:00
|
|
|
Doctrine\Common\Annotations\AnnotationReader,
|
|
|
|
Doctrine\ORM\Mapping\Driver\AnnotationDriver,
|
|
|
|
Doctrine\ORM\Mapping\Driver\XmlDriver,
|
|
|
|
Doctrine\ORM\Mapping\Driver\YamlDriver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Task to create the database schema for a set of classes based on their mappings.
|
|
|
|
*
|
|
|
|
* This task has the following arguments:
|
|
|
|
*
|
2009-10-08 02:14:13 +04:00
|
|
|
* <tt>--class-dir=<path></tt>
|
2009-08-31 20:21:29 +04:00
|
|
|
* Specifies the directory where to start looking for mapped classes.
|
|
|
|
* This argument is required when the annotation metadata driver is used,
|
|
|
|
* otherwise it has no effect.
|
|
|
|
*
|
|
|
|
* <tt>--dump-sql</tt>
|
2009-09-01 12:18:36 +04:00
|
|
|
* Specifies that instead of directly executing the SQL statements,
|
|
|
|
* they should be printed to the standard output.
|
2009-08-31 20:21:29 +04:00
|
|
|
*
|
|
|
|
* <tt>--create</tt>
|
|
|
|
* Specifies that the schema of the classes should be created.
|
|
|
|
*
|
|
|
|
* <tt>--drop</tt>
|
|
|
|
* Specifies that the schema of the classes should be dropped.
|
|
|
|
*
|
|
|
|
* <tt>--update</tt>
|
|
|
|
* Specifies that the schema of the classes should be updated.
|
|
|
|
*
|
|
|
|
*
|
2009-09-13 03:25:47 +04:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @since 2.0
|
|
|
|
* @version $Revision: 3938 $
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-08-31 20:21:29 +04:00
|
|
|
*/
|
|
|
|
class SchemaToolTask extends AbstractTask
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function extendedHelp()
|
|
|
|
{
|
2009-09-04 21:24:48 +04:00
|
|
|
$printer = $this->getPrinter();
|
|
|
|
|
|
|
|
$printer->write('Task: ')->writeln('schema-tool', 'KEYWORD')
|
|
|
|
->write('Synopsis: ');
|
|
|
|
$this->_writeSynopsis($printer);
|
|
|
|
|
|
|
|
$printer->writeln('Description: Processes the schema and either apply it directly on EntityManager or generate the SQL output.')
|
|
|
|
->writeln('Options:')
|
|
|
|
->write('--create', 'REQ_ARG')
|
|
|
|
->writeln("\t\tCreates the schema in EntityManager (create tables on Database)")
|
|
|
|
->writeln("\t\t\tIf defined, --drop and --update can not be requested on same task")
|
|
|
|
->write(PHP_EOL)
|
2009-11-05 11:47:56 +03:00
|
|
|
->write('--drop=<metadata|database>', 'REQ_ARG')
|
2009-09-04 21:24:48 +04:00
|
|
|
->writeln("\t\t\tDrops the schema of EntityManager (drop tables on Database)")
|
2009-11-05 11:47:56 +03:00
|
|
|
->writeln("\t\t\tDefaults to 'metadata' if only --drop is specified.")
|
2009-09-04 21:24:48 +04:00
|
|
|
->writeln("\t\t\tIf defined, --create and --update can not be requested on same task")
|
|
|
|
->write(PHP_EOL)
|
|
|
|
->write('--update', 'REQ_ARG')
|
|
|
|
->writeln("\t\tUpdates the schema in EntityManager (update tables on Database)")
|
|
|
|
->writeln("\t\t\tIf defined, --create and --drop can not be requested on same task")
|
|
|
|
->write(PHP_EOL)
|
2009-10-17 05:57:50 +04:00
|
|
|
->write('--re-create', 'REQ_ARG')
|
|
|
|
->writeln("\t\tRuns --drop then --create to re-create the database.")
|
|
|
|
->write(PHP_EOL)
|
2009-09-04 21:24:48 +04:00
|
|
|
->write('--dump-sql', 'OPT_ARG')
|
|
|
|
->writeln("\t\tInstead of try to apply generated SQLs into EntityManager, output them.")
|
|
|
|
->write(PHP_EOL)
|
2009-10-08 02:14:13 +04:00
|
|
|
->write('--class-dir=<path>', 'OPT_ARG')
|
2009-10-08 03:39:34 +04:00
|
|
|
->writeln("\tOptional class directory to fetch for Entities.")
|
|
|
|
->write(PHP_EOL);
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function basicHelp()
|
|
|
|
{
|
2009-09-04 21:24:48 +04:00
|
|
|
$this->_writeSynopsis($this->getPrinter());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _writeSynopsis($printer)
|
|
|
|
{
|
|
|
|
$printer->write('schema-tool', 'KEYWORD')
|
2009-11-05 11:47:56 +03:00
|
|
|
->write(' (--create | --drop=<metadata|database> | --update | --re-create)', 'REQ_ARG')
|
2009-10-17 05:57:50 +04:00
|
|
|
->writeln(' [--dump-sql] [--class-dir=<path>]', 'OPT_ARG');
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$args = $this->getArguments();
|
|
|
|
$printer = $this->getPrinter();
|
|
|
|
|
2009-10-08 03:39:34 +04:00
|
|
|
if (array_key_exists('re-create', $args)) {
|
|
|
|
$args['drop'] = true;
|
|
|
|
$args['create'] = true;
|
|
|
|
$this->setArguments($args);
|
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
|
|
|
|
$isCreate = isset($args['create']);
|
|
|
|
$isDrop = isset($args['drop']);
|
|
|
|
$isUpdate = isset($args['update']);
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
if ($isUpdate && ($isCreate || $isDrop)) {
|
|
|
|
$printer->writeln("You can't use --update with --create or --drop", 'ERROR');
|
2009-08-31 20:21:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
2009-10-08 03:39:34 +04:00
|
|
|
|
|
|
|
if ( ! ($isCreate || $isDrop || $isUpdate)) {
|
|
|
|
$printer->writeln('You must specify at a minimum one of the options (--create, --drop, --update, --re-create).', 'ERROR');
|
|
|
|
return false;
|
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
|
2009-10-30 03:15:53 +03:00
|
|
|
$metadataDriver = $this->getEntityManager()->getConfiguration()->getMetadataDriverImpl();
|
|
|
|
|
2009-10-08 02:14:13 +04:00
|
|
|
if ($metadataDriver instanceof \Doctrine\ORM\Mapping\Driver\AnnotationDriver) {
|
|
|
|
if ( ! isset($args['class-dir'])) {
|
|
|
|
$printer->writeln("The supplied configuration uses the annotation metadata driver."
|
|
|
|
. " The 'class-dir' argument is required for this driver.", 'ERROR');
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$metadataDriver->setClassDirectory($args['class-dir']);
|
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the task.
|
|
|
|
*/
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$args = $this->getArguments();
|
2009-10-08 03:39:34 +04:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
$isCreate = isset($args['create']);
|
|
|
|
$isDrop = isset($args['drop']);
|
|
|
|
$isUpdate = isset($args['update']);
|
|
|
|
|
2009-10-30 03:15:53 +03:00
|
|
|
$em = $this->getEntityManager();
|
|
|
|
$cmf = $em->getMetadataFactory();
|
|
|
|
$driver = $em->getConfiguration()->getMetadataDriverImpl();
|
2009-11-05 11:47:56 +03:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
$classes = array();
|
2009-10-08 02:14:13 +04:00
|
|
|
$preloadedClasses = $driver->preload(true);
|
2009-11-05 11:47:56 +03:00
|
|
|
|
2009-10-08 02:14:13 +04:00
|
|
|
foreach ($preloadedClasses as $className) {
|
|
|
|
$classes[] = $cmf->getMetadataFor($className);
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
2009-10-08 02:14:13 +04:00
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
$printer = $this->getPrinter();
|
|
|
|
|
2009-09-11 23:50:48 +04:00
|
|
|
if (empty($classes)) {
|
|
|
|
$printer->writeln('No classes to process.', 'INFO');
|
|
|
|
return;
|
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
|
2009-10-30 03:15:53 +03:00
|
|
|
$tool = new SchemaTool($em);
|
|
|
|
|
2009-10-07 08:07:23 +04:00
|
|
|
if ($isDrop) {
|
2009-11-05 11:47:56 +03:00
|
|
|
$dropMode = $args['drop'];
|
|
|
|
if(!in_array($dropMode, array('metadata', 'database'))) {
|
|
|
|
$dropMode = 'metadata';
|
|
|
|
}
|
|
|
|
|
2009-08-31 20:21:29 +04:00
|
|
|
if (isset($args['dump-sql'])) {
|
2009-11-05 11:47:56 +03:00
|
|
|
foreach ($tool->getDropSchemaSql($classes, $dropMode) as $sql) {
|
2009-09-01 20:48:28 +04:00
|
|
|
$printer->writeln($sql);
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-10-07 08:07:23 +04:00
|
|
|
$printer->writeln('Dropping database schema...', 'INFO');
|
2009-09-05 21:56:50 +04:00
|
|
|
|
|
|
|
try {
|
2009-11-05 11:47:56 +03:00
|
|
|
$tool->dropSchema($classes, $dropMode);
|
2009-10-07 08:07:23 +04:00
|
|
|
$printer->writeln('Database schema dropped successfully.', 'INFO');
|
2009-09-05 21:56:50 +04:00
|
|
|
} catch (\Exception $ex) {
|
2009-09-09 00:13:09 +04:00
|
|
|
throw new DoctrineException($ex);
|
2009-09-05 21:56:50 +04:00
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($isCreate) {
|
2009-08-31 20:21:29 +04:00
|
|
|
if (isset($args['dump-sql'])) {
|
2009-10-07 08:07:23 +04:00
|
|
|
foreach ($tool->getCreateSchemaSql($classes) as $sql) {
|
2009-09-01 20:48:28 +04:00
|
|
|
$printer->writeln($sql);
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-10-07 08:07:23 +04:00
|
|
|
$printer->writeln('Creating database schema...', 'INFO');
|
2009-09-05 21:56:50 +04:00
|
|
|
|
|
|
|
try {
|
2009-10-07 08:07:23 +04:00
|
|
|
$tool->createSchema($classes);
|
|
|
|
$printer->writeln('Database schema created successfully.', 'INFO');
|
2009-09-05 21:56:50 +04:00
|
|
|
} catch (\Exception $ex) {
|
2009-09-09 00:13:09 +04:00
|
|
|
throw new DoctrineException($ex);
|
2009-09-05 21:56:50 +04:00
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
2009-10-07 08:07:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($isUpdate) {
|
2009-09-03 00:35:30 +04:00
|
|
|
if (isset($args['dump-sql'])) {
|
|
|
|
foreach ($tool->getUpdateSchemaSql($classes) as $sql) {
|
|
|
|
$printer->writeln($sql);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$printer->writeln('Updating database schema...', 'INFO');
|
2009-09-05 21:56:50 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
$tool->updateSchema($classes);
|
|
|
|
$printer->writeln('Database schema updated successfully.', 'INFO');
|
|
|
|
} catch (\Exception $ex) {
|
2009-09-09 00:13:09 +04:00
|
|
|
throw new DoctrineException($ex);
|
2009-09-05 21:56:50 +04:00
|
|
|
}
|
2009-09-03 00:35:30 +04:00
|
|
|
}
|
2009-08-31 20:21:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|