Passed
Push — master ( 2a5595...992182 )
by Aimeos
04:19
created

OrderMigrateTaxrate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 121
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPostDependencies() 0 3 1
A getPreDependencies() 0 3 1
B migrate() 0 94 9
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2019
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Migrates the tax rate in order tables
14
 */
15
class OrderMigrateTaxrate extends \Aimeos\MW\Setup\Task\Base
16
{
17
	/**
18
	 * Returns the list of task names which this task depends on.
19
	 *
20
	 * @return string[] List of task names
21
	 */
22
	public function getPreDependencies()
23
	{
24
		return array( 'TablesCreateMShop' );
25
	}
26
27
28
	/**
29
	 * Returns the list of task names which depends on this task.
30
	 *
31
	 * @return string[] List of task names
32
	 */
33
	public function getPostDependencies()
34
	{
35
		return [];
36
	}
37
38
39
	/**
40
	 * Migrate database schema
41
	 */
42
	public function migrate()
43
	{
44
		$dbdomain = 'db-order';
45
		$this->msg( 'Migrating taxrate column in order tables', 0 ); $this->status( '' );
46
47
		if( $this->getSchema( $dbdomain )->tableExists( 'mshop_order_base_product' ) === true )
48
		{
49
			$this->msg( 'Migrating taxrate column in order base product table', 1 );
50
51
			$start = 0;
52
			$conn = $this->acquire( $dbdomain );
53
			$select = 'SELECT "id", "taxrate" FROM "mshop_order_base_product" WHERE "taxrate" NOT LIKE \'{%\' LIMIT 1000 OFFSET :offset';
54
			$update = 'UPDATE "mshop_order_base_product" SET "taxrate" = ? WHERE "id" = ?';
55
56
			$stmt = $conn->create( $update, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
57
58
			do
59
			{
60
				$count = 0;
61
				$map = [];
62
				$sql = str_replace( ':offset', $start, $select );
63
				$result = $conn->create( $sql )->execute();
64
65
				while( ( $row = $result->fetch() ) !== false )
66
				{
67
					$map[$row['id']] = $row['taxrate'];
68
					$count++;
69
				}
70
71
				foreach( $map as $id => $taxrate )
72
				{
73
					$stmt->bind( 1, json_encode( ['' => $taxrate], JSON_FORCE_OBJECT ) );
74
					$stmt->bind( 2, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
75
76
					$stmt->execute()->finish();
77
				}
78
79
				$start += $count;
80
			}
81
			while( $count === 1000 );
82
83
			$this->release( $conn, $dbdomain );
84
85
			$this->status( 'done' );
86
		}
87
		else
88
		{
89
			$this->status( 'OK' );
90
		}
91
92
93
		if( $this->getSchema( $dbdomain )->tableExists( 'mshop_order_base_service' ) === true )
94
		{
95
			$this->msg( 'Migrating taxrate column in order base service table', 1 );
96
97
			$start = 0;
98
			$conn = $this->acquire( $dbdomain );
99
			$select = 'SELECT "id", "taxrate" FROM "mshop_order_base_service" WHERE "taxrate" NOT LIKE \'{%\' LIMIT 1000 OFFSET :offset';
100
			$update = 'UPDATE "mshop_order_base_service" SET "taxrate" = ? WHERE "id" = ?';
101
102
			$stmt = $conn->create( $update, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
103
104
			do
105
			{
106
				$count = 0;
107
				$map = [];
108
				$sql = str_replace( ':offset', $start, $select );
109
				$result = $conn->create( $sql )->execute();
110
111
				while( ( $row = $result->fetch() ) !== false )
112
				{
113
					$map[$row['id']] = $row['taxrate'];
114
					$count++;
115
				}
116
117
				foreach( $map as $id => $taxrate )
118
				{
119
					$stmt->bind( 1, json_encode( ['' => $taxrate], JSON_FORCE_OBJECT ) );
120
					$stmt->bind( 2, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
121
122
					$stmt->execute()->finish();
123
				}
124
125
				$start += $count;
126
			}
127
			while( $count === 1000 );
128
129
			$this->release( $conn, $dbdomain );
130
131
			$this->status( 'done' );
132
		}
133
		else
134
		{
135
			$this->status( 'OK' );
136
		}
137
	}
138
}
139