AddColumnMigration   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 9
c 3
b 0
f 0
dl 0
loc 38
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 6 1
A up() 0 12 3
1
<?php
2
3
/**
4
 * Base class for migrations that add a column to a table.
5
 *
6
 * @author Sam Stenvall <[email protected]>
7
 * @copyright Copyright &copy; Sam Stenvall 2014-
8
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
9
 */
10
abstract class AddColumnMigration extends CDbMigration
11
{
12
13
	/**
14
	 * @return string
15
	 */
16
	abstract protected function getTableName();
17
18
	/**
19
	 * @return string
20
	 */
21
	abstract protected function getColumnName();
22
23
	/**
24
	 * @return string
25
	 */
26
	abstract protected function getColumnType();
27
28
	public function up()
29
	{
30
		$table = Yii::app()->db->schema->getTable($this->getTableName());
31
32
		if ($table === null)
33
			return false;
34
35
		// Check if column already exists
36
		$columns = $table->getColumnNames();
37
38
		if (!in_array($this->getColumnName(), $columns))
39
			$this->addColumn($this->getTableName(), $this->getColumnName(), $this->getColumnType());
40
	}
41
42
	public function down()
43
	{
44
		$this->dropColumn($this->getTableName(), $this->getColumnName());
45
		
46
		// Clear the schema cache for this table
47
		$this->refreshTableSchema($this->getTableName());
48
	}
49
50
}
51