Skip to content

Commit 55e10ec

Browse files
committed
add subscription setup cli commands & skip setup in subscription run
1 parent fe23cbc commit 55e10ec

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Console\Command;
6+
7+
use Patchlevel\EventSourcing\Console\OutputStyle;
8+
use Patchlevel\EventSourcing\Store\Store;
9+
use Patchlevel\EventSourcing\Store\SubscriptionStore;
10+
use Symfony\Component\Console\Attribute\AsCommand;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
15+
#[AsCommand(
16+
'event-sourcing:schema:subscription-setup',
17+
'setup subscription (pub/sub) for store',
18+
)]
19+
final class SchemaSubscriptionSetupCommand extends Command
20+
{
21+
public function __construct(
22+
private readonly Store $store,
23+
) {
24+
parent::__construct();
25+
}
26+
27+
protected function execute(InputInterface $input, OutputInterface $output): int
28+
{
29+
$io = new OutputStyle($input, $output);
30+
31+
if (!$this->store instanceof SubscriptionStore) {
32+
$io->error('store does not support subscriptions');
33+
34+
return 1;
35+
}
36+
37+
if (!$this->store->supportSubscription()) {
38+
$io->error('store does not support subscriptions');
39+
40+
return 1;
41+
}
42+
43+
$this->store->setupSubscription();
44+
45+
return 0;
46+
}
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\Console\Command;
6+
7+
use Patchlevel\EventSourcing\Console\OutputStyle;
8+
use Patchlevel\EventSourcing\Store\Store;
9+
use Patchlevel\EventSourcing\Store\SubscriptionStore;
10+
use Symfony\Component\Console\Attribute\AsCommand;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
15+
use function method_exists;
16+
17+
#[AsCommand(
18+
'event-sourcing:schema:subscription-teardown',
19+
'teardown subscription (pub/sub) for store',
20+
)]
21+
final class SchemaSubscriptionTeardownCommand extends Command
22+
{
23+
public function __construct(
24+
private readonly Store $store,
25+
) {
26+
parent::__construct();
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output): int
30+
{
31+
$io = new OutputStyle($input, $output);
32+
33+
if (!$this->store instanceof SubscriptionStore) {
34+
$io->error('store does not support subscriptions');
35+
36+
return 1;
37+
}
38+
39+
if (!$this->store->supportSubscription()) {
40+
$io->error('store does not support subscriptions');
41+
42+
return 1;
43+
}
44+
45+
if (method_exists($this->store, 'teardownSubscription')) {
46+
$this->store->teardownSubscription();
47+
48+
return 0;
49+
}
50+
51+
$io->error('store does not support teardownSubscription');
52+
53+
return 1;
54+
}
55+
}

src/Console/Command/SubscriptionRunCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ protected function configure(): void
7070
null,
7171
InputOption::VALUE_NONE,
7272
'rebuild (remove & boot) subscriptions before run',
73+
)
74+
->addOption(
75+
'skip-subscription-setup',
76+
null,
77+
InputOption::VALUE_NONE,
78+
'skip subscription setup',
7379
);
7480
}
7581

@@ -81,11 +87,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8187
$timeLimit = InputHelper::nullablePositiveInt($input->getOption('time-limit'));
8288
$sleep = InputHelper::positiveIntOrZero($input->getOption('sleep'));
8389
$rebuild = InputHelper::bool($input->getOption('rebuild'));
90+
$skipSubscriptionSetup = InputHelper::bool($input->getOption('skip-subscription-setup'));
8491

8592
$criteria = $this->subscriptionEngineCriteria($input);
8693
$criteria = $this->resolveCriteriaIntoCriteriaWithOnlyIds($criteria);
8794

88-
if ($this->store instanceof SubscriptionStore) {
95+
if ($this->store instanceof SubscriptionStore && !$skipSubscriptionSetup) {
8996
$this->store->setupSubscription();
9097
}
9198

src/Store/DoctrineDbalStore.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,25 @@ public function setupSubscription(): void
410410
));
411411
}
412412

413+
public function teardownSubscription(): void
414+
{
415+
if (!$this->supportSubscription()) {
416+
return;
417+
}
418+
419+
$functionName = $this->createTriggerFunctionName();
420+
421+
$this->connection->executeStatement(sprintf(
422+
'DROP FUNCTION IF EXISTS %s() CASCADE;',
423+
$functionName,
424+
));
425+
426+
$this->connection->executeStatement(sprintf(
427+
'DROP TRIGGER IF EXISTS notify_trigger ON %s;',
428+
$this->config['table_name'],
429+
));
430+
}
431+
413432
private function createTriggerFunctionName(): string
414433
{
415434
$tableConfig = explode('.', $this->config['table_name']);

src/Store/StreamDoctrineDbalStore.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,25 @@ public function setupSubscription(): void
442442
));
443443
}
444444

445+
public function teardownSubscription(): void
446+
{
447+
if (!$this->supportSubscription()) {
448+
return;
449+
}
450+
451+
$functionName = $this->createTriggerFunctionName();
452+
453+
$this->connection->executeStatement(sprintf(
454+
'DROP FUNCTION IF EXISTS %s() CASCADE;',
455+
$functionName,
456+
));
457+
458+
$this->connection->executeStatement(sprintf(
459+
'DROP TRIGGER IF EXISTS notify_trigger ON %s;',
460+
$this->config['table_name'],
461+
));
462+
}
463+
445464
private function createTriggerFunctionName(): string
446465
{
447466
$tableConfig = explode('.', $this->config['table_name']);

src/Store/SubscriptionStore.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public function supportSubscription(): bool;
1111
public function setupSubscription(): void;
1212

1313
public function wait(int $timeoutMilliseconds): void;
14+
15+
// public function teardownSubscription(): void;
1416
}

0 commit comments

Comments
 (0)