Skip to content

Commit

Permalink
feat: add support for GENERATE_UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama committed Jan 26, 2024
1 parent 017e198 commit 8b54515
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Added
- `Schema\Builder::dropAllTables` works properly, dropping foreign keys, indexes, then tables in order of interleaving (#161)
- Support for inserting and selecting array of DateTime/Numeric objects (#168)
- Allow pretending for DDL statements (#170)
- Support for GENERATE_UUID() in migrations (#174)

Changed
- `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156)
Expand Down
12 changes: 11 additions & 1 deletion src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Colopl\Spanner\Concerns\MarksAsNotSupported;
use Illuminate\Database\Schema\Blueprint as BaseBlueprint;
use Illuminate\Database\Schema\ColumnDefinition;
use Colopl\Spanner\Schema\ColumnDefinition as SpannerColumnDefinition;
use Illuminate\Support\Fluent;

/**
Expand Down Expand Up @@ -93,7 +94,16 @@ public function tinyIncrements($column)
$this->markAsNotSupported('AUTO_INCREMENT');
}

// region Spanner Specific Types
/**
* @inheritDoc
* @return SpannerColumnDefinition
*/
public function uuid($column = 'uuid')
{
$definition = new SpannerColumnDefinition(['type' => 'uuid', 'name' => $column]);
$this->addColumnDefinition($definition);
return $definition;
}

/**
* @param string $column
Expand Down
14 changes: 14 additions & 0 deletions src/Schema/ColumnDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Colopl\Spanner\Schema;

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

class ColumnDefinition extends BaseColumnDefinition
{
public function generateUuid(): static
{
return $this->default(new Expression('generate_uuid()'));
}
}
34 changes: 30 additions & 4 deletions tests/Schema/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class BlueprintTest extends TestCase
{
public function testCreateTable(): void
public function test_create(): void
{
$conn = $this->getDefaultConnection();

Expand All @@ -37,7 +37,7 @@ public function testCreateTable(): void
$table->integer('int');
$table->float('float');
$table->decimal('decimal');
$table->string('name');
$table->string('string');
$table->char('char');
$table->text('text');
$table->mediumText('medium_text');
Expand All @@ -58,7 +58,7 @@ public function testCreateTable(): void
'`int` int64 not null',
'`float` float64 not null',
'`decimal` numeric not null',
'`name` string(255) not null',
'`string` string(255) not null',
'`char` string(255) not null',
'`text` string(max) not null',
'`medium_text` string(max) not null',
Expand All @@ -72,7 +72,33 @@ public function testCreateTable(): void
);
}

public function test_dropTable(): void
public function test_create_with_generateUuid(): void
{
$conn = $this->getDefaultConnection();

$blueprint = new Blueprint('t', function (Blueprint $table) {
$table->uuid('id')->primary()->generateUuid();
$table->string('name');
});
$blueprint->create();

$queries = $blueprint->toSql($conn, new Grammar());
$this->assertSame(
'create table `t` (' . implode(', ', [
'`id` string(36) not null default (generate_uuid())',
'`name` string(255) not null',
]) . ') primary key (`id`)',
$queries[0]
);

$conn->runDdlBatch($queries);
$conn->table('t')->insert(['name' => 't']);
$row = $conn->table('t')->first();
$this->assertSame(36, strlen($row['id']));
$this->assertSame('t', $row['name']);
}

public function test_drop(): void
{
$conn = $this->getDefaultConnection();

Expand Down

0 comments on commit 8b54515

Please sign in to comment.