Passed
Push — master ( d3429f...53215a )
by Pauli
02:01
created

TimestampInit::setUpdated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2021
11
 */
12
13
namespace OCA\Music\Migration;
14
15
use OCP\IConfig;
16
use OCP\IDBConnection;
17
use OCP\Migration\IOutput;
18
use OCP\Migration\IRepairStep;
19
20
class TimestampInit implements IRepairStep {
21
22
	/** @var IDBConnection */
23
	private $db;
24
25
	/** @var IConfig */
26
	private $config;
27
28
	public function __construct(IDBConnection $connection, IConfig $config) {
29
		$this->db = $connection;
30
		$this->config = $config;
31
	}
32
33
	public function getName() {
34
		return 'Set creation and update dates for the library entities without one';
35
	}
36
37
	/**
38
	 * @inheritdoc
39
	 */
40
	public function run(IOutput $output) {
41
		$installedVersion = $this->config->getAppValue('music', 'installed_version');
42
43
		// Music version 0.18.0 added the `created` and `updated` columns to all library entity tables
44
		if (\version_compare($installedVersion, '0.18.0', '<')) {
45
			$now = new \DateTime();
46
			$timestamp = $now->format('Y-m-d H:i:s');
47
48
			$tables = ['albums', 'artists', 'genres', 'playlists', 'tracks'];
49
50
			foreach ($tables as $tableShortName) {
51
				$n = $this->setCreated("*PREFIX*music_$tableShortName", $timestamp);
52
				$m = $this->setUpdated("*PREFIX*music_$tableShortName", $timestamp);
53
				$output->info("This date was added as creation date for $n and as update date for $m $tableShortName");
54
			}
55
		}
56
	}
57
58
	private function setCreated(string $table, string $timestamp) {
59
		$sql = "UPDATE `$table` SET `created` = ? WHERE `created` IS NULL";
60
		return $this->db->executeUpdate($sql, [$timestamp]);
61
	}
62
63
	private function setUpdated(string $table, string $timestamp) {
64
		$sql = "UPDATE `$table` SET `updated` = ? WHERE `updated` IS NULL";
65
		return $this->db->executeUpdate($sql, [$timestamp]);
66
	}
67
}
68