Skip to content

Commit 4bbec53

Browse files
committed
Introduce SagaStoreRelation schema
As a Saga can be linked to multiple SagaIds, a relation table is required (similar as with EventStore).
1 parent e1948b1 commit 4bbec53

File tree

8 files changed

+99
-14
lines changed

8 files changed

+99
-14
lines changed

resources/migrations/doctrine/Version20251002195234.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,23 @@ public function up(Schema $schema): void
1212
$this->addSql(
1313
<<<'SQL'
1414
CREATE TABLE `saga_store` (
15-
`saga_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
15+
`id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
1616
`saga_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
1717
`payload` json NOT NULL,
1818
`created_at` timestamp(6) NOT NULL,
1919
`updated_at` timestamp(6) NULL DEFAULT NULL,
20-
PRIMARY KEY (`saga_id`, `saga_name`)
20+
PRIMARY KEY (`id`)
21+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
22+
SQL
23+
);
24+
25+
$this->addSql(
26+
<<<'SQL'
27+
CREATE TABLE `saga_store_relation` (
28+
`id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
29+
`saga_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
30+
PRIMARY KEY (`id`,`saga_id`),
31+
CONSTRAINT `saga_store_relation_ibfk_1` FOREIGN KEY (`id`) REFERENCES `saga_store` (`id`)
2132
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2233
SQL
2334
);

resources/migrations/phinx/20251002194212.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ final class V20251002194212 extends AbstractMigration
88
{
99
public function change(): void
1010
{
11-
$this->table('saga_store', ['id' => false, 'primary_key' => ['saga_id', 'saga_name']])
12-
->addColumn('saga_id', 'string', ['limit' => 50, 'null' => false])
11+
$this->table('saga_store', ['id' => false, 'primary_key' => 'id'])
12+
->addColumn('id', 'string', ['limit' => 50, 'null' => false])
1313
->addColumn('saga_name', 'string', ['null' => false])
1414
->addColumn('payload', 'json', ['null' => false])
1515
->addColumn('created_at', 'timestamp', ['limit' => 6, 'null' => false])
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Phinx\Migration\AbstractMigration;
6+
7+
final class V20251015202332 extends AbstractMigration
8+
{
9+
public function change(): void
10+
{
11+
$this->table('saga_store_relation', ['id' => false, 'primary_key' => ['id', 'saga_id']])
12+
->addColumn('id', 'string', ['limit' => 50, 'null' => false])
13+
->addColumn('saga_id', 'string', ['limit' => 50, 'null' => false])
14+
->addForeignKey('id', 'saga_store', 'id')
15+
->create();
16+
}
17+
}

resources/schema.sql

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ CREATE TABLE `event_store_relation` (
1616
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
1717

1818
CREATE TABLE `saga_store` (
19-
`saga_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
19+
`id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
2020
`saga_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
2121
`payload` json NOT NULL,
2222
`created_at` timestamp(6) NOT NULL,
2323
`updated_at` timestamp(6) NULL DEFAULT NULL,
24-
PRIMARY KEY (`saga_id`, `saga_name`)
24+
PRIMARY KEY (`id`)
25+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
26+
27+
CREATE TABLE `saga_store_relation` (
28+
`id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
29+
`saga_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
30+
PRIMARY KEY (`id`,`saga_id`),
31+
CONSTRAINT `saga_store_relation_ibfk_1` FOREIGN KEY (`id`) REFERENCES `saga_store` (`id`)
2532
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\RdbmsEventStoreDoctrineDbal\Saga\TableSchema;
6+
7+
final readonly class SagaStoreRelationTableSchema
8+
{
9+
public function __construct(
10+
public string $tableName,
11+
public string $idFieldName,
12+
public string $sagaIdFieldName,
13+
) {}
14+
}

src/Saga/TableSchema/SagaStoreTableSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
public function __construct(
1010
public string $tableName,
11-
public string $sagaIdFieldName,
11+
public string $idFieldName,
1212
public string $sagaNameFieldName,
1313
public string $payloadFieldName,
1414
public string $createdAtFieldName,

src/Saga/TableSchema/SagaTableSchemaFactory.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
public static function createDefaultSagaStore(
1010
string $tableName = 'saga_store',
11-
string $sagaIdFieldName = 'saga_id',
11+
string $idFieldName = 'id',
1212
string $sagaNameFieldName = 'saga_name',
1313
string $payloadFieldName = 'payload',
1414
string $createdAtFieldName = 'created_at',
@@ -18,7 +18,7 @@ public static function createDefaultSagaStore(
1818
): SagaStoreTableSchema {
1919
return new SagaStoreTableSchema(
2020
$tableName,
21-
$sagaIdFieldName,
21+
$idFieldName,
2222
$sagaNameFieldName,
2323
$payloadFieldName,
2424
$createdAtFieldName,
@@ -27,4 +27,16 @@ public static function createDefaultSagaStore(
2727
$updatedAtFieldFormat,
2828
);
2929
}
30+
31+
public static function createDefaultSagaStoreRelation(
32+
string $tableName = 'saga_store_relation',
33+
string $idFieldName = 'id',
34+
string $sagaIdFieldName = 'saga_id',
35+
): SagaStoreRelationTableSchema {
36+
return new SagaStoreRelationTableSchema(
37+
$tableName,
38+
$idFieldName,
39+
$sagaIdFieldName,
40+
);
41+
}
3042
}

tests/Saga/TableSchema/SagaTableSchemaFactoryTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
final class SagaTableSchemaFactoryTest extends TestCase
1515
{
1616
#[Test]
17-
public function itShouldCreateDefaultEventStoreTableSchema(): void
17+
public function itShouldCreateDefaultSagaStoreTableSchema(): void
1818
{
1919
$schema = SagaTableSchemaFactory::createDefaultSagaStore();
2020

2121
self::assertSame('saga_store', $schema->tableName);
22-
self::assertSame('saga_id', $schema->sagaIdFieldName);
22+
self::assertSame('id', $schema->idFieldName);
2323
self::assertSame('saga_name', $schema->sagaNameFieldName);
2424
self::assertSame('payload', $schema->payloadFieldName);
2525
self::assertSame('created_at', $schema->createdAtFieldName);
@@ -29,11 +29,11 @@ public function itShouldCreateDefaultEventStoreTableSchema(): void
2929
}
3030

3131
#[Test]
32-
public function itShouldCreateCustomEventStoreTableSchema(): void
32+
public function itShouldCreateCustomSagaStoreTableSchema(): void
3333
{
3434
$schema = SagaTableSchemaFactory::createDefaultSagaStore(
3535
'custom_saga_store',
36-
'custom_saga_id',
36+
'custom_id',
3737
'custom_saga_name',
3838
'custom_payload',
3939
'custom_created_at',
@@ -43,12 +43,36 @@ public function itShouldCreateCustomEventStoreTableSchema(): void
4343
);
4444

4545
self::assertSame('custom_saga_store', $schema->tableName);
46-
self::assertSame('custom_saga_id', $schema->sagaIdFieldName);
46+
self::assertSame('custom_id', $schema->idFieldName);
4747
self::assertSame('custom_saga_name', $schema->sagaNameFieldName);
4848
self::assertSame('custom_payload', $schema->payloadFieldName);
4949
self::assertSame('custom_created_at', $schema->createdAtFieldName);
5050
self::assertSame('custom_created_at_format', $schema->createdAtFieldFormat);
5151
self::assertSame('custom_updated_at', $schema->updatedAtFieldName);
5252
self::assertSame('custom_updated_at_format', $schema->updatedAtFieldFormat);
5353
}
54+
55+
#[Test]
56+
public function itShouldCreateDefaultSagaStoreRelationTableSchema(): void
57+
{
58+
$schema = SagaTableSchemaFactory::createDefaultSagaStoreRelation();
59+
60+
self::assertSame('saga_store_relation', $schema->tableName);
61+
self::assertSame('id', $schema->idFieldName);
62+
self::assertSame('saga_id', $schema->sagaIdFieldName);
63+
}
64+
65+
#[Test]
66+
public function itShouldCreateCustomSagaStoreRelationTableSchema(): void
67+
{
68+
$schema = SagaTableSchemaFactory::createDefaultSagaStoreRelation(
69+
'custom_saga_store',
70+
'custom_id',
71+
'custom_saga_id',
72+
);
73+
74+
self::assertSame('custom_saga_store', $schema->tableName);
75+
self::assertSame('custom_id', $schema->idFieldName);
76+
self::assertSame('custom_saga_id', $schema->sagaIdFieldName);
77+
}
5478
}

0 commit comments

Comments
 (0)