-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
WorkspaceProjection.php
390 lines (348 loc) · 16.1 KB
/
WorkspaceProjection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
declare(strict_types=1);
namespace Neos\ContentRepository\Core\Projection\Workspace;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Neos\ContentRepository\Core\EventStore\EventInterface;
use Neos\ContentRepository\Core\Feature\WorkspaceCreation\Event\RootWorkspaceWasCreated;
use Neos\ContentRepository\Core\Feature\WorkspaceCreation\Event\WorkspaceWasCreated;
use Neos\ContentRepository\Core\Feature\WorkspaceModification\Event\WorkspaceBaseWorkspaceWasChanged;
use Neos\ContentRepository\Core\Feature\WorkspaceModification\Event\WorkspaceOwnerWasChanged;
use Neos\ContentRepository\Core\Feature\WorkspaceModification\Event\WorkspaceWasRemoved;
use Neos\ContentRepository\Core\Feature\WorkspaceModification\Event\WorkspaceWasRenamed;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasDiscarded;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasPartiallyDiscarded;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasPartiallyPublished;
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasPublished;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Event\WorkspaceRebaseFailed;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Event\WorkspaceWasRebased;
use Neos\ContentRepository\Core\Infrastructure\DbalCheckpointStorage;
use Neos\ContentRepository\Core\Infrastructure\DbalSchemaDiff;
use Neos\ContentRepository\Core\Infrastructure\DbalSchemaFactory;
use Neos\ContentRepository\Core\Projection\CheckpointStorageStatusType;
use Neos\ContentRepository\Core\Projection\ProjectionInterface;
use Neos\ContentRepository\Core\Projection\ProjectionStatus;
use Neos\ContentRepository\Core\Projection\WithMarkStaleInterface;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;
use Neos\EventStore\Model\Event\SequenceNumber;
use Neos\EventStore\Model\EventEnvelope;
/**
* @internal
* @implements ProjectionInterface<WorkspaceFinder>
*/
class WorkspaceProjection implements ProjectionInterface, WithMarkStaleInterface
{
private const DEFAULT_TEXT_COLLATION = 'utf8mb4_unicode_520_ci';
/**
* @var WorkspaceFinder|null Cache for the workspace finder returned by {@see getState()},
* so that always the same instance is returned
*/
private ?WorkspaceFinder $workspaceFinder = null;
private DbalCheckpointStorage $checkpointStorage;
private WorkspaceRuntimeCache $workspaceRuntimeCache;
public function __construct(
private readonly Connection $dbal,
private readonly string $tableName,
) {
$this->checkpointStorage = new DbalCheckpointStorage(
$this->dbal,
$this->tableName . '_checkpoint',
self::class
);
$this->workspaceRuntimeCache = new WorkspaceRuntimeCache();
}
public function setUp(): void
{
foreach ($this->determineRequiredSqlStatements() as $statement) {
$this->dbal->executeStatement($statement);
}
$this->checkpointStorage->setUp();
}
public function status(): ProjectionStatus
{
$checkpointStorageStatus = $this->checkpointStorage->status();
if ($checkpointStorageStatus->type === CheckpointStorageStatusType::ERROR) {
return ProjectionStatus::error($checkpointStorageStatus->details);
}
if ($checkpointStorageStatus->type === CheckpointStorageStatusType::SETUP_REQUIRED) {
return ProjectionStatus::setupRequired($checkpointStorageStatus->details);
}
try {
$this->dbal->connect();
} catch (\Throwable $e) {
return ProjectionStatus::error(sprintf('Failed to connect to database: %s', $e->getMessage()));
}
try {
$requiredSqlStatements = $this->determineRequiredSqlStatements();
} catch (\Throwable $e) {
return ProjectionStatus::error(sprintf('Failed to determine required SQL statements: %s', $e->getMessage()));
}
if ($requiredSqlStatements !== []) {
return ProjectionStatus::setupRequired(sprintf('The following SQL statement%s required: %s', count($requiredSqlStatements) !== 1 ? 's are' : ' is', implode(chr(10), $requiredSqlStatements)));
}
return ProjectionStatus::ok();
}
/**
* @return array<string>
*/
private function determineRequiredSqlStatements(): array
{
$schemaManager = $this->dbal->getSchemaManager();
if (!$schemaManager instanceof AbstractSchemaManager) {
throw new \RuntimeException('Failed to retrieve Schema Manager', 1625653914);
}
$workspaceTable = new Table($this->tableName, [
(new Column('workspacename', Type::getType(Types::STRING)))->setLength(255)->setNotnull(true)->setCustomSchemaOption('collation', self::DEFAULT_TEXT_COLLATION),
(new Column('baseworkspacename', Type::getType(Types::STRING)))->setLength(255)->setNotnull(false)->setCustomSchemaOption('collation', self::DEFAULT_TEXT_COLLATION),
(new Column('workspacetitle', Type::getType(Types::STRING)))->setLength(255)->setNotnull(true)->setCustomSchemaOption('collation', self::DEFAULT_TEXT_COLLATION),
(new Column('workspacedescription', Type::getType(Types::STRING)))->setLength(255)->setNotnull(true)->setCustomSchemaOption('collation', self::DEFAULT_TEXT_COLLATION),
(new Column('workspaceowner', Type::getType(Types::STRING)))->setLength(255)->setNotnull(false)->setCustomSchemaOption('collation', self::DEFAULT_TEXT_COLLATION),
DbalSchemaFactory::columnForContentStreamId('currentcontentstreamid')->setNotNull(true),
(new Column('status', Type::getType(Types::BINARY)))->setLength(20)->setNotnull(false)
]);
$workspaceTable->setPrimaryKey(['workspacename']);
$schema = DbalSchemaFactory::createSchemaWithTables($schemaManager, [$workspaceTable]);
return DbalSchemaDiff::determineRequiredSqlStatements($this->dbal, $schema);
}
public function reset(): void
{
$this->dbal->exec('TRUNCATE ' . $this->tableName);
$this->checkpointStorage->acquireLock();
$this->checkpointStorage->updateAndReleaseLock(SequenceNumber::none());
}
public function canHandle(EventInterface $event): bool
{
return in_array($event::class, [
WorkspaceWasCreated::class,
WorkspaceWasRenamed::class,
RootWorkspaceWasCreated::class,
WorkspaceWasDiscarded::class,
WorkspaceWasPartiallyDiscarded::class,
WorkspaceWasPartiallyPublished::class,
WorkspaceWasPublished::class,
WorkspaceWasRebased::class,
WorkspaceRebaseFailed::class,
WorkspaceWasRemoved::class,
WorkspaceOwnerWasChanged::class,
WorkspaceBaseWorkspaceWasChanged::class,
]);
}
public function apply(EventInterface $event, EventEnvelope $eventEnvelope): void
{
match ($event::class) {
WorkspaceWasCreated::class => $this->whenWorkspaceWasCreated($event),
WorkspaceWasRenamed::class => $this->whenWorkspaceWasRenamed($event),
RootWorkspaceWasCreated::class => $this->whenRootWorkspaceWasCreated($event),
WorkspaceWasDiscarded::class => $this->whenWorkspaceWasDiscarded($event),
WorkspaceWasPartiallyDiscarded::class => $this->whenWorkspaceWasPartiallyDiscarded($event),
WorkspaceWasPartiallyPublished::class => $this->whenWorkspaceWasPartiallyPublished($event),
WorkspaceWasPublished::class => $this->whenWorkspaceWasPublished($event),
WorkspaceWasRebased::class => $this->whenWorkspaceWasRebased($event),
WorkspaceRebaseFailed::class => $this->whenWorkspaceRebaseFailed($event),
WorkspaceWasRemoved::class => $this->whenWorkspaceWasRemoved($event),
WorkspaceOwnerWasChanged::class => $this->whenWorkspaceOwnerWasChanged($event),
WorkspaceBaseWorkspaceWasChanged::class => $this->whenWorkspaceBaseWorkspaceWasChanged($event),
default => throw new \InvalidArgumentException(sprintf('Unsupported event %s', get_debug_type($event))),
};
}
public function getCheckpointStorage(): DbalCheckpointStorage
{
return $this->checkpointStorage;
}
public function getState(): WorkspaceFinder
{
if (!$this->workspaceFinder) {
$this->workspaceFinder = new WorkspaceFinder(
$this->dbal,
$this->workspaceRuntimeCache,
$this->tableName
);
}
return $this->workspaceFinder;
}
public function markStale(): void
{
$this->workspaceRuntimeCache->disableCache();
}
private function whenWorkspaceWasCreated(WorkspaceWasCreated $event): void
{
$this->dbal->insert($this->tableName, [
'workspaceName' => $event->workspaceName->value,
'baseWorkspaceName' => $event->baseWorkspaceName->value,
'workspaceTitle' => $event->workspaceTitle->value,
'workspaceDescription' => $event->workspaceDescription->value,
'workspaceOwner' => $event->workspaceOwner?->value,
'currentContentStreamId' => $event->newContentStreamId->value,
'status' => WorkspaceStatus::UP_TO_DATE->value
]);
}
private function whenWorkspaceWasRenamed(WorkspaceWasRenamed $event): void
{
$this->dbal->update(
$this->tableName,
[
'workspaceTitle' => $event->workspaceTitle->value,
'workspaceDescription' => $event->workspaceDescription->value,
],
['workspaceName' => $event->workspaceName->value]
);
}
private function whenRootWorkspaceWasCreated(RootWorkspaceWasCreated $event): void
{
$this->dbal->insert($this->tableName, [
'workspaceName' => $event->workspaceName->value,
'workspaceTitle' => $event->workspaceTitle->value,
'workspaceDescription' => $event->workspaceDescription->value,
'currentContentStreamId' => $event->newContentStreamId->value,
'status' => WorkspaceStatus::UP_TO_DATE->value
]);
}
private function whenWorkspaceWasDiscarded(WorkspaceWasDiscarded $event): void
{
$this->updateContentStreamId($event->newContentStreamId, $event->workspaceName);
$this->markWorkspaceAsOutdated($event->workspaceName);
$this->markDependentWorkspacesAsOutdated($event->workspaceName);
}
private function whenWorkspaceWasPartiallyDiscarded(WorkspaceWasPartiallyDiscarded $event): void
{
$this->updateContentStreamId($event->newContentStreamId, $event->workspaceName);
$this->markDependentWorkspacesAsOutdated($event->workspaceName);
}
private function whenWorkspaceWasPartiallyPublished(WorkspaceWasPartiallyPublished $event): void
{
// TODO: How do we test this method?
// It's hard to design a BDD testcase failing if this method is commented out...
$this->updateContentStreamId(
$event->newSourceContentStreamId,
$event->sourceWorkspaceName
);
$this->markDependentWorkspacesAsOutdated($event->targetWorkspaceName);
// NASTY: we need to set the source workspace name as non-outdated; as it has been made up-to-date again.
$this->markWorkspaceAsUpToDate($event->sourceWorkspaceName);
$this->markDependentWorkspacesAsOutdated($event->sourceWorkspaceName);
}
private function whenWorkspaceWasPublished(WorkspaceWasPublished $event): void
{
// TODO: How do we test this method?
// It's hard to design a BDD testcase failing if this method is commented out...
$this->updateContentStreamId(
$event->newSourceContentStreamId,
$event->sourceWorkspaceName
);
$this->markDependentWorkspacesAsOutdated($event->targetWorkspaceName);
// NASTY: we need to set the source workspace name as non-outdated; as it has been made up-to-date again.
$this->markWorkspaceAsUpToDate($event->sourceWorkspaceName);
$this->markDependentWorkspacesAsOutdated($event->sourceWorkspaceName);
}
private function whenWorkspaceWasRebased(WorkspaceWasRebased $event): void
{
$this->updateContentStreamId($event->newContentStreamId, $event->workspaceName);
$this->markDependentWorkspacesAsOutdated($event->workspaceName);
// When the rebase is successful, we can set the status of the workspace back to UP_TO_DATE.
$this->markWorkspaceAsUpToDate($event->workspaceName);
}
private function whenWorkspaceRebaseFailed(WorkspaceRebaseFailed $event): void
{
$this->markWorkspaceAsOutdatedConflict($event->workspaceName);
}
private function whenWorkspaceWasRemoved(WorkspaceWasRemoved $event): void
{
$this->dbal->delete(
$this->tableName,
['workspaceName' => $event->workspaceName->value]
);
}
private function whenWorkspaceOwnerWasChanged(WorkspaceOwnerWasChanged $event): void
{
$this->dbal->update(
$this->tableName,
['workspaceOwner' => $event->newWorkspaceOwner],
['workspaceName' => $event->workspaceName->value]
);
}
private function whenWorkspaceBaseWorkspaceWasChanged(WorkspaceBaseWorkspaceWasChanged $event): void
{
$this->dbal->update(
$this->tableName,
[
'baseWorkspaceName' => $event->baseWorkspaceName->value,
'currentContentStreamId' => $event->newContentStreamId->value,
],
['workspaceName' => $event->workspaceName->value]
);
}
private function updateContentStreamId(
ContentStreamId $contentStreamId,
WorkspaceName $workspaceName,
): void {
$this->dbal->update($this->tableName, [
'currentContentStreamId' => $contentStreamId->value,
], [
'workspaceName' => $workspaceName->value
]);
}
private function markWorkspaceAsUpToDate(WorkspaceName $workspaceName): void
{
$this->dbal->executeUpdate('
UPDATE ' . $this->tableName . '
SET status = :upToDate
WHERE
workspacename = :workspaceName
', [
'upToDate' => WorkspaceStatus::UP_TO_DATE->value,
'workspaceName' => $workspaceName->value
]);
}
private function markDependentWorkspacesAsOutdated(WorkspaceName $baseWorkspaceName): void
{
$this->dbal->executeUpdate('
UPDATE ' . $this->tableName . '
SET status = :outdated
WHERE
baseworkspacename = :baseWorkspaceName
', [
'outdated' => WorkspaceStatus::OUTDATED->value,
'baseWorkspaceName' => $baseWorkspaceName->value
]);
}
private function markWorkspaceAsOutdated(WorkspaceName $workspaceName): void
{
$this->dbal->executeUpdate('
UPDATE ' . $this->tableName . '
SET
status = :outdated
WHERE
workspacename = :workspaceName
', [
'outdated' => WorkspaceStatus::OUTDATED->value,
'workspaceName' => $workspaceName->value
]);
}
private function markWorkspaceAsOutdatedConflict(WorkspaceName $workspaceName): void
{
$this->dbal->executeUpdate('
UPDATE ' . $this->tableName . '
SET
status = :outdatedConflict
WHERE
workspacename = :workspaceName
', [
'outdatedConflict' => WorkspaceStatus::OUTDATED_CONFLICT->value,
'workspaceName' => $workspaceName->value
]);
}
}