From 4a37fdb961498c748188bbd7cb653e1153e28738 Mon Sep 17 00:00:00 2001 From: Don Hardman Date: Tue, 14 Jan 2025 10:58:42 +0700 Subject: [PATCH] Feature: drop sharded table if exists --- src/Plugin/Sharding/DropHandler.php | 23 ++++++++++++++++++++--- src/Plugin/Sharding/Payload.php | 6 ++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/Plugin/Sharding/DropHandler.php b/src/Plugin/Sharding/DropHandler.php index f20a3e22..fc700866 100644 --- a/src/Plugin/Sharding/DropHandler.php +++ b/src/Plugin/Sharding/DropHandler.php @@ -84,7 +84,11 @@ protected function validate(): ?Task { */ $result = $resp->getResult(); if (!isset($result[0]['data'][0])) { - return static::getErrorTask( + // In case quiet mode we have IF EXISTS so do nothing + if ($this->payload->quiet) { + return $this->getNoneTask(); + } + return $this->getErrorTask( "table '{$this->payload->table}' is missing: " . 'DROP SHARDED TABLE failed: ' ."table '{$this->payload->table}' must exist" @@ -92,7 +96,7 @@ protected function validate(): ?Task { } if (false === stripos($result[0]['data'][0]['Create Table'], "type='distributed'")) { - return static::getErrorTask( + return $this->getErrorTask( "table '{$this->payload->table}' is not distributed: " . 'DROP SHARDED TABLE failed: ' ."table '{$this->payload->table}' must be distributed" @@ -102,7 +106,7 @@ protected function validate(): ?Task { // In case we have no state, means table is not sharded $state = $this->getTableState($this->payload->table); if (!$state) { - return static::getErrorTask( + return $this->getErrorTask( "table '{$this->payload->table}' is not sharded: " . 'DROP SHARDED TABLE failed: ' ."table '{$this->payload->table}' be created with sharding" @@ -147,6 +151,19 @@ protected function getErrorTask(string $message): Task { )->run(); } + /** + * Get none task that does nothing for if exists + * @return Task + */ + protected function getNoneTask(): Task { + $taskFn = static function (): TaskResult { + return TaskResult::none(); + }; + return Task::create( + $taskFn, [] + )->run(); + } + /** * Get task function for handling sharding case * @return Closure diff --git a/src/Plugin/Sharding/Payload.php b/src/Plugin/Sharding/Payload.php index 60f90819..2bad7de7 100644 --- a/src/Plugin/Sharding/Payload.php +++ b/src/Plugin/Sharding/Payload.php @@ -27,6 +27,7 @@ final class Payload extends BasePayload { public string $table; public string $structure; public string $extra; + public bool $quiet; /** @var array */ public array $options; @@ -97,7 +98,7 @@ protected static function fromCreate(Request $request): static { $self = new static(); // We just need to do something, but actually its' just for PHPstan $self->path = $request->path; - $self->type = stripos($request->payload, 'create') === 0 ? 'create' : 'alter'; + $self->type = $request->command === 'create' ? 'create' : 'alter'; $self->cluster = $matches['cluster'] ?? ''; $self->table = $matches['table']; $self->structure = $matches['structure']; @@ -113,7 +114,7 @@ protected static function fromCreate(Request $request): static { * @throws QueryParseError */ protected static function fromDrop(Request $request): static { - $pattern = '/DROP\s+SHARDED\s+TABLE\s+' + $pattern = '/DROP\s+SHARDED\s+TABLE\s+(?PIF\s+EXISTS\s+)?' . '(?:(?P[^:\s]+):)?(?P[^:\s\()]+)/ius'; if (!preg_match($pattern, $request->payload, $matches)) { throw QueryParseError::create('Failed to parse query'); @@ -122,6 +123,7 @@ protected static function fromDrop(Request $request): static { $self = new static(); $self->path = $request->path; $self->type = 'drop'; + $self->quiet = !!$matches['quiet']; $self->cluster = $matches['cluster']; $self->table = $matches['table']; $self->structure = '';