Issues (40)

lib/Migration/Version010901Date20231008222500.php (5 issues)

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.1 level from the v1.4.0 level
14
 */
15
class Version010901Date20231008222500 extends SimpleMigrationStep {
16
17
	/**
18
	 * @param IOutput $output
19
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
20
	 * @param array $options
21
	 * @return void
22
	 */
23
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
24
	}
25
26
	/**
27
	 * @param IOutput $output
28
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
29
	 * @param array $options
30
	 * @return null|ISchemaWrapper
31
	 */
32
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
33
		/** @var ISchemaWrapper $schema */
34
		$schema = $schemaClosure();
35
		$this->fixInconsistentIdTypes($schema);
36
		$this->allowNegativeYear($schema);
37
		$this->ampacheSessionChanges($schema);
38
		$this->addRatingFields($schema);
39
		return $schema;
40
	}
41
42
	/**
43
	 * @param IOutput $output
44
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
45
	 * @param array $options
46
	 * @return void
47
	 */
48
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
49
	}
50
51
	/**
52
	 * Some of the foreign keys referring to entity IDs have been previously defined as signed
53
	 * although the referred primary key has always been unsigned.
54
	 */
55
	private function fixInconsistentIdTypes(ISchemaWrapper $schema) : void {
56
		$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

56
		/** @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...
57
		$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

57
		/** @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...
58
										->changeColumn('album_id', ['unsigned' => true]);
59
		$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

59
		/** @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...
60
		$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

60
		/** @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...
61
	}
62
63
	/**
64
	 * Although untypical, it's not totally impossible that some historical piece of music would
65
	 * be tagged with a negative year indicating a year BCE.
66
	 */
67
	private function allowNegativeYear(ISchemaWrapper $schema) : void {
68
		$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

68
		/** @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...
69
	}
70
71
	/**
72
	 * Add the new fields to the `music_ampache_sessions` table
73
	 */
74
	private function ampacheSessionChanges(ISchemaWrapper $schema) : void {
75
		// On SQLite, it's not possible to add notnull columns to an existing table without a default value, see
76
		// https://stackoverflow.com/questions/3170634/cannot-add-a-not-null-column-with-default-value-null-in-sqlite3.
77
78
		// The problem mentioned above was tried to be worked around in the commit f9a95569 by dropping and recreating
79
		// the table and it did help in some environments but not everywhere. Next, it was attempted to do the dropping
80
		// and recreating in separate migration steps; this worked in some more environments but the problem still
81
		// remained in others. It was not figured out, what was the important difference between the working and not
82
		// working environments but the problem was seen for example on NC 21.0.2 + SQLite 3.8.7.1.
83
84
		// In the end, the only systematically working solution seemed to be to make the `ampache_user_id` column
85
		// nullable (sigh).
86
		$table = $schema->getTable('music_ampache_sessions');
87
		$this->setColumn($table, 'api_version', 'string', ['notnull' => false, 'length' => 16]);
88
		$this->setColumn($table, 'ampache_user_id', 'integer', ['notnull' => false, 'unsigned' => true]);
89
	}
90
91
	/**
92
	 * Add the new field 'rating' to applicable tables
93
	 */
94
	private function addRatingFields(ISchemaWrapper $schema) : void {
95
		$tableNames = [
96
			'music_artists',
97
			'music_albums',
98
			'music_tracks',
99
			'music_playlists',
100
			'music_podcast_channels',
101
			'music_podcast_episodes'
102
		];
103
104
		foreach ($tableNames as $tableName) {
105
			$table = $schema->getTable($tableName);
106
			$this->setColumn($table, 'rating', 'integer', ['notnull' => true, 'default' => 0]);
107
		}
108
109
		// Also, add 'starred' field for playlists
110
		$this->setColumn($schema->getTable('music_playlists'), 'starred', 'datetime', ['notnull' => false]);
111
	}
112
113
	/**
114
	 * @param \Doctrine\DBAL\Schema\Table $table
115
	 */
116
	private function setColumn($table, string $name, string $type, array $args) : void {
117
		if (!$table->hasColumn($name)) {
118
			$table->addColumn($name, $type, $args);
119
		}
120
	}
121
}
122