Passed
Push — master ( 484f92...9dd3d0 )
by Aimeos
04:56
created

MediaMigratePreview::getPreDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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), 2019
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Migrates the preview URLs in media table
14
 */
15
class MediaMigratePreview 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 preview columns in media table', 0 );
46
47
		if( $this->getSchema( $dbdomain )->tableExists( 'mshop_media' ) === false )
48
		{
49
			$this->status( 'OK' );
50
			return;
51
		}
52
53
		$start = 0;
54
		$conn = $this->acquire( $dbdomain );
55
		$select = 'SELECT "id", "preview" FROM "mshop_media" WHERE "preview" NOT LIKE \'{%\' LIMIT 1000 OFFSET :offset';
56
		$update = 'UPDATE "mshop_media" SET "preview" = ? WHERE "id" = ?';
57
58
		$stmt = $conn->create( $update, \Aimeos\MW\DB\Connection\Base::TYPE_PREP );
59
60
		do
61
		{
62
			$count = 0;
63
			$map = [];
64
			$sql = str_replace( ':offset', $start, $select );
65
			$result = $conn->create( $sql )->execute();
66
67
			while( ( $row = $result->fetch() ) !== false )
68
			{
69
				$map[$row['id']] = $row['preview'];
70
				$count++;
71
			}
72
73
			foreach( $map as $id => $preview )
74
			{
75
				$stmt->bind( 1, json_encode( ['1' => $preview] ) );
76
				$stmt->bind( 2, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
77
78
				$stmt->execute()->finish();
79
			}
80
81
			$start += $count;
82
		}
83
		while( $count === 1000 );
84
85
		$this->release( $conn, $dbdomain );
86
87
		$this->status( 'done' );
88
	}
89
}
90