Skip to content

Commit

Permalink
feat: rename table support
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama committed Jan 17, 2025
1 parent ee339af commit 068b147
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,36 @@ public function fullText($columns, $name = null, $algorithm = null)
return $command;
}

/**
* {@inheritdoc}
* @return RenameDefinition
*/
public function rename($to): RenameDefinition
{
$this->commands[] = $command = new RenameDefinition(__FUNCTION__, $to);
return $command;
}

/**
* @param string $name
* @return RenameDefinition
*/
public function addSynonym(string $name): RenameDefinition
{
$this->commands[] = $command = (new RenameDefinition(__FUNCTION__, ''))->synonym($name);
return $command;
}

/**
* @param string $name
* @return RenameDefinition
*/
public function dropSynonym(string $name): RenameDefinition
{
$this->commands[] = $command = (new RenameDefinition(__FUNCTION__, ''))->synonym($name);
return $command;
}

/**
* @see https://cloud.google.com/spanner/docs/ttl#defining_a_row_deletion_policy
* @param string $column
Expand Down
40 changes: 40 additions & 0 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,46 @@ public function compileDropFullText(Blueprint $blueprint, Fluent $command): stri
);
}

/**
* @param Blueprint $blueprint
* @param RenameDefinition $command
* @return string
*/
public function compileRename(Blueprint $blueprint, Fluent $command)
{
$from = $this->wrapTable($blueprint);
$to = $this->wrapTable($command->to);
$schema = "alter table {$from} rename to {$to}";
if ($command->synonym !== null) {
$schema .= " add synonym {$this->wrapTable($command->synonym)}";
}
return $schema;
}

/**
* @param Blueprint $blueprint
* @param RenameDefinition $command
* @return string
*/
public function compileAddSynonym(Blueprint $blueprint, Fluent $command)
{
$table = $this->wrapTable($blueprint);
$synonym = $this->wrapTable($command->synonym);
return "alter table {$table} add synonym {$synonym}";
}

/**
* @param Blueprint $blueprint
* @param RenameDefinition $command
* @return string
*/
public function compileDropSynonym(Blueprint $blueprint, Fluent $command)
{
$table = $this->wrapTable($blueprint);
$synonym = $this->wrapTable($command->synonym);
return "alter table {$table} drop synonym {$synonym}";
}

/**
* @param Blueprint $blueprint
* @param RowDeletionPolicyDefinition $command
Expand Down
40 changes: 40 additions & 0 deletions src/Schema/RenameDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Copyright 2019 Colopl Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Colopl\Spanner\Schema;

use Illuminate\Support\Fluent;

/**
* @property string $name
* @property string $to
* @property string|null $synonym
* @method $this synonym(string|null $name = null)
* @extends Fluent<string, mixed>
*/
class RenameDefinition extends Fluent
{
/**
* @param string $name
* @param string $to
*/
public function __construct(string $name, string $to)
{
parent::__construct(['name' => $name, 'to' => $to]);
}
}

0 comments on commit 068b147

Please sign in to comment.