Skip to content
This repository
Browse code

Correção nas mensagens do shell

Adição de opção -f para varrer todas as tabelas do bd ao invés de apenas os modelos
  • Loading branch information...
commit eba748e94938e1c2c7e571c6ab63eecc544d3feb 1 parent 7869e0e
Cauan Cabral authored

Showing 1 changed file with 91 additions and 74 deletions. Show diff stats Hide diff stats

  1. 165  Console/Command/AuditableShell.php
165  Console/Command/AuditableShell.php
@@ -14,6 +14,7 @@
14 14
  */
15 15
 App::uses('Shell', 'Console');
16 16
 App::uses('ConnectionManager', 'Model');
  17
+App::uses('CakeSchema', 'Model');
17 18
 
18 19
 /**
19 20
  * Auditable Shell.
@@ -43,14 +44,14 @@ class AuditableShell extends Shell {
43 44
 	public $ignoredModels = array(
44 45
 		'Log',
45 46
 		'LogDetail',
46  
-		'Aro',
47  
-		'Aco',
48 47
 	);
49 48
 
50 49
 	public $ignoredTables = array(
  50
+		'acos',
  51
+		'aros',
  52
+		'aros_acos',
51 53
 		'logs',
52 54
 		'sessions',
53  
-		'aros_acos',
54 55
 		'schema_migrations',
55 56
 	);
56 57
 
@@ -59,7 +60,8 @@ class AuditableShell extends Shell {
59 60
  *
60 61
  * @return void
61 62
  */
