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: IDENTITY column support #243

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# v8.3.0 (2024-11-08)

- add support for IDENTITY columns (#243)
- consolidate schema options formatting (#241)
- add support for invisible columns (#240)
- add support for change streams using Blueprint (#230)
Expand Down
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ services:
depends_on:
- emulator
emulator:
image: "gcr.io/cloud-spanner-emulator/emulator:1.5.23"
image: "gcr.io/cloud-spanner-emulator/emulator:1.5.28"
2 changes: 2 additions & 0 deletions src/Schema/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Colopl\Spanner\Schema;

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\ColumnDefinition as BaseColumnDefinition;

/**
Expand All @@ -33,6 +34,7 @@
* @property int|null $precision
* @property int|null $scale
* @property bool|null $useCurrent
* @property string|Expression|true|null $generatedAs
* @property string|null $virtualAs
* @property bool|null $storedAs
*/
Expand Down
26 changes: 25 additions & 1 deletion src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Grammar extends BaseGrammar
/**
* @inheritdoc
*/
protected $modifiers = ['Nullable', 'Default', 'Invisible', 'UseSequence'];
protected $modifiers = ['Nullable', 'Default', 'GeneratedAs', 'Invisible', 'UseSequence'];

/**
* Compile the query to determine the tables.
Expand Down Expand Up @@ -733,6 +733,30 @@ protected function modifyInvisible(Blueprint $blueprint, Fluent $column)
: null;
}

/**
* Get the SQL for an identity column modifier.
*
* @param Blueprint $blueprint
* @param ColumnDefinition $column
* @return string|null
*/
protected function modifyGeneratedAs(Blueprint $blueprint, Fluent $column): ?string
{
$as = $column->generatedAs;

if ($as === null) {
return null;
}

$expression = match (true) {
$as === true => 'bit_reversed_positive',
$as instanceof Expression => $this->getValue($as),
default => $as,
};

return " generated by default as identity ({$expression})";
}

/**
* @param Blueprint $blueprint
* @param IntColumnDefinition $column
Expand Down
6 changes: 6 additions & 0 deletions tests/Schema/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ public function test_default_values(): void
$table->uuid('id');
$table->integer('null')->default(null)->nullable();
$table->integer('int')->default(1);
$table->integer('int_gen')->generatedAs();
$table->integer('int_genc')->generatedAs('bit_reversed_positive start counter with 2');
taka-oyama marked this conversation as resolved.
Show resolved Hide resolved
$table->integer('int_seq')->useSequence();
$table->bigInteger('bigint')->default(1);
$table->integer('bigint_seq')->useSequence();
Expand Down Expand Up @@ -733,6 +735,8 @@ public function test_default_values(): void
'`id` string(36) not null',
'`null` int64',
'`int` int64 not null default (1)',
'`int_gen` int64 not null generated by default as identity (bit_reversed_positive)',
'`int_genc` int64 not null generated by default as identity (bit_reversed_positive start counter with 2)',
'`int_seq` int64 not null default (get_next_sequence_value(sequence `' . $tableName . '_int_seq_sequence`))',
'`bigint` int64 not null default (1)',
'`bigint_seq` int64 not null default (get_next_sequence_value(sequence `' . $tableName . '_bigint_seq_sequence`))',
Expand Down Expand Up @@ -772,6 +776,8 @@ public function test_default_values(): void

$this->assertSame(null, $result['null']);
$this->assertSame(1, $result['int']);
$this->assertIsInt($result['int_gen']);
$this->assertIsInt($result['int_genc']);
$this->assertIsInt($result['int_seq']);
$this->assertSame(1, $result['bigint']);
$this->assertIsInt($result['bigint_seq']);
Expand Down
Loading