m130705_222015_multiple_backends::up()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 24
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 37
rs 9.536
1
<?php
2
3
class m130705_222015_multiple_backends extends CDbMigration
4
{
5
6
	public function up()
7
	{
8
		// Check if this migration needs to be applied at all
9
		if (Yii::app()->db->schema->getTable('config') === null)
10
			return;
11
		
12
		// Retrieve the current configuration
13
		$backend = array();
14
		$config = Yii::app()->db->createCommand('SELECT * FROM `config`')->queryAll();
15
16
		// Turn the result into something usable
17
		foreach ($config as $row)
18
			$backend[$row['property']] = $row['value'];
19
20
		// Create the "backends" table and drop the old "config" table
21
		$this->createTable('backend', array(
22
			'id'=>'INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL',
23
			'name'=>'VARCHAR NOT NULL',
24
			'hostname'=>'VARCHAR NOT NULL',
25
			'port'=>'INTEGER NOT NULL',
26
			'username'=>'VARCHAR NOT NULL',
27
			'password'=>'VARCHAR NOT NULL',
28
			'proxyLocation'=>'VARCHAR',
29
			'default'=>'INTEGER NOT NULL'
30
		));
31
32
		$this->dropTable('config');
33
34
		// Insert the previous configuration as a new backend
35
		$this->insert('backend', array(
36
			'name'=>'Default backend',
37
			'hostname'=>$backend['hostname'],
38
			'port'=>$backend['port'],
39
			'username'=>$backend['username'],
40
			'password'=>$backend['password'],
41
			'proxyLocation'=>$backend['proxyLocation'],
42
			'default'=>1,
43
		));
44
	}
45
46
	public function down()
47
	{
48
		echo "m130705_222015_multiple_backends does not support migration down.\n";
49
		return false;
50
	}
51
52
}