Development

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

You must first sign up to be able to contribute.

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

Revision 22691, 4.1 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     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
Note: See TracBrowser for help on using the browser.