| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php'); |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfPropelInsertSqlTask extends sfPropelBaseTask |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
* @see sfTask |
|---|
| 25 |
*/ |
|---|
| 26 |
protected function configure() |
|---|
| 27 |
{ |
|---|
| 28 |
$this->addOptions(array( |
|---|
| 29 |
new sfCommandOption('application', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', true), |
|---|
| 30 |
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'cli'), |
|---|
| 31 |
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', null), |
|---|
| 32 |
new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Do not ask for confirmation'), |
|---|
| 33 |
new sfCommandOption('phing-arg', null, sfCommandOption::PARAMETER_REQUIRED | sfCommandOption::IS_ARRAY, 'Arbitrary phing argument'), |
|---|
| 34 |
)); |
|---|
| 35 |
|
|---|
| 36 |
$this->aliases = array('propel-insert-sql'); |
|---|
| 37 |
$this->namespace = 'propel'; |
|---|
| 38 |
$this->name = 'insert-sql'; |
|---|
| 39 |
$this->briefDescription = 'Inserts SQL for current model'; |
|---|
| 40 |
|
|---|
| 41 |
$this->detailedDescription = <<<EOF |
|---|
| 42 |
The [propel:insert-sql|INFO] task creates database tables: |
|---|
| 43 |
|
|---|
| 44 |
[./symfony propel:insert-sql|INFO] |
|---|
| 45 |
|
|---|
| 46 |
The task connects to the database and executes all SQL statements |
|---|
| 47 |
found in [config/sql/*schema.sql|COMMENT] files. |
|---|
| 48 |
|
|---|
| 49 |
Before execution, the task will ask you to confirm the execution |
|---|
| 50 |
as it deletes all data in your database. |
|---|
| 51 |
|
|---|
| 52 |
To bypass the confirmation, you can pass the [--no-confirmation|COMMENT] |
|---|
| 53 |
option: |
|---|
| 54 |
|
|---|
| 55 |
[./symfony propel:insert-sql --no-confirmation|INFO] |
|---|
| 56 |
|
|---|
| 57 |
The task read the database configuration from `databases.yml`. |
|---|
| 58 |
You can use a specific application/environment by passing |
|---|
| 59 |
an [--application|INFO] or [--env|INFO] option. |
|---|
| 60 |
|
|---|
| 61 |
You can also use the [--connection|INFO] option if you want to |
|---|
| 62 |
only load SQL statements for a given connection. |
|---|
| 63 |
EOF; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* @see sfTask |
|---|
| 68 |
*/ |
|---|
| 69 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 70 |
{ |
|---|
| 71 |
if ( |
|---|
| 72 |
!$options['no-confirmation'] |
|---|
| 73 |
&& |
|---|
| 74 |
!$this->askConfirmation(array('This command will remove all data in your database.', 'Are you sure you want to proceed? (y/N)'), null, false) |
|---|
| 75 |
) |
|---|
| 76 |
{ |
|---|
| 77 |
$this->logSection('propel', 'Task aborted.'); |
|---|
| 78 |
|
|---|
| 79 |
return 1; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
$this->schemaToXML(self::DO_NOT_CHECK_SCHEMA, 'generated-'); |
|---|
| 83 |
$this->copyXmlSchemaFromPlugins('generated-'); |
|---|
| 84 |
|
|---|
| 85 |
$databaseManager = new sfDatabaseManager($this->configuration); |
|---|
| 86 |
|
|---|
| 87 |
$properties = $this->getProperties(sfConfig::get('sf_data_dir').'/sql/sqldb.map'); |
|---|
| 88 |
$sqls = array(); |
|---|
| 89 |
foreach ($properties as $file => $connection) |
|---|
| 90 |
{ |
|---|
| 91 |
if (!is_null($options['connection']) && $options['connection'] != $connection) |
|---|
| 92 |
{ |
|---|
| 93 |
continue; |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
if (!isset($sqls[$connection])) |
|---|
| 97 |
{ |
|---|
| 98 |
$sqls[$connection] = array(); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
$sqls[$connection][] = $file; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
$this->tmpDir = sfToolkit::getTmpDir().'/propel_insert_sql_'.rand(11111, 99999); |
|---|
| 105 |
register_shutdown_function(array($this, 'removeTmpDir')); |
|---|
| 106 |
mkdir($this->tmpDir, 0777, true); |
|---|
| 107 |
foreach ($sqls as $connection => $files) |
|---|
| 108 |
{ |
|---|
| 109 |
$dir = $this->tmpDir.'/'.$connection; |
|---|
| 110 |
mkdir($dir, 0777, true); |
|---|
| 111 |
|
|---|
| 112 |
$content = ''; |
|---|
| 113 |
foreach ($files as $file) |
|---|
| 114 |
{ |
|---|
| 115 |
$content .= "$file=$connection\n"; |
|---|
| 116 |
copy(sfConfig::get('sf_data_dir').'/sql/'.$file, $dir.'/'.$file); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
file_put_contents($dir.'/sqldb.map', $content); |
|---|
| 120 |
$properties = $this->getPhingPropertiesForConnection($databaseManager, $connection); |
|---|
| 121 |
$properties['propel.sql.dir'] = $dir; |
|---|
| 122 |
|
|---|
| 123 |
$ret = $this->callPhing('insert-sql', self::CHECK_SCHEMA, $properties); |
|---|
| 124 |
} |
|---|
| 125 |
$this->removeTmpDir(); |
|---|
| 126 |
|
|---|
| 127 |
$this->cleanup(); |
|---|
| 128 |
|
|---|
| 129 |
return !$ret; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
public function removeTmpDir() |
|---|
| 133 |
{ |
|---|
| 134 |
if (!is_dir($this->tmpDir)) |
|---|
| 135 |
{ |
|---|
| 136 |
return; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
sfToolkit::clearDirectory($this->tmpDir); |
|---|
| 140 |
rmdir($this->tmpDir); |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|