Passed
Push — feature/909_Ampache_API_improv... ( a84036...a957d5 )
by Pauli
03:28
created

Version010900Date20230815223500   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 89
rs 10
c 1
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ampacheSessionChanges() 0 5 1
A fixInconsistentIdTypes() 0 6 1
A changeSchema() 0 8 1
A postSchemaChange() 0 1 1
A addRatingFields() 0 17 2
A preSchemaChange() 0 1 1
A setColumn() 0 3 2
A allowNegativeYear() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OCA\Music\Migration;
6
7
use Closure;
8
use OCP\DB\ISchemaWrapper;
9
use OCP\Migration\SimpleMigrationStep;
10
use OCP\Migration\IOutput;
11
12
/**
13
 * Migrate the DB schema to Music v1.9.0 level from the v1.4.0 level
14
 */
15
class Version010900Date20230815223500 extends SimpleMigrationStep {
16
17
	/**
18
	 * @param IOutput $output
19
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
20
	 * @param array $options
21
	 */
22
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
23
	}
24
25
	/**
26
	 * @param IOutput $output
27
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
28
	 * @param array $options
29
	 * @return null|ISchemaWrapper
30
	 */
31
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
32
		/** @var ISchemaWrapper $schema */
33
		$schema = $schemaClosure();
34
		$this->fixInconsistentIdTypes($schema);
35
		$this->allowNegativeYear($schema);
36
		$this->ampacheSessionChanges($schema);
37
		$this->addRatingFields($schema);
38
		return $schema;
39
	}
40
41
	/**
42
	 * @param IOutput $output
43
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
44
	 * @param array $options
45
	 */
46
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
47
	}
48
49
	/**
50
	 * Some of the foreign keys referring to entity IDs have been previously defined as signed
51
	 * although the referred primary key has always been unsigned.
52
	 */
53
	private function fixInconsistentIdTypes(ISchemaWrapper $schema) {
54
		$schema->getTable('music_albums')->changeColumn('album_artist_id', ['unsigned' => true]);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::changeColumn() has been deprecated: Use {@link modifyColumn()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
		/** @scrutinizer ignore-deprecated */ $schema->getTable('music_albums')->changeColumn('album_artist_id', ['unsigned' => true]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
55
		$schema->getTable('music_tracks')->changeColumn('artist_id', ['unsigned' => true])
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::changeColumn() has been deprecated: Use {@link modifyColumn()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
		/** @scrutinizer ignore-deprecated */ $schema->getTable('music_tracks')->changeColumn('artist_id', ['unsigned' => true])

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
56
										->changeColumn('album_id', ['unsigned' => true]);
57
		$schema->getTable('music_bookmarks')->changeColumn('entry_id', ['unsigned' => true]);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::changeColumn() has been deprecated: Use {@link modifyColumn()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
		/** @scrutinizer ignore-deprecated */ $schema->getTable('music_bookmarks')->changeColumn('entry_id', ['unsigned' => true]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
58
		$schema->getTable('music_ampache_users')->changeColumn('id', ['unsigned' => true]);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::changeColumn() has been deprecated: Use {@link modifyColumn()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

58
		/** @scrutinizer ignore-deprecated */ $schema->getTable('music_ampache_users')->changeColumn('id', ['unsigned' => true]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
59
	}
60
61
	/**
62
	 * Although untypical, it's not totally impossible that some historical piece of music would
63
	 * be tagged with a negative year indicating a year BCE.
64
	 */
65
	private function allowNegativeYear(ISchemaWrapper $schema) {
66
		$schema->getTable('music_tracks')->changeColumn('year', ['unsigned' => false]);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::changeColumn() has been deprecated: Use {@link modifyColumn()} instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

66
		/** @scrutinizer ignore-deprecated */ $schema->getTable('music_tracks')->changeColumn('year', ['unsigned' => false]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
67
	}
68
69
	/**
70
	 * Add the new fields to the `music_ampache_sessions` table
71
	 */
72
	private function ampacheSessionChanges(ISchemaWrapper $schema) {
73
		$table = $schema->getTable('music_ampache_sessions');
74
75
		$this->setColumn($table, 'api_version', 'string', ['notnull' => false, 'length' => 16]);
76
		$this->setColumn($table, 'ampache_user_id', 'int', ['notnull' => true, 'unsigned' => true]);
77
	}
78
79
	/**
80
	 * Add the new field 'rating' to applicable tables
81
	 */
82
	private function addRatingFields(ISchemaWrapper $schema) {
83
		$tableNames = [
84
			'oc_music_artists',
85
			'oc_music_albums',
86
			'oc_music_tracks',
87
			'oc_music_playlists',
88
			'oc_music_podcast_channels',
89
			'oc_music_podcast_episodes'
90
		];
91
92
		foreach ($tableNames as $tableName) {
93
			$table = $schema->getTable($tableName);
94
			$this->setColumn($table, 'rating', 'int', ['notnull' => true, 'default' => 0]);
95
		}
96
97
		// Also, add 'starred' field for playlists
98
		$this->setColumn($schema->getTable('oc_music_playlists'), 'starred', 'datetime', ['notnull' => false]);
99
	}
100
101
	private function setColumn($table, string $name, string $type, array $args) {
102
		if (!$table->hasColumn($name)) {
103
			$table->addColumn($name, $type, $args);
104
		}
105
	}
106
}
107