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

ProductChangeTypeidNotNull::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 Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2017
7
 */
8
9
10
namespace Aimeos\MW\Setup\Task;
11
12
13
/**
14
 * Changes product typeid column to NOT NULL.
15
 */
16
class ProductChangeTypeidNotNull extends \Aimeos\MW\Setup\Task\Base
17
{
18
	private $mysql = array(
19
		'UPDATE "mshop_product"
20
			SET "typeid" = ( SELECT "id" FROM "mshop_product_type" WHERE "siteid" IS NULL AND "domain" = \'product\' AND "code" = \'product\' )
21
			WHERE "typeid" IS NULL',
22
		'ALTER TABLE "mshop_product" CHANGE "typeid" "typeid" INTEGER NOT NULL',
23
	);
24
25
26
	/**
27
	 * Returns the list of task names which this task depends on.
28
	 *
29
	 * @return string[] List of task names
30
	 */
31
	public function getPreDependencies()
32
	{
33
		return array( 'ProductAddTypeid' );
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
	 * Add column to table if it doesn't exist.
58
	 *
59
	 * @param array $stmts List of SQL statements to execute for adding columns
60
	 */
61
	protected function process( $stmts )
62
	{
63
		$this->msg( 'Changing typeid column of product table', 0 ); $this->status( '' );
64
65
		$this->msg( sprintf( 'Checking table "%1$s": ', 'mshop_product' ), 1 );
66
67
		if( $this->schema->tableExists( 'mshop_product' ) === true
68
			&& $this->schema->columnExists( 'mshop_product', 'typeid' ) === true
69
			&& $this->schema->getColumnDetails( 'mshop_product', 'typeid' )->isNullable() === true )
70
		{
71
			$this->executeList( $stmts );
72
			$this->status( 'migrated' );
73
		}
74
		else
75
		{
76
			$this->status( 'OK' );
77
		}
78
	}
79
}