62  
-	public function startup() {
  63
+	public function startup()
  64
+	{
63 65
 		$this->out(__d('Auditable', 'Cake Auditable Shell'));
64 66
 		$this->hr();
65 67
 
@@ -72,7 +74,8 @@ public function startup() {
72 74
  *
73 75
  * @return void
74 76
  */
75  
-	public function getOptionParser() {
  77
+	public function getOptionParser()
  78
+	{
76 79
 		$parser = parent::getOptionParser();
77 80
 		return $parser->description(
78 81
 			'The Auditable shell.' .
@@ -81,6 +84,10 @@ public function getOptionParser() {
81 84
 					'short' => 'c',
82 85
 					'default' => 'default',
83 86
 					'help' => __d('Auditable', 'Set db config <config>. Uses \'default\' if none is specified.')))
  87
+			->addOption('force', array(
  88
+					'short' => 'f',
  89
+					'boolean' => true,
  90
+					'help' => __d('Auditable', 'Force changes in all tables of database.')))
84 91
 			->addSubcommand('insert', array(
85 92
 				'help' => __d('Auditable', 'Insert columns \'created_by\' and \'modified_by\' in all database tables.')))
86 93
 			->addSubcommand('remove', array(
@@ -92,7 +99,8 @@ public function getOptionParser() {
92 99
  *
93 100
  * @return void
94 101
  */
95  
-	public function main() {
  102
+	public function main()
  103
+	{
96 104
 		$this->run();
97 105
 	}
98 106
 
@@ -101,7 +109,8 @@ public function main() {
101 109
  *
102 110
  * @return void
103 111
  */
104  
-	public function run() {
  112
+	public function run()
  113
+	{
105 114
 		$null = null;
106 115
 		$this->db =& ConnectionManager::getDataSource($this->connection);
107 116
 		$this->db->cacheSources = false;
@@ -114,12 +123,7 @@ public function run() {
114 123
 		}
115 124
 
116 125
 		try {
117  
-			if($this->args[0] === 'insert')
118  
-				$this->_insert();
119  
-
120  
-			if($this->args[0] === 'remove')
121  
-				$this->_remove();
122  
-
  126
+			$this->_run($this->args[0]);
123 127
 			$this->_clearCache();
124 128
 
125 129
 		} catch (Exception $e) {
@@ -134,86 +138,68 @@ public function run() {
134 138
 		return true;
135 139
 	}
136 140
 
137  
-	protected function _insert() {
138  
-		$fieldOptions = array('type' => 'integer', 'null' => true, 'default' => null);
139  
-		$models = $this->_getModels();
  141
+	protected function _run($type)
  142
+	{
  143
+		if(!isset($this->params['force']))
  144
+			$tables = $this->_getModels();
  145
+		else
  146
+			$tables = $this->_getTables();
  147
+
  148
+		if($type === 'insert')
  149
+			$this->out(__d('Auditable', 'Adding fields'));
  150
+		else
  151
+			$this->out(__d('Auditable', 'Droping fields'));
140 152
 
141  
-		$this->out(__d('Auditable', 'Adding fields'));
142 153
 		$this->out('');
143 154
 
144  
-		foreach($models as $modelName)
  155
+		foreach($tables as $tableName => $schema)
145 156
 		{
146  
-			$_model = ClassRegistry::init($modelName);
147  
-
148  
-			if(!isset($_model->table) || empty($_model->table) || in_array($_model->table, $this->ignoredTables) || $_model->useDbConfig !== $this->connection)
149  
-				continue;
150 157
 
151  
-			$schema = $_model->schema(true);
152  
-
153  
-			if(empty($schema))
154  
-				continue;
  158
+			$status = $this->{'_' . $type}($schema, $tableName);
155 159
 
156  
-			$tableName = $_model->tablePrefix ? $_model->tablePrefix . $_model->table : $_model->table;
157  
-
158  
-			$changes = array('add' => array());
159  
-
160  
-			if(!isset($schema['created_by']))
161  
-				$changes[$tableName]['add']['created_by'] = $fieldOptions;
162  
-
163  
-			if(!isset($schema['modified_by']))
164  
-				$changes[$tableName]['add']['modified_by'] = $fieldOptions;
165  
-
166  
-			$sql = $this->db->alterSchema($changes);
167  
-
168  
-			if(empty($sql))
169  
-				continue;
170  
-
171  
-			$status = $this->_execute($sql) ? 'Sucesso' : 'Falha';
172  
-
173  
-			$this->out(sprintf(__d('Auditable', 'Changing table \'%s\': %s'), $tableName, $status));
  160
+			if($status !== null)
  161
+			{
  162
+				$this->out(sprintf(__d('Auditable', 'Changing table \'%s\': %s'), $tableName, $status ? __d('Auditable', 'Success') : __d('Auditable', 'Error')));
  163
+			}
174 164
 		}
175 165
 	}
176 166
 
177  
-	protected function _remove()
  167
+	protected function _insert($schema, $tableName)
178 168
 	{
179  
-		$models = $this->_getModels();
  169
+		$fieldOptions = array('type' => 'integer', 'null' => true, 'default' => null);
180 170
 
181  
-		$this->out(__d('Auditable', 'Droping fields'));
182  
-		$this->out('');
  171
+		$changes = array('add' => array());
183 172
 
184  
-		foreach($models as $modelName)
185  
-		{
186  
-			$_model = ClassRegistry::init($modelName);
  173
+		if(!isset($schema['created_by']))
  174
+			$changes[$tableName]['add']['created_by'] = $fieldOptions;
187 175
 
188  
-			if(!isset($_model->table) || empty($_model->table) || in_array($_model->table, $this->ignoredTables) || $_model->useDbConfig !== $this->connection)
189  
-				continue;
  176
+		if(!isset($schema['modified_by']))
  177
+			$changes[$tableName]['add']['modified_by'] = $fieldOptions;
190 178
 
191  
-			$schema = $_model->schema(true);
  179
+		$sql = $this->db->alterSchema($changes);
192 180
 
193  
-			if(empty($schema))
194  
-				continue;
195  
-
196  
-			$tableName = $_model->tablePrefix ? $_model->tablePrefix . $_model->table : $_model->table;
  181
+		if(empty($sql))
  182
+			return null;
197 183
 
198  
-			$changes = array('drop' => array());
199  
-
200  
-			if(isset($schema['created_by']))
201  
-				$changes[$tableName]['drop']['created_by'] = array();
  184
+		return (bool)$this->_execute($sql);
  185
+	}
202 186
 
203  
-			if(isset($schema['modified_by']))
204  
-				$changes[$tableName]['drop']['modified_by'] = array();
  187
+	protected function _remove($schema, $tableName)
  188
+	{
  189
+		$changes = array('drop' => array());
205 190
 
206  
-			$sql = $this->db->alterSchema($changes);
  191
+		if(isset($schema['created_by']))
  192
+			$changes[$tableName]['drop']['created_by'] = array();
207 193
 
208  
-			if(empty($sql))
209  
-				continue;
  194
+		if(isset($schema['modified_by']))
  195
+			$changes[$tableName]['drop']['modified_by'] = array();
210 196
 
211  
-			$status = $this->_execute($sql) ? 'Sucesso' : 'Falha';
  197
+		$sql = $this->db->alterSchema($changes);
212 198
 
213  
-			$this->out(sprintf(__d('Auditable', 'Changing table \'%s\': %s'), $tableName, $status));
214  
-		}
  199
+		if(empty($sql))
  200
+			return null;
215 201
 
216  
-		return true;
  202
+		return (bool)$this->_execute($sql);
217 203
 	}
218 204
 
219 205
 	protected function _execute($sql)
@@ -227,7 +213,6 @@ protected function _execute($sql)
227 213
 	protected function _getModels()
228 214
 	{
229 215
 		$models = App::objects('Model');
230  
-
231 216
 		$plugins = CakePlugin::loaded();
232 217
 
233 218
 		foreach($plugins as $plugin)
@@ -246,13 +231,45 @@ protected function _getModels()
246 231
 			}
247 232
 		}
248 233
 
  234
+		$out = array();
249 235
 		foreach($models as $k => $m)
250 236
 		{
251 237
 			if(strpos(strtolower($m), 'appmodel') !== false)
252  
-				unset($models[$k]);
  238
+				continue;
  239
+
  240
+			$_model = ClassRegistry::init($m);
  241
+
  242
+			if(!isset($_model->table) || empty($_model->table) || in_array($_model->table, $this->ignoredModels) || $_model->useDbConfig !== $this->connection)
  243
+				continue;
  244
+
  245
+			$schema = $_model->schema(true);
  246
+
  247
+			if(empty($schema))
  248
+				continue;
  249
+
  250
+			$tableName = $_model->tablePrefix ? $_model->tablePrefix . $_model->table : $_model->table;
  251
+
  252
+			$out[$tableName] = $schema;
  253
+		}
  254
+
  255
+		return $out;
  256
+	}
  257
+
  258
+	protected function _getTables()
  259
+	{
  260
+		$_Schema = new CakeSchema();
  261
+		$database = $_Schema->read();
  262
+
  263
+		$tables = array();
  264
+		foreach($database['tables'] as $tableName => $schema)
  265
+		{
  266
+			if(in_array($tableName, $this->ignoredTables) || empty($tableName) || $tableName === 'missing')
  267
+				continue;
  268
+
  269
+			$tables[$tableName] = $schema;
253 270
 		}
254 271
 
255  
-		return $models;
  272
+		return $tables;
256 273
 	}
257 274
 
258 275
 	protected function _clearCache() {

0 notes on commit eba748e

Please sign in to comment.
Something went wrong with that request. Please try again.