Skip to content

Commit 0d6df00

Browse files
authored
feat: IDENTITY column support (#243)
1 parent f3d950b commit 0d6df00

File tree

6 files changed

+38
-5
lines changed

6 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# v8.3.0 (2024-11-08)
22

3+
- add support for IDENTITY columns (#243)
34
- consolidate schema options formatting (#241)
45
- add support for invisible columns (#240)
56
- add support for change streams using Blueprint (#230)

compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ services:
88
depends_on:
99
- emulator
1010
emulator:
11-
image: "gcr.io/cloud-spanner-emulator/emulator:1.5.23"
11+
image: "gcr.io/cloud-spanner-emulator/emulator:1.5.28"

phpstan.neon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ parameters:
99
path: src/Concerns/ManagesPartitionedDml.php
1010
- message: '#^Unable to resolve the template type TKey in call to function collect$#'
1111
path: src/Concerns/ManagesSessionPool.php
12-
- message: "#^Strict comparison using \\=\\=\\= between Illuminate\\\\Database\\\\Query\\\\IndexHint and null will always evaluate to false\\.$#"
13-
count: 1
14-
path: src/Query/Builder.php
1512
- message: '#^Parameter \#1 \$pdo of method Illuminate\\Database\\Connection::__construct\(\) expects Closure|PDO, null given\.$#'
1613
path: src/Connection.php
1714
- message: '#^Method Colopl\\Spanner\\Connection::selectWithOptions\(\) should return array<int, array> but returns mixed\.$#'
1815
path: src/Connection.php
16+
- message: "#^Method Colopl\\\\Spanner\\\\Connection\\:\\:cursor\\(\\) should return Generator\\<int, stdClass, mixed, mixed\\> but returns Generator\\<int, array, mixed, mixed\\>\\.$#"
17+
path: src/Connection.php
18+
count: 1
1919
- message: '#^Generator expects value type array, mixed given.$#'
2020
path: src/Connection.php
2121
count: 1

src/Schema/ColumnDefinition.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
namespace Colopl\Spanner\Schema;
2222

23+
use Illuminate\Database\Query\Expression;
2324
use Illuminate\Database\Schema\ColumnDefinition as BaseColumnDefinition;
2425

2526
/**
@@ -33,6 +34,7 @@
3334
* @property int|null $precision
3435
* @property int|null $scale
3536
* @property bool|null $useCurrent
37+
* @property string|Expression|true|null $generatedAs
3638
* @property string|null $virtualAs
3739
* @property bool|null $storedAs
3840
*/

src/Schema/Grammar.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Grammar extends BaseGrammar
3838
/**
3939
* @inheritdoc
4040
*/
41-
protected $modifiers = ['Nullable', 'Default', 'Invisible', 'UseSequence'];
41+
protected $modifiers = ['Nullable', 'Default', 'GeneratedAs', 'Invisible', 'UseSequence'];
4242

4343
/**
4444
* Compile the query to determine the tables.
@@ -733,6 +733,30 @@ protected function modifyInvisible(Blueprint $blueprint, Fluent $column)
733733
: null;
734734
}
735735

736+
/**
737+
* Get the SQL for an identity column modifier.
738+
*
739+
* @param Blueprint $blueprint
740+
* @param ColumnDefinition $column
741+
* @return string|null
742+
*/
743+
protected function modifyGeneratedAs(Blueprint $blueprint, Fluent $column): ?string
744+
{
745+
$as = $column->generatedAs;
746+
747+
if ($as === null) {
748+
return null;
749+
}
750+
751+
$expression = match (true) {
752+
$as === true => 'bit_reversed_positive',
753+
$as instanceof Expression => $this->getValue($as),
754+
default => $as,
755+
};
756+
757+
return " generated by default as identity ({$expression})";
758+
}
759+
736760
/**
737761
* @param Blueprint $blueprint
738762
* @param IntColumnDefinition $column

tests/Schema/BlueprintTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ public function test_default_values(): void
694694
$table->uuid('id');
695695
$table->integer('null')->default(null)->nullable();
696696
$table->integer('int')->default(1);
697+
$table->integer('int_gen')->generatedAs();
698+
$table->integer('int_genc')->generatedAs('bit_reversed_positive start counter with 2');
697699
$table->integer('int_seq')->useSequence();
698700
$table->bigInteger('bigint')->default(1);
699701
$table->integer('bigint_seq')->useSequence();
@@ -733,6 +735,8 @@ public function test_default_values(): void
733735
'`id` string(36) not null',
734736
'`null` int64',
735737
'`int` int64 not null default (1)',
738+
'`int_gen` int64 not null generated by default as identity (bit_reversed_positive)',
739+
'`int_genc` int64 not null generated by default as identity (bit_reversed_positive start counter with 2)',
736740
'`int_seq` int64 not null default (get_next_sequence_value(sequence `' . $tableName . '_int_seq_sequence`))',
737741
'`bigint` int64 not null default (1)',
738742
'`bigint_seq` int64 not null default (get_next_sequence_value(sequence `' . $tableName . '_bigint_seq_sequence`))',
@@ -772,6 +776,8 @@ public function test_default_values(): void
772776

773777
$this->assertSame(null, $result['null']);
774778
$this->assertSame(1, $result['int']);
779+
$this->assertIsInt($result['int_gen']);
780+
$this->assertIsInt($result['int_genc']);
775781
$this->assertIsInt($result['int_seq']);
776782
$this->assertSame(1, $result['bigint']);
777783
$this->assertIsInt($result['bigint_seq']);

0 commit comments

Comments
 (0)