Development

/branches/1.3/lib/plugins/sfPropelPlugin/lib/task/sfPropelInsertSqlTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfPropelPlugin/lib/task/sfPropelInsertSqlTask.class.php

Revision 22691, 4.4 kB (checked in by FabianLange, 5 years ago)

[1.2, 1.3] moved code hooking cleanup method to ensure directory has been set (closes #7260)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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$
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     $this->schemaToXML(self::DO_NOT_CHECK_SCHEMA, 'generated-');
72     $this->copyXmlSchemaFromPlugins('generated-');
73
74     $databaseManager = new sfDatabaseManager($this->configuration);
75
76     $properties = $this->getProperties(sfConfig::get('sf_data_dir').'/sql/sqldb.map');
77     $sqls = array();
78     foreach ($properties as $file => $connection)
79     {
80       if (null !== $options['connection'] && $options['connection'] != $connection)
81       {
82         continue;
83       }
84
85       if (!isset($sqls[$connection]))
86       {
87         $sqls[$connection] = array();
88       }
89
90       $sqls[$connection][] = $file;
91     }
92
93     if (
94       !$options['no-confirmation']
95       &&
96       !$this->askConfirmation(array(
97           'WARNING: The data in the database'.(count($sqls) > 1 ? 's' : '').' related to the connection name'.(count($sqls) > 1 ? 's' : ''),
98           sprintf('         %s will be removed.', implode(', ', array_keys($sqls))),
99           '',
100           'Are you sure you want to proceed? (y/N)',
101         ), 'QUESTION_LARGE', false)
102     )
103     {
104       $this->logSection('propel', 'Task aborted.');
105
106       return 1;
107     }
108
109     $this->tmpDir = sys_get_temp_dir().'/propel_insert_sql_'.rand(11111, 99999);
110     register_shutdown_function(array($this, 'removeTmpDir'));
111     mkdir($this->tmpDir, 0777, true);
112     foreach ($sqls as $connection => $files)
113     {
114       $dir = $this->tmpDir.'/'.$connection;
115       mkdir($dir, 0777, true);
116
117       $content = '';
118       foreach ($files as $file)
119       {
120         $content .= "$file=$connection\n";
121         copy(sfConfig::get('sf_data_dir').'/sql/'.$file, $dir.'/'.$file);
122       }
123
124       file_put_contents($dir.'/sqldb.map', $content);
125       $properties = $this->getPhingPropertiesForConnection($databaseManager, $connection);
126       $properties['propel.sql.dir'] = $dir;
127
128       $ret = $this->callPhing('insert-sql', self::CHECK_SCHEMA, $properties);
129     }
130     $this->removeTmpDir();
131
132     $this->cleanup();
133
134     return !$ret;
135   }
136
137   public function removeTmpDir()
138   {
139     if (!is_dir($this->tmpDir))
140     {
141       return;
142     }
143
144     sfToolkit::clearDirectory($this->tmpDir);
145     rmdir($this->tmpDir);
146   }
147 }
148
Note: See TracBrowser for help on using the browser.