Skip to content

Commit

Permalink
Feature: drop sharded table if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
donhardman committed Jan 15, 2025
1 parent 59f3eea commit 4a37fdb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
23 changes: 20 additions & 3 deletions src/Plugin/Sharding/DropHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,19 @@ 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"
);
}

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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/Plugin/Sharding/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class Payload extends BasePayload {
public string $table;
public string $structure;
public string $extra;
public bool $quiet;
/** @var array<string,int|string> */
public array $options;

Expand Down Expand Up @@ -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'];
Expand All @@ -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+(?P<quiet>IF\s+EXISTS\s+)?'
. '(?:(?P<cluster>[^:\s]+):)?(?P<table>[^:\s\()]+)/ius';
if (!preg_match($pattern, $request->payload, $matches)) {
throw QueryParseError::create('Failed to parse query');
Expand All @@ -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 = '';
Expand Down

0 comments on commit 4a37fdb

Please sign in to comment.