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

ProductWarehouseAddLabelStatus::migrate()   D

Complexity

Conditions 9
Paths 25

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 25
nop 0
dl 0
loc 41
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Adds label and status to warehouse table.
15
 */
16
class ProductWarehouseAddLabelStatus extends \Aimeos\MW\Setup\Task\Base
17
{
18
	private $mysql = array(
19
		'mshop_product_warehouse' => array(
20
			'label'  => 'ALTER TABLE "mshop_product_warehouse" ADD "label" VARCHAR(255) NOT NULL',
21
			'status' => 'ALTER TABLE "mshop_product_warehouse" ADD "status" SMALLINT NOT NULL DEFAULT 0 AFTER label',
22
		),
23
	);
24
25
	private $update = array(
26
		'mshop_product_warehouse' => array(
27
			'status' => 'UPDATE "mshop_product_warehouse" SET status = 1 WHERE label = \'\'',
28
			'label' => 'UPDATE "mshop_product_warehouse" SET label = code WHERE label = \'\'',
29
		),
30
	);
31
32
33
	/**
34
	 * Returns the list of task names which this task depends on.
35
	 *
36
	 * @return array List of task names
37
	 */
38
	public function getPreDependencies()
39
	{
40
		return [];
41
	}
42
43
44
	/**
45
	 * Returns the list of task names which depends on this task.
46
	 *
47
	 * @return string[] List of task names
48
	 */
49
	public function getPostDependencies()
50
	{
51
		return array( 'TablesCreateMShop' );
52
	}
53
54
55
	/**
56
	 * Executes the task for MySQL databases.
57
	 */
58
	public function migrate()
59
	{
60
		$this->msg( sprintf( 'Adding label and status columns for product warehouse' ), 0 );
61
		$this->status( '' );
62
63
		foreach( $this->mysql as $table => $columns ) {
64
65
			if( $this->schema->tableExists( $table ) ) {
66
67
				foreach( $columns as $column => $stmt ) {
68
69
					$this->msg( sprintf( 'Checking column "%1$s.%2$s": ', $table, $column ), 1 );
70
71
					if( !$this->schema->columnExists( $table, $column ) ) {
72
						$this->execute( $stmt );
73
						$this->status( 'added' );
74
					} else {
75
						$this->status( 'OK' );
76
					}
77
				}
78
			}
79
		}
80
81
82
		foreach( $this->update as $table => $columns ) {
83
84
			if( $this->schema->tableExists( $table ) ) {
85
86
				foreach( $columns as $column => $stmt ) {
87
88
					$this->msg( sprintf( 'Update column "%1$s.%2$s": ', $table, $column ), 1 );
89
90
					if( $this->schema->columnExists( $table, $column ) ) {
91
						$this->execute( $stmt );
92
						$this->status( 'updated' );
93
					}
94
				}
95
			}
96
		}
97
98
	}
99
}