Skip to content

Commit

Permalink
feat(migration-attributes): tests
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Jul 29, 2024
1 parent 7c1ee52 commit ad490c9
Show file tree
Hide file tree
Showing 21 changed files with 475 additions and 30 deletions.
1 change: 1 addition & 0 deletions apps/testing/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'OCA\\Testing\\Listener\\RegisterDeclarativeSettingsListener' => $baseDir . '/../lib/Listener/RegisterDeclarativeSettingsListener.php',
'OCA\\Testing\\Listener\\SetDeclarativeSettingsValueListener' => $baseDir . '/../lib/Listener/SetDeclarativeSettingsValueListener.php',
'OCA\\Testing\\Locking\\FakeDBLockingProvider' => $baseDir . '/../lib/Locking/FakeDBLockingProvider.php',
'OCA\\Testing\\Migration\\Version30000Date20240102030405' => $baseDir . '/../lib/Migration/Version30000Date20240102030405.php',
'OCA\\Testing\\Provider\\FakeText2ImageProvider' => $baseDir . '/../lib/Provider/FakeText2ImageProvider.php',
'OCA\\Testing\\Provider\\FakeTextProcessingProvider' => $baseDir . '/../lib/Provider/FakeTextProcessingProvider.php',
'OCA\\Testing\\Provider\\FakeTextProcessingProviderSync' => $baseDir . '/../lib/Provider/FakeTextProcessingProviderSync.php',
Expand Down
1 change: 1 addition & 0 deletions apps/testing/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ComposerStaticInitTesting
'OCA\\Testing\\Listener\\RegisterDeclarativeSettingsListener' => __DIR__ . '/..' . '/../lib/Listener/RegisterDeclarativeSettingsListener.php',
'OCA\\Testing\\Listener\\SetDeclarativeSettingsValueListener' => __DIR__ . '/..' . '/../lib/Listener/SetDeclarativeSettingsValueListener.php',
'OCA\\Testing\\Locking\\FakeDBLockingProvider' => __DIR__ . '/..' . '/../lib/Locking/FakeDBLockingProvider.php',
'OCA\\Testing\\Migration\\Version30000Date20240102030405' => __DIR__ . '/..' . '/../lib/Migration/Version30000Date20240102030405.php',
'OCA\\Testing\\Provider\\FakeText2ImageProvider' => __DIR__ . '/..' . '/../lib/Provider/FakeText2ImageProvider.php',
'OCA\\Testing\\Provider\\FakeTextProcessingProvider' => __DIR__ . '/..' . '/../lib/Provider/FakeTextProcessingProvider.php',
'OCA\\Testing\\Provider\\FakeTextProcessingProviderSync' => __DIR__ . '/..' . '/../lib/Provider/FakeTextProcessingProviderSync.php',
Expand Down
41 changes: 41 additions & 0 deletions apps/testing/lib/Migration/Version30000Date20240102030405.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Testing\Migration;

use Closure;
use OCP\Migration\Attributes\AddColumn;
use OCP\Migration\Attributes\AddIndex;
use OCP\Migration\Attributes\ColumnType;
use OCP\Migration\Attributes\CreateTable;
use OCP\Migration\Attributes\DropColumn;
use OCP\Migration\Attributes\DropIndex;
use OCP\Migration\Attributes\DropTable;
use OCP\Migration\Attributes\IndexType;
use OCP\Migration\Attributes\ModifyColumn;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

#[DropTable('old_table')]
#[CreateTable('new_table', description: 'Table is used to store things, but also to get more things', notes: ['this is a notice', 'and another one, if really needed'])]
#[AddColumn('my_table')]
#[AddColumn('my_table', 'another_field')]
#[AddColumn('other_table', 'last_one', ColumnType::DATE)]
#[AddIndex('my_table')]
#[AddIndex('my_table', IndexType::PRIMARY)]
#[DropColumn('other_table')]
#[DropColumn('other_table', 'old_column', description: 'field is not used anymore and replaced by \'last_one\'')]
#[DropIndex('other_table')]
#[ModifyColumn('other_table')]
#[ModifyColumn('other_table', 'this_field')]
#[ModifyColumn('other_table', 'this_field', ColumnType::BIGINT)]
class Version30000Date20240102030405 extends SimpleMigrationStep {
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
return null;
}
}
9 changes: 6 additions & 3 deletions lib/private/Migration/MetadataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
*
* @param string $appId
*
* @return array
* @return array<string, MigrationAttribute[]>
* @since 30.0.0
*/
public function extractMigrationAttributes(string $appId): array {
Expand All @@ -48,7 +48,10 @@ public function extractMigrationAttributes(string $appId): array {
$class = new ReflectionClass($ms->createInstance($version));
$attributes = $class->getAttributes();
foreach ($attributes as $attribute) {
$metadata[$version][] = $attribute->newInstance();
$item = $attribute->newInstance();
if ($item instanceof MigrationAttribute) {
$metadata[$version][] = $item;
}
}
}

Expand Down Expand Up @@ -144,7 +147,7 @@ private function createAttribute(array $item): MigrationAttribute {
}

try {
$attribute = new $class();
$attribute = new $class($item['table'] ?? '');
return $attribute->import($item);
} catch (\Error) {
throw new AttributeException('cannot import Attribute');
Expand Down
3 changes: 1 addition & 2 deletions lib/private/Updater/ReleaseMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public function downloadMetadata(string $url): array {
try {
$response = $client->get($url, [
'timeout' => 10,
'connect_timeout' => 10,
'verify' => false,
'connect_timeout' => 10
]);
} catch (Exception $e) {
throw new ReleaseMetadataException('could not reach metadata at ' . $url, previous: $e);
Expand Down
7 changes: 4 additions & 3 deletions lib/public/Migration/Attributes/AddColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Attribute;

/**
* attribute on new column creation
*
* @since 30.0.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
Expand All @@ -21,9 +23,8 @@ class AddColumn extends ColumnMigrationAttribute {
*/
public function definition(): string {
$type = is_null($this->getType()) ? '' : ' (' . $this->getType()->value . ')';
$table = empty($this->getTable()) ? '' : ' to table \'' . $this->getTable() . '\'';
return empty($this->getName()) ?
'Addition of a new column' . $type . $table
: 'Addition of column \'' . $this->getName() . '\'' . $type . $table;
'Addition of a new column' . $type . ' to table \'' . $this->getTable() . '\''
: 'Addition of column \'' . $this->getName() . '\'' . $type . ' to table \'' . $this->getTable() . '\'';
}
}
5 changes: 3 additions & 2 deletions lib/public/Migration/Attributes/AddIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Attribute;

/**
* attribute on index creation
*
* @since 30.0.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
Expand All @@ -21,7 +23,6 @@ class AddIndex extends IndexMigrationAttribute {
*/
public function definition(): string {
$type = is_null($this->getType()) ? '' : ' (' . $this->getType()->value . ')';
$table = empty($this->getTable()) ? '' : ' to table \'' . $this->getTable() . '\'';
return 'Addition of a new index' . $type . $table;
return 'Addition of a new index' . $type . ' to table \'' . $this->getTable() . '\'';
}
}
4 changes: 3 additions & 1 deletion lib/public/Migration/Attributes/ColumnMigrationAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use JsonSerializable;

/**
* generic class related to migration attribute about column changes
*
* @since 30.0.0
*/
class ColumnMigrationAttribute extends MigrationAttribute implements JsonSerializable {
public function __construct(
string $table = '',
string $table,
private string $name = '',
private ?ColumnType $type = null,
string $description = '',
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Migration/Attributes/ColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace OCP\Migration\Attributes;

/**
* enum FieldType based on OCP\DB\Types
* enum ColumnType based on OCP\DB\Types
*
* @see \OCP\DB\Types
* @since 30.0.0
Expand Down
2 changes: 2 additions & 0 deletions lib/public/Migration/Attributes/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Attribute;

/**
* attribute on table creation
*
* @since 30.0.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
Expand Down
7 changes: 4 additions & 3 deletions lib/public/Migration/Attributes/DropColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Attribute;

/**
* attribute on column drop
*
* @since 30.0.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
Expand All @@ -20,9 +22,8 @@ class DropColumn extends ColumnMigrationAttribute {
* @since 30.0.0
*/
public function definition(): string {
$table = empty($this->getTable()) ? '' : ' from table \'' . $this->getTable() . '\'';
return empty($this->getName()) ?
'Deletion of a column' . $table
: 'Deletion of column \'' . $this->getName() . '\'' . $table;
'Deletion of a column from table \'' . $this->getTable() . '\''
: 'Deletion of column \'' . $this->getName() . '\' from table \'' . $this->getTable() . '\'';
}
}
6 changes: 3 additions & 3 deletions lib/public/Migration/Attributes/DropIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Attribute;

/**
* attribute on index drop
*
* @since 30.0.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
Expand All @@ -20,8 +22,6 @@ class DropIndex extends IndexMigrationAttribute {
* @since 30.0.0
*/
public function definition(): string {
return empty($this->getTable()) ?
'Deletion of an index'
: 'Deletion of an index from table \'' . $this->getTable() . '\'';
return 'Deletion of an index from table \'' . $this->getTable() . '\'';
}
}
2 changes: 2 additions & 0 deletions lib/public/Migration/Attributes/DropTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Attribute;

/**
* attribute on table drop
*
* @since 30.0.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
class DropTable extends TableMigrationAttribute {
Expand Down
3 changes: 3 additions & 0 deletions lib/public/Migration/Attributes/GenericMigrationAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use JsonSerializable;

/**
* generic entry, used to replace migration attribute not yet known in current version
* but used in a future release
*
* @since 30.0.0
*/
class GenericMigrationAttribute extends MigrationAttribute implements JsonSerializable {
Expand Down
4 changes: 3 additions & 1 deletion lib/public/Migration/Attributes/IndexMigrationAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
use JsonSerializable;

/**
* generic class related to migration attribute about index changes
*
* @since 30.0.0
*/
class IndexMigrationAttribute extends MigrationAttribute implements JsonSerializable {
public function __construct(
string $table = '',
string $table,
private ?IndexType $type = null,
string $description = '',
array $notes = [],
Expand Down
2 changes: 2 additions & 0 deletions lib/public/Migration/Attributes/IndexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace OCP\Migration\Attributes;

/**
* type of index
*
* @since 30.0.0
*/
enum IndexType : string {
Expand Down
7 changes: 3 additions & 4 deletions lib/public/Migration/Attributes/MigrationAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
class MigrationAttribute implements JsonSerializable {
public function __construct(
private string $table = '',
private string $table,
private string $description = '',
private array $notes = [],
) {
Expand Down Expand Up @@ -93,8 +93,7 @@ public function definition(): string {
* @since 30.0.0
*/
public function import(array $data): self {
return $this->setTable($data['table'] ?? '')
->setDescription($data['description'] ?? '')
return $this->setDescription($data['description'] ?? '')
->setNotes($data['notes'] ?? []);
}

Expand All @@ -107,7 +106,7 @@ public function jsonSerialize(): array {
'class' => get_class($this),
'table' => $this->getTable(),
'description' => $this->getDescription(),
'notes' => $this->getNotes(),
'notes' => $this->getNotes()
];
}
}
7 changes: 4 additions & 3 deletions lib/public/Migration/Attributes/ModifyColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Attribute;

/**
* attribute on column modification
*
* @since 30.0.0
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_CLASS)]
Expand All @@ -21,9 +23,8 @@ class ModifyColumn extends ColumnMigrationAttribute {
*/
public function definition(): string {
$type = is_null($this->getType()) ? '' : ' to ' . $this->getType()->value;
$table = empty($this->getTable()) ? '' : ' from table \'' . $this->getTable() . '\'';
return empty($this->getName()) ?
'Modification of a column' . $table . $type
: 'Modification of column \'' . $this->getName() . '\'' . $table . $type;
'Modification of a column from table \'' . $this->getTable() . '\'' . $type
: 'Modification of column \'' . $this->getName() . '\' from table \'' . $this->getTable() . '\'' . $type;
}
}
2 changes: 2 additions & 0 deletions lib/public/Migration/Attributes/TableMigrationAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use JsonSerializable;

/**
* generic class related to migration attribute about table changes
*
* @since 30.0.0
*/
class TableMigrationAttribute extends MigrationAttribute implements JsonSerializable {
Expand Down
Loading

0 comments on commit ad490c9

Please sign in to comment.