Completed
Push — master ( 0bfde0...914031 )
by Aimeos
08:19
created

ProductChangeStockProductidParentid::migrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2017
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Renames the "prodid" column to "parentid"
14
 */
15
class ProductChangeStockProductidParentid extends \Aimeos\MW\Setup\Task\Base
16
{
17
	private $mysql = array(
18
		'prodid' => array(
19
			'ALTER TABLE "mshop_product_stock" DROP FOREIGN KEY "fk_msprost_prodid"',
20
			'ALTER TABLE "mshop_product_stock" CHANGE "prodid" "parentid" INTEGER NOT NULL',
21
			'ALTER TABLE "mshop_product_stock" ADD CONSTRAINT "fk_msprost_parentid" FOREIGN KEY ("parentid") REFERENCES "mshop_product" ("id") ON UPDATE CASCADE ON DELETE CASCADE',
22
		),
23
	);
24
25
26
	/**
27
	 * Returns the list of task names which this task depends on.
28
	 *
29
	 * @return array List of task names
30
	 */
31
	public function getPreDependencies()
32
	{
33
		return array();
34
	}
35
36
37
	/**
38
	 * Returns the list of task names which depends on this task.
39
	 *
40
	 * @return string[] List of task names
41
	 */
42
	public function getPostDependencies()
43
	{
44
		return array( 'TablesCreateMShop' );
45
	}
46
47
48
	/**
49
	 * Executes the task for MySQL databases.
50
	 */
51
	public function migrate()
52
	{
53
		$this->process( $this->mysql );
54
	}
55
56
57
	/**
58
	 * Changes the column in table
59
	 *
60
	 * array string $stmts List of SQL statements for changing the columns
61
	 */
62
	protected function process( array $stmts )
63
	{
64
		$table = 'mshop_product_stock';
65
		$this->msg( sprintf( 'Rename "prodid" to "parentid" in table "%1$s"', $table ), 0 ); $this->status( '' );
66
67
		foreach( $stmts as $column => $stmts )
68
		{
69
			$this->msg( sprintf( 'Checking column "%1$s"', $column ), 1 );
70
71
			if( $this->schema->tableExists( $table )
72
				&& $this->schema->columnExists( $table, $column ) === true
73
			) {
74
				$this->executeList( $stmts );
75
				$this->status( 'done' );
76
			} else {
77
				$this->status( 'OK' );
78
			}
79
		}
80
	}
81
}
82