| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
require_once(dirname(__FILE__).'/sfPropelBaseTask.class.php');
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Inserts SQL for current model.
|
|
|
15 |
*
|
|
|
16 |
* @package symfony
|
|
|
17 |
* @subpackage propel
|
|
|
18 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
19 |
* @version SVN: $Id: sfPropelInsertSqlTask.class.php 23922 2009-11-14 14:58:38Z fabien $
|
|
|
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->namespace = 'propel';
|
|
|
37 |
$this->name = 'insert-sql';
|
|
|
38 |
$this->briefDescription = 'Inserts SQL for current model';
|
|
|
39 |
|
|
|
40 |
$this->detailedDescription = <<<EOF
|
|
|
41 |
The [propel:insert-sql|INFO] task creates database tables:
|
|
|
42 |
|
|
|
43 |
[./symfony propel:insert-sql|INFO]
|
|
|
44 |
|
|
|
45 |
The task connects to the database and executes all SQL statements
|
|
|
46 |
found in [config/sql/*schema.sql|COMMENT] files.
|
|
|
47 |
|
|
|
48 |
Before execution, the task will ask you to confirm the execution
|
|
|
49 |
as it deletes all data in your database.
|
|
|
50 |
|
|
|
51 |
To bypass the confirmation, you can pass the [--no-confirmation|COMMENT]
|
|
|
52 |
option:
|
|
|
53 |
|
|
|
54 |
[./symfony propel:insert-sql --no-confirmation|INFO]
|
|
|
55 |
|
|
|
56 |
The task read the database configuration from `databases.yml`.
|
|
|
57 |
You can use a specific application/environment by passing
|
|
|
58 |
an [--application|INFO] or [--env|INFO] option.
|
|
|
59 |
|
|
|
60 |
You can also use the [--connection|INFO] option if you want to
|
|
|
61 |
only load SQL statements for a given connection.
|
|
|
62 |
EOF;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* @see sfTask
|
|
|
67 |
*/
|
|
|
68 |
protected function execute($arguments = array(), $options = array())
|
|
|
69 |
{
|
|
|
70 |
$this->schemaToXML(self::DO_NOT_CHECK_SCHEMA, 'generated-');
|
|
|
71 |
$this->copyXmlSchemaFromPlugins('generated-');
|
|
|
72 |
|
|
|
73 |
$databaseManager = new sfDatabaseManager($this->configuration);
|
|
|
74 |
|
|
|
75 |
$properties = $this->getProperties(sfConfig::get('sf_data_dir').'/sql/sqldb.map');
|
|
|
76 |
$sqls = array();
|
|
|
77 |
foreach ($properties as $file => $connection)
|
|
|
78 |
{
|
|
|
79 |
if (null !== $options['connection'] && $options['connection'] != $connection)
|
|
|
80 |
{
|
|
|
81 |
continue;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if (!isset($sqls[$connection]))
|
|
|
85 |
{
|
|
|
86 |
$sqls[$connection] = array();
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$sqls[$connection][] = $file;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if (
|
|
|
93 |
!$options['no-confirmation']
|
|
|
94 |
&&
|
|
|
95 |
!$this->askConfirmation(array(
|
|
|
96 |
'WARNING: The data in the database'.(count($sqls) > 1 ? 's' : '').' related to the connection name'.(count($sqls) > 1 ? 's' : ''),
|
|
|
97 |
sprintf(' %s will be removed.', implode(', ', array_keys($sqls))),
|
|
|
98 |
'',
|
|
|
99 |
'Are you sure you want to proceed? (y/N)',
|
|
|
100 |
), 'QUESTION_LARGE', false)
|
|
|
101 |
)
|
|
|
102 |
{
|
|
|
103 |
$this->logSection('propel', 'Task aborted.');
|
|
|
104 |
|
|
|
105 |
return 1;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
$this->tmpDir = sys_get_temp_dir().'/propel_insert_sql_'.rand(11111, 99999);
|
|
|
109 |
register_shutdown_function(array($this, 'removeTmpDir'));
|
|
|
110 |
mkdir($this->tmpDir, 0777, true);
|
|
|
111 |
foreach ($sqls as $connection => $files)
|
|
|
112 |
{
|
|
|
113 |
$dir = $this->tmpDir.'/'.$connection;
|
|
|
114 |
mkdir($dir, 0777, true);
|
|
|
115 |
|
|
|
116 |
$content = '';
|
|
|
117 |
foreach ($files as $file)
|
|
|
118 |
{
|
|
|
119 |
$content .= "$file=$connection\n";
|
|
|
120 |
copy(sfConfig::get('sf_data_dir').'/sql/'.$file, $dir.'/'.$file);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
file_put_contents($dir.'/sqldb.map', $content);
|
|
|
124 |
$properties = $this->getPhingPropertiesForConnection($databaseManager, $connection);
|
|
|
125 |
$properties['propel.sql.dir'] = $dir;
|
|
|
126 |
|
|
|
127 |
$ret = $this->callPhing('insert-sql', self::CHECK_SCHEMA, $properties);
|
|
|
128 |
}
|
|
|
129 |
$this->removeTmpDir();
|
|
|
130 |
|
|
|
131 |
$this->cleanup();
|
|
|
132 |
|
|
|
133 |
return !$ret;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public function removeTmpDir()
|
|
|
137 |
{
|
|
|
138 |
if (!is_dir($this->tmpDir))
|
|
|
139 |
{
|
|
|
140 |
return;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
sfToolkit::clearDirectory($this->tmpDir);
|
|
|
144 |
rmdir($this->tmpDir);
|
|
|
145 |
}
|
|
|
146 |
}
|