diff --git a/database/migrations/2023_02_18_202830_create_versions_table.php b/database/migrations/2023_02_18_202830_create_versions_table.php index ec1cccdff..6706d0feb 100644 --- a/database/migrations/2023_02_18_202830_create_versions_table.php +++ b/database/migrations/2023_02_18_202830_create_versions_table.php @@ -29,7 +29,7 @@ public function up() $table->json('data'); $table->json('aux'); $table->string('editor'); - $table->timestamp('created_at', 6); + $table->timestamp('created_at', 3); $table->index(['versionable_id', 'versionable_type', 'created_at', 'tenant_id'], 'idx_versions_id_type_created_tenantid'); $table->index(['publish_at', 'published']); diff --git a/src/GraphQL/Mutations/AddElement.php b/src/GraphQL/Mutations/AddElement.php index 0f2cef3ec..c0dfbf6ce 100644 --- a/src/GraphQL/Mutations/AddElement.php +++ b/src/GraphQL/Mutations/AddElement.php @@ -54,7 +54,7 @@ public function __invoke( $rootValue, array $args ) : Element $version->files()->attach( $args['files'] ?? [] ); - return $element->refresh(); + return $element; }, 3 ); } } diff --git a/src/GraphQL/Mutations/AddFile.php b/src/GraphQL/Mutations/AddFile.php index 3803e684d..ee425b7b5 100644 --- a/src/GraphQL/Mutations/AddFile.php +++ b/src/GraphQL/Mutations/AddFile.php @@ -62,7 +62,7 @@ public function __invoke( $rootValue, array $args ) : File ], ] ); - return $file->refresh(); + return $file; }, 3 ); } diff --git a/src/GraphQL/Mutations/AddPage.php b/src/GraphQL/Mutations/AddPage.php index cf13ce969..53df65662 100644 --- a/src/GraphQL/Mutations/AddPage.php +++ b/src/GraphQL/Mutations/AddPage.php @@ -68,7 +68,7 @@ public function __invoke( $rootValue, array $args ) : Page $version->elements()->attach( $args['elements'] ?? [] ); $version->files()->attach( $args['files'] ?? [] ); - return $page->refresh(); + return $page; }, 3 ); } ); } diff --git a/src/Models/Content.php b/src/Models/Content.php index 228a96697..5fc237168 100644 --- a/src/Models/Content.php +++ b/src/Models/Content.php @@ -9,9 +9,11 @@ use Aimeos\Cms\Concerns\Tenancy; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Laravel\Scout\Attributes\SearchUsingFullText; use Laravel\Scout\Searchable; +use Illuminate\Support\Str; /** @@ -77,6 +79,17 @@ public function getConnectionName() : string } + /** + * Generate a new unique key for the model. + * + * @return string + */ + public function newUniqueId() + { + return strtoupper( (string) Str::uuid7() ); + } + + /** * Get the name of the index associated with the model. */ @@ -97,4 +110,18 @@ public function toSearchableArray(): array 'content' => $this->content ]; } + + + /** + * Interact with the "id" property. + * + * @return Attribute Eloquent attribute for the "id" property + */ + protected function id(): Attribute + { + return Attribute::make( + get: fn( $value ) => strtoupper( $value ), + set: fn( $value ) => strtoupper( $value ), + ); + } } \ No newline at end of file diff --git a/src/Models/Element.php b/src/Models/Element.php index ea441b6dc..e236ecb84 100644 --- a/src/Models/Element.php +++ b/src/Models/Element.php @@ -21,6 +21,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Date; use Illuminate\Support\Collection; +use Illuminate\Support\Str; /** @@ -177,6 +178,17 @@ public function latest() : MorphOne } + /** + * Generate a new unique key for the model. + * + * @return string + */ + public function newUniqueId() + { + return strtoupper( (string) Str::uuid7() ); + } + + /** * Publish the given version of the element. * @@ -273,6 +285,20 @@ protected function data(): Attribute } + /** + * Interact with the "id" property. + * + * @return Attribute Eloquent attribute for the "id" property + */ + protected function id(): Attribute + { + return Attribute::make( + get: fn( $value ) => strtoupper( $value ), + set: fn( $value ) => strtoupper( $value ), + ); + } + + /** * Prepare the model for pruning. */ diff --git a/src/Models/File.php b/src/Models/File.php index dcc3b01f5..59099fff6 100644 --- a/src/Models/File.php +++ b/src/Models/File.php @@ -21,6 +21,7 @@ use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Date; +use Illuminate\Support\Str; use Illuminate\Http\UploadedFile; use Intervention\Image\ImageManager; @@ -267,6 +268,17 @@ public function latest() : MorphOne } + /** + * Generate a new unique key for the model. + * + * @return string + */ + public function newUniqueId() + { + return strtoupper( (string) Str::uuid7() ); + } + + /** * Get the prunable model query. * @@ -491,6 +503,20 @@ protected function filename( string $filename, ?string $ext = null, array $size } + /** + * Interact with the "id" property. + * + * @return Attribute Eloquent attribute for the "id" property + */ + protected function id(): Attribute + { + return Attribute::make( + get: fn( $value ) => strtoupper( $value ), + set: fn( $value ) => strtoupper( $value ), + ); + } + + /** * Prepare the model for pruning. */ diff --git a/src/Models/Page.php b/src/Models/Page.php index ce43d88a9..432dd2a1c 100644 --- a/src/Models/Page.php +++ b/src/Models/Page.php @@ -28,6 +28,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; +use Illuminate\Support\Str; /** @@ -336,6 +337,17 @@ public function nav( $level = 0 ) : \Aimeos\Nestedset\Collection } + /** + * Generate a new unique key for the model. + * + * @return string + */ + public function newUniqueId() + { + return strtoupper( (string) Str::uuid7() ); + } + + /** * Relation to the parent. * @@ -513,6 +525,20 @@ protected function domain(): Attribute } + /** + * Interact with the "id" property. + * + * @return Attribute Eloquent attribute for the "id" property + */ + protected function id(): Attribute + { + return Attribute::make( + get: fn( $value ) => strtoupper( $value ), + set: fn( $value ) => strtoupper( $value ), + ); + } + + /** * Interact with the "name" property. * diff --git a/src/Models/Version.php b/src/Models/Version.php index 039f84760..b00598ce1 100644 --- a/src/Models/Version.php +++ b/src/Models/Version.php @@ -8,6 +8,7 @@ namespace Aimeos\Cms\Models; use Aimeos\Cms\Concerns\Tenancy; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\MorphTo; @@ -15,6 +16,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Date; use Illuminate\Support\Collection; +use Illuminate\Support\Str; /** @@ -49,9 +51,13 @@ class Version extends Model protected $casts = [ 'data' => 'object', 'aux' => 'object', - 'created_at' => 'datetime:Y-m-d H:i:s', ]; + /** + * The date format with milliseconds used by created_at. + */ + protected $dateFormat = 'Y-m-d H:i:s.v'; + /** * The attributes that are mass assignable. * @@ -145,6 +151,17 @@ public function getUpdatedAtColumn() } + /** + * Generate a new unique key for the model. + * + * @return string + */ + public function newUniqueId() + { + return strtoupper( (string) Str::uuid7() ); + } + + /** * Get the parent versionable model (page, file or element). */ @@ -183,4 +200,18 @@ public function getDirty() return $dirty; } + + + /** + * Interact with the "id" property. + * + * @return Attribute Eloquent attribute for the "id" property + */ + protected function id(): Attribute + { + return Attribute::make( + get: fn( $value ) => strtoupper( $value ), + set: fn( $value ) => strtoupper( $value ), + ); + } } diff --git a/tests/GraphqlElementTest.php b/tests/GraphqlElementTest.php index b5ef207c2..c62610c1c 100644 --- a/tests/GraphqlElementTest.php +++ b/tests/GraphqlElementTest.php @@ -223,6 +223,7 @@ public function testElementVersions() $this->expectsDatabaseQueryCount(3); +\Illuminate\Support\Facades\DB::enableQueryLog(); $response = $this->actingAs($this->user)->graphQL('{ element(id: "' . $element->id . '") { id @@ -237,6 +238,8 @@ public function testElementVersions() } } }'); +print_r(\Illuminate\Support\Facades\DB::getQueryLog()); +print_r(\Illuminate\Support\Facades\DB::table('cms_versions')->get()->all()); $elementData = $response->json('data.element'); @@ -259,9 +262,8 @@ public function testAddElement() $this->seed(CmsSeeder::class); $file = File::where( 'mime', 'image/jpeg' )->firstOrFail(); - $element = Element::where( 'type', 'footer' )->firstOrFail(); - $this->expectsDatabaseQueryCount( 8 ); + $this->expectsDatabaseQueryCount( 7 ); $response = $this->actingAs($this->user)->graphQL(' mutation { diff --git a/tests/GraphqlFileTest.php b/tests/GraphqlFileTest.php index 5db51359e..15c12aa9c 100644 --- a/tests/GraphqlFileTest.php +++ b/tests/GraphqlFileTest.php @@ -240,7 +240,7 @@ public function testAddFile() { $this->seed( CmsSeeder::class ); - $this->expectsDatabaseQueryCount( 4 ); + $this->expectsDatabaseQueryCount( 3 ); $response = $this->actingAs( $this->user )->multipartGraphQL( [ 'query' => ' mutation($file: Upload!, $preview: Upload) { diff --git a/tests/GraphqlPageTest.php b/tests/GraphqlPageTest.php index 377b40ce7..34500c628 100644 --- a/tests/GraphqlPageTest.php +++ b/tests/GraphqlPageTest.php @@ -570,7 +570,7 @@ public function testAddPage() $file = File::where( 'mime', 'image/jpeg' )->firstOrFail(); $element = Element::where( 'type', 'footer' )->firstOrFail(); - $this->expectsDatabaseQueryCount( 10 ); + $this->expectsDatabaseQueryCount( 9 ); $response = $this->actingAs( $this->user )->graphQL( ' mutation { addPage(input: { @@ -641,7 +641,7 @@ public function testAddPageChild() $root = Page::where('tag', 'root')->firstOrFail(); - $this->expectsDatabaseQueryCount( 10 ); + $this->expectsDatabaseQueryCount( 8 ); $response = $this->actingAs( $this->user )->graphQL( ' mutation { addPage(input: { @@ -681,7 +681,7 @@ public function testAddPageChildRef() $root = Page::where('tag', 'root')->firstOrFail(); $ref = Page::where('tag', 'blog')->firstOrFail(); - $this->expectsDatabaseQueryCount( 10 ); + $this->expectsDatabaseQueryCount( 8 ); $response = $this->actingAs( $this->user )->graphQL( ' mutation { addPage(input: {