Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename table support #259

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 43 additions & 0 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,49 @@ 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 (isset($command->synonym)) {
$synonym = is_string($command->synonym)
? $this->wrapTable($command->synonym)
: $from;
$schema .= ", add synonym {$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|true $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]);
}
}
109 changes: 109 additions & 0 deletions tests/Schema/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,115 @@ public function test_interleaving(): void
], $statements);
}

public function test_rename_table(): void
{
$conn = $this->getDefaultConnection();
$conn->useDefaultSchemaGrammar();
$grammar = $conn->getSchemaGrammar();
$tableName = $this->generateTableName();
$blueprint = new Blueprint($tableName);
$blueprint->id();
$blueprint->create();
$blueprint->build($conn, $grammar);

$blueprint = new Blueprint($tableName);
$blueprint->rename($tableName . '_v2');
$blueprint->build($conn, $grammar);

$statements = $blueprint->toSql($conn, $grammar);
$this->assertSame([
"alter table `{$tableName}` rename to `{$tableName}_v2`",
], $statements);
}

public function test_rename_table_with_synonym(): void
{
$conn = $this->getDefaultConnection();
$conn->useDefaultSchemaGrammar();
$grammar = $conn->getSchemaGrammar();
$tableName = $this->generateTableName();
$blueprint = new Blueprint($tableName);
$blueprint->id();
$blueprint->create();
$blueprint->build($conn, $grammar);

$blueprint = new Blueprint($tableName);
$blueprint->rename($tableName . '_v2')->synonym();
$blueprint->build($conn, $grammar);

$statements = $blueprint->toSql($conn, $grammar);
$this->assertSame([
"alter table `{$tableName}` rename to `{$tableName}_v2`, add synonym `{$tableName}`",
], $statements);
}

public function test_rename_table_with_synonym_custom_name(): void
{
$conn = $this->getDefaultConnection();
$conn->useDefaultSchemaGrammar();
$grammar = $conn->getSchemaGrammar();
$tableName = $this->generateTableName();
$blueprint = new Blueprint($tableName);
$blueprint->id();
$blueprint->create();
$blueprint->build($conn, $grammar);

$blueprint = new Blueprint($tableName);
$blueprint->rename($tableName . '_v2')->synonym($tableName . '_v1');
$blueprint->build($conn, $grammar);

$statements = $blueprint->toSql($conn, $grammar);
$this->assertSame([
"alter table `{$tableName}` rename to `{$tableName}_v2`, add synonym `{$tableName}_v1`",
], $statements);
}

public function test_rename_table_add_synonym(): void
{
$conn = $this->getDefaultConnection();
$conn->useDefaultSchemaGrammar();
$grammar = $conn->getSchemaGrammar();
$tableName = $this->generateTableName();
$blueprint = new Blueprint($tableName);
$blueprint->id();
$blueprint->create();
$blueprint->build($conn, $grammar);

$blueprint = new Blueprint($tableName);
$blueprint->addSynonym($tableName . '_v1');
$blueprint->build($conn, $grammar);

$statements = $blueprint->toSql($conn, $grammar);
$this->assertSame([
"alter table `{$tableName}` add synonym `{$tableName}_v1`",
], $statements);
}

public function test_rename_table_drop_synonym(): void
{
$conn = $this->getDefaultConnection();
$conn->useDefaultSchemaGrammar();
$grammar = $conn->getSchemaGrammar();
$tableName = $this->generateTableName();
$blueprint = new Blueprint($tableName);
$blueprint->id();
$blueprint->create();
$blueprint->build($conn, $grammar);

$blueprint = new Blueprint($tableName);
$blueprint->addSynonym($tableName . '_v1');
$blueprint->build($conn, $grammar);

$blueprint = new Blueprint($tableName);
$blueprint->dropSynonym($tableName . '_v1');
$blueprint->build($conn, $grammar);

$statements = $blueprint->toSql($conn, $grammar);
$this->assertSame([
"alter table `{$tableName}` drop synonym `{$tableName}_v1`",
], $statements);
}

public function test_create_with_row_deletion_policy(): void
{
$conn = $this->getDefaultConnection();
Expand Down
Loading