From b2352a36c417f584a80d6c6eacbe87df5749ca63 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Sat, 19 Oct 2024 20:56:34 +0200 Subject: [PATCH 1/2] Add uuids and migration --- lib/Db/ObjectEntity.php | 3 + lib/Db/Register.php | 6 ++ lib/Db/RegisterMapper.php | 7 ++ lib/Db/Schema.php | 3 + lib/Db/SchemaMapper.php | 7 ++ lib/Db/Source.php | 6 ++ lib/Db/SourceMapper.php | 6 ++ lib/Migration/Version1Date20241019205009.php | 72 ++++++++++++++++++++ 8 files changed, 110 insertions(+) create mode 100755 lib/Migration/Version1Date20241019205009.php diff --git a/lib/Db/ObjectEntity.php b/lib/Db/ObjectEntity.php index 66099ae..ab941cc 100644 --- a/lib/Db/ObjectEntity.php +++ b/lib/Db/ObjectEntity.php @@ -11,6 +11,7 @@ class ObjectEntity extends Entity implements JsonSerializable protected ?string $uuid = null; protected ?string $register = null; protected ?string $schema = null; + protected ?string $version = null; protected ?array $object = []; protected ?DateTime $updated = null; protected ?DateTime $created = null; @@ -19,6 +20,7 @@ public function __construct() { $this->addType(fieldName:'uuid', type: 'string'); $this->addType(fieldName:'register', type: 'string'); $this->addType(fieldName:'schema', type: 'string'); + $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName:'object', type: 'json'); $this->addType(fieldName:'updated', type: 'datetime'); $this->addType(fieldName:'created', type: 'datetime'); @@ -63,6 +65,7 @@ public function jsonSerialize(): array // return [ // 'id' => $this->id, // 'uuid' => $this->uuid, +// 'version' => $this->version, // 'register' => $this->register, // 'schema' => $this->schema, // 'object' => $this->object, diff --git a/lib/Db/Register.php b/lib/Db/Register.php index 2bbd0cb..73607fb 100644 --- a/lib/Db/Register.php +++ b/lib/Db/Register.php @@ -8,7 +8,9 @@ class Register extends Entity implements JsonSerializable { + protected ?string $uuid = null; protected ?string $title = null; + protected ?string $version = null; protected ?string $description = null; protected ?array $schemas = []; protected ?string $source = null; @@ -17,7 +19,9 @@ class Register extends Entity implements JsonSerializable protected ?DateTime $created = null; public function __construct() { + $this->addType(fieldName: 'uuid', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); + $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'description', type: 'string'); $this->addType(fieldName: 'schemas', type: 'json'); $this->addType(fieldName: 'source', type: 'string'); @@ -64,7 +68,9 @@ public function jsonSerialize(): array { return [ 'id' => $this->id, + 'uuid' => $this->uuid, 'title' => $this->title, + 'version' => $this->version, 'description' => $this->description, 'schemas' => $this->schemas, 'source' => $this->source, diff --git a/lib/Db/RegisterMapper.php b/lib/Db/RegisterMapper.php index 3ec4b57..a04f918 100644 --- a/lib/Db/RegisterMapper.php +++ b/lib/Db/RegisterMapper.php @@ -7,6 +7,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use Symfony\Component\Uid\Uuid; class RegisterMapper extends QBMapper { @@ -61,6 +62,12 @@ public function createFromArray(array $object): Register { $register = new Register(); $register->hydrate(object: $object); + + // Set uuid if not provided + if ($register->getUuid() === null) { + $register->setUuid(Uuid::v4()); + } + return $this->insert(entity: $register); } diff --git a/lib/Db/Schema.php b/lib/Db/Schema.php index 8ac7262..04423ff 100644 --- a/lib/Db/Schema.php +++ b/lib/Db/Schema.php @@ -8,6 +8,7 @@ class Schema extends Entity implements JsonSerializable { + protected ?string $uuid = null; protected ?string $title = null; protected ?string $version = null; protected ?string $description = null; @@ -20,6 +21,7 @@ class Schema extends Entity implements JsonSerializable protected ?DateTime $created = null; public function __construct() { + $this->addType(fieldName: 'uuid', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'description', type: 'string'); @@ -93,6 +95,7 @@ public function jsonSerialize(): array $array = [ 'id' => $this->id, + 'uuid' => $this->uuid, 'title' => $this->title, 'version' => $this->version, 'description' => $this->description, diff --git a/lib/Db/SchemaMapper.php b/lib/Db/SchemaMapper.php index cc43d97..26a517c 100644 --- a/lib/Db/SchemaMapper.php +++ b/lib/Db/SchemaMapper.php @@ -7,6 +7,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use Symfony\Component\Uid\Uuid; class SchemaMapper extends QBMapper { @@ -70,6 +71,12 @@ public function createFromArray(array $object): Schema { $schema = new Schema(); $schema->hydrate(object: $object); + + // Set uuid if not provided + if ($schema->getUuid() === null) { + $schema->setUuid(Uuid::v4()); + } + return $this->insert(entity: $schema); } diff --git a/lib/Db/Source.php b/lib/Db/Source.php index 09e92ef..caccaec 100644 --- a/lib/Db/Source.php +++ b/lib/Db/Source.php @@ -8,7 +8,9 @@ class Source extends Entity implements JsonSerializable { + protected ?string $uuid = null; protected ?string $title = null; + protected ?string $version = null; protected ?string $description = null; protected ?string $databaseUrl = null; protected ?string $type = null; @@ -16,7 +18,9 @@ class Source extends Entity implements JsonSerializable protected ?DateTime $created = null; public function __construct() { + $this->addType(fieldName: 'uuid', type: 'string'); $this->addType(fieldName: 'title', type: 'string'); + $this->addType(fieldName: 'version', type: 'string'); $this->addType(fieldName: 'description', type: 'string'); $this->addType(fieldName: 'databaseUrl', type: 'string'); $this->addType(fieldName: 'type', type: 'string'); @@ -60,7 +64,9 @@ public function jsonSerialize(): array { return [ 'id' => $this->id, + 'uuid' => $this->uuid, 'title' => $this->title, + 'version' => $this->version, 'description' => $this->description, 'databaseUrl' => $this->databaseUrl, 'type' => $this->type, diff --git a/lib/Db/SourceMapper.php b/lib/Db/SourceMapper.php index 67ded28..94a3eb0 100644 --- a/lib/Db/SourceMapper.php +++ b/lib/Db/SourceMapper.php @@ -7,6 +7,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use Symfony\Component\Uid\Uuid; class SourceMapper extends QBMapper { @@ -61,6 +62,11 @@ public function createFromArray(array $object): Source { $source = new Source(); $source->hydrate(object: $object); + + // Set uuid if not provided + if ($source->getUuid() === null) { + $source->setUuid(Uuid::v4()); + } return $this->insert(entity: $source); } diff --git a/lib/Migration/Version1Date20241019205009.php b/lib/Migration/Version1Date20241019205009.php new file mode 100755 index 0000000..c055e05 --- /dev/null +++ b/lib/Migration/Version1Date20241019205009.php @@ -0,0 +1,72 @@ +getTable('openregister_sources'); + $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); + $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); + $table->addIndex(['uuid'], 'openregister_sources_uuid_index'); + + // Update the abilities table to add the base column + $table = $schema->getTable('openregister_schemas'); + $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); + $table->addIndex(['uuid'], 'openregister_sources_uuid_index'); + + // Update the abilities table to add the base column + $table = $schema->getTable('openregister_registers'); + $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); + $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); + $table->addIndex(['uuid'], 'openregister_sources_uuid_index'); + + // Update the abilities table to add the base column + $table = $schema->getTable('openregister_objects'); + $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); + + return $schema; + } + + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + } +} From 59a819c2d25adcc9bc8523c9ddd9f3947858bc07 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Sat, 19 Oct 2024 22:47:40 +0200 Subject: [PATCH 2/2] Check if colums exsist... --- lib/Migration/Version1Date20241019205009.php | 41 ++++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lib/Migration/Version1Date20241019205009.php b/lib/Migration/Version1Date20241019205009.php index c055e05..857091a 100755 --- a/lib/Migration/Version1Date20241019205009.php +++ b/lib/Migration/Version1Date20241019205009.php @@ -15,9 +15,6 @@ use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; -/** - * FIXME Auto-generated migration step: Please modify to your needs! - */ class Version1Date20241019205009 extends SimpleMigrationStep { /** @@ -38,26 +35,38 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); - // Update the abilities table to add the base column + // Update the openregister_sources table $table = $schema->getTable('openregister_sources'); - $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); - $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); - $table->addIndex(['uuid'], 'openregister_sources_uuid_index'); + if (!$table->hasColumn('uuid')) { + $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); + $table->addIndex(['uuid'], 'openregister_sources_uuid_index'); + } + if (!$table->hasColumn('version')) { + $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); + } - // Update the abilities table to add the base column + // Update the openregister_schemas table $table = $schema->getTable('openregister_schemas'); - $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); - $table->addIndex(['uuid'], 'openregister_sources_uuid_index'); + if (!$table->hasColumn('uuid')) { + $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); + $table->addIndex(['uuid'], 'openregister_schemas_uuid_index'); + } - // Update the abilities table to add the base column + // Update the openregister_registers table $table = $schema->getTable('openregister_registers'); - $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); - $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); - $table->addIndex(['uuid'], 'openregister_sources_uuid_index'); + if (!$table->hasColumn('uuid')) { + $table->addColumn(name: 'uuid', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255]); + $table->addIndex(['uuid'], 'openregister_registers_uuid_index'); + } + if (!$table->hasColumn('version')) { + $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); + } - // Update the abilities table to add the base column + // Update the openregister_objects table $table = $schema->getTable('openregister_objects'); - $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); + if (!$table->hasColumn('version')) { + $table->addColumn(name: 'version', typeName: Types::STRING, options: ['notnull' => true, 'length' => 255, 'default' => '0.0.1']); + } return $schema; }