Skip to content

Commit

Permalink
feat: laravel 11 support (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama authored Apr 11, 2024
1 parent 23daae1 commit 4caec32
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 120 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
# v7.2.0 (Not released yet)
# v8.0.0 (2024-04-11)

Added
- Laravel 11 Support (#200)
- The following deprecated methods have been removed
- `Schema\Builder::getAllTables()`
- `Schema\Builder::getIndexListing()`
- `Schema\Grammar::compileTableExists()`
- `Schema\Grammar::compileGetAllTables()`
- `Schema\Grammar::compileColumnListing()`
- `Schema\Grammar::compileIndexListing()`
- `Query\Processor::processColumnListing()`
- `Query\Processor::processIndexListing()`
- `Blueprint::decimal()` can no longer specify `unsigned` flag.

# v7.2.0 (2024-03-27)

Added
- Support for `Query\Builder::upsert()` (#203)
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.1-cli-alpine
FROM php:8.2-cli-alpine

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
{"name": "Takayasu Oyama", "email": "[email protected]"}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"ext-grpc": "*",
"ext-json": "*",
"laravel/framework": "^10.37.0",
"laravel/framework": "^11",
"google/cloud-spanner": "^1.58.4",
"grpc/grpc": "^1.42",
"symfony/cache": "~6",
"symfony/cache": "~7",
"symfony/deprecation-contracts": "~2",
"symfony/lock": "~6"
"symfony/lock": "~7"
},
"require-dev": {
"orchestra/testbench": "~8",
"phpunit/phpunit": "~10.0",
"orchestra/testbench": "~9",
"phpunit/phpunit": "~11.0",
"phpstan/phpstan": "^1"
},
"autoload": {
Expand Down
30 changes: 30 additions & 0 deletions src/Query/ArrayValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Query;

/**
* Just a temp object wrapper so UPDATE statements can handle array types
*/
final readonly class ArrayValue
{
public function __construct(
public array $value
)
{
}
}
15 changes: 15 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Colopl\Spanner\Connection;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Arr;
use LogicException;

Expand All @@ -44,6 +45,20 @@ public function insert(array $values)
return parent::insert($this->prepareInsertForDml($values));
}

/**
* @inheritDoc
*/
public function update(array $values)
{
foreach ($values as $key => $value) {
if (is_array($value)) {
$values[$key] = new ArrayValue($value);
}
}

return parent::update($values);
}

/**
* @inheritDoc
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Query/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ protected function compileLock(Builder $query, $value)
return '';
}

/**
* @inheritDoc
*/
public function prepareBindingsForUpdate(array $bindings, array $values)
{
$bindings = parent::prepareBindingsForUpdate($bindings, $values);
foreach ($bindings as $key => $value) {
if ($value instanceof ArrayValue) {
$bindings[$key] = $value->value;
}
}
return $bindings;
}

/**
* @inheritDoc
*/
Expand Down
20 changes: 0 additions & 20 deletions src/Query/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ protected function processColumn(mixed $value): mixed
return $value;
}

/**
* @inheritDoc
*/
public function processColumnListing($results)
{
return array_map(function ($result) {
return ((object) $result)->column_name;
}, $results);
}

/**
* Process the results of a columns query.
*
Expand All @@ -94,16 +84,6 @@ public function processColumns($results)
}, $results);
}

/**
* @deprecated Use processIndexes($results) instead.
* @param array $results
* @return array
*/
public function processIndexListing($results)
{
return self::processIndexes($results);
}

/**
* @param array $results
* @return array
Expand Down
12 changes: 4 additions & 8 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,10 @@ public function uuid($column = 'uuid')
/**
* @param string $column
* @param int|null $length
* @param bool $fixed
* @return ColumnDefinition
*/
public function binary($column, $length = null)
public function binary($column, $length = null, $fixed = false)
{
$length = $length ?: Builder::$defaultBinaryLength;

Expand All @@ -138,10 +139,9 @@ public function binary($column, $length = null)
* @param string $column
* @param int $total
* @param int $places
* @param bool $unsigned
* @return ColumnDefinition
*/
public function decimal($column, $total = 38, $places = 9, $unsigned = false)
public function decimal($column, $total = 38, $places = 9)
{
if ($total !== 38) {
$this->markAsNotSupported('decimal with precision other than 38');
Expand All @@ -151,11 +151,7 @@ public function decimal($column, $total = 38, $places = 9, $unsigned = false)
$this->markAsNotSupported('decimal with scale other than 9');
}

if ($unsigned) {
$this->markAsNotSupported('unsigned decimal');
}

return parent::decimal($column, $total, $places, $unsigned);
return parent::decimal($column, $total, $places);
}

/**
Expand Down
23 changes: 0 additions & 23 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ class Builder extends BaseBuilder
*/
public static $defaultMorphKeyType = 'uuid';

/**
* @deprecated Will be removed in a future Laravel version.
*
* @return list<array{ name: string, type: string }>
*/
public function getAllTables()
{
return $this->connection->select(
$this->grammar->compileGetAllTables()
);
}

/**
* @inheritDoc Adds a parent key, for tracking interleaving
*
Expand All @@ -64,17 +52,6 @@ public function getTables()
);
}

/**
* @deprecated Use getIndexes($table) instead
*
* @param string $table
* @return string[]
*/
public function getIndexListing($table)
{
return parent::getIndexes($table);
}

/**
* @param string $table
* @param string $name
Expand Down
46 changes: 0 additions & 46 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ class Grammar extends BaseGrammar
*/
protected $modifiers = ['Nullable', 'Default', 'UseSequence'];

/**
* Compile the query to determine if a table exists.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileTableExists()
{
return 'select * from information_schema.tables where table_schema = \'\' and table_name = ?';
}

/**
* Compile the query to determine the tables.
*
Expand All @@ -62,40 +50,6 @@ public function compileTables()
return 'select `table_name` as name, `table_type` as type, `parent_table_name` as parent from information_schema.tables where table_schema = \'\' and table_type = \'BASE TABLE\'';
}

/**
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileGetAllTables()
{
return 'select `table_name` as name, `table_type` as type from information_schema.tables where table_schema = \'\' and table_type = \'BASE TABLE\'';
}

/**
* Compile the query to determine the list of columns.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileColumnListing()
{
return 'select column_name as `column_name` from information_schema.columns where table_schema = \'\' and table_name = ?';
}

/**
* Compile the query to determine the list of indexes.
*
* @deprecated Use compileIndexes($table) instead.
*
* @return string
*/
public function compileIndexListing()
{
return 'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = ?';
}

/**
* Compile the query to determine the list of indexes.
*
Expand Down
20 changes: 10 additions & 10 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ public function testQueryExecutedEvent(): void
$tableName = self::TABLE_NAME_USER;
$uuid = $this->generateUuid();
$name = 'test';
$conn->insert("INSERT INTO ${tableName} (`userId`, `name`) VALUES ('${uuid}', '${name}')");
$conn->insert("INSERT INTO {$tableName} (`userId`, `name`) VALUES ('{$uuid}', '{$name}')");
$afterName = 'test2';
$conn->update("UPDATE ${tableName} SET `name` = '${afterName}' WHERE `userId` = '${uuid}'");
$conn->delete("DELETE FROM ${tableName} WHERE `userId` = '${uuid}'");
$conn->update("UPDATE {$tableName} SET `name` = '{$afterName}' WHERE `userId` = '{$uuid}'");
$conn->delete("DELETE FROM {$tableName} WHERE `userId` = '{$uuid}'");

$this->assertSame(5, $executedCount);
}
Expand Down Expand Up @@ -471,36 +471,36 @@ public function test_stale_reads(): void
$timestamp = null;
$db->runTransaction(function(Transaction $tx) use ($tableName, $uuid, &$timestamp) {
$name = 'first';
$tx->executeUpdate("INSERT INTO ${tableName} (`userId`, `name`) VALUES ('${uuid}', '${name}')");
$tx->executeUpdate("INSERT INTO {$tableName} (`userId`, `name`) VALUES ('{$uuid}', '{$name}')");
$timestamp = $tx->commit();
});
$db->close();
$this->assertNotEmpty($timestamp);

$timestampBound = new StrongRead();
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$this->assertCount(1, $rows);
$this->assertSame($uuid, $rows[0]['userId']);
$this->assertSame('first', $rows[0]['name']);

$oldDatetime = Carbon::instance($timestamp->get())->subSecond();

$timestampBound = new ReadTimestamp($oldDatetime);
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$this->assertEmpty($rows);

$timestampBound = new ExactStaleness(10);
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$this->assertEmpty($rows);

$timestampBound = new MaxStaleness(10);
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$this->assertCount(1, $rows);
$this->assertSame($uuid, $rows[0]['userId']);
$this->assertSame('first', $rows[0]['name']);

$timestampBound = new MinReadTimestamp($oldDatetime);
$rows = $conn->selectWithOptions("SELECT * FROM ${tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$rows = $conn->selectWithOptions("SELECT * FROM {$tableName} WHERE userID = ?", [$uuid], $timestampBound->transactionOptions());
$this->assertCount(1, $rows);
$this->assertSame($uuid, $rows[0]['userId']);
$this->assertSame('first', $rows[0]['name']);
Expand All @@ -518,7 +518,7 @@ public function testEventListenOrder(): void
$tableName = self::TABLE_NAME_USER;
$uuid = $this->generateUuid();
$name = 'test';
$conn->insert("INSERT INTO ${tableName} (`userId`, `name`) VALUES ('${uuid}', '${name}')");
$conn->insert("INSERT INTO {$tableName} (`userId`, `name`) VALUES ('{$uuid}', '{$name}')");

$this->assertCount(3, $receivedEventClasses);
$this->assertSame(TransactionBeginning::class, $receivedEventClasses[0]);
Expand Down
10 changes: 5 additions & 5 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function testInsertDatetime(): void
$qb = $conn->table($tableName);

$row = $this->generateTestRow();
$carbonMax = Carbon::maxValue();
$carbonMax = Carbon::create(9999, 12, 31, 23, 59, 59, 'UTC');
$row['timestampTest'] = $carbonMax;
$qb->insert($row);

Expand Down Expand Up @@ -658,13 +658,13 @@ public function testWhereDatetime(): void
$qb = $conn->table($tableName);

$row = $this->generateTestRow();
$carbonMax = Carbon::maxValue();
$carbonMax = Carbon::create(9999, 12, 31, 23, 59, 59, 'UTC');
$row['timestampTest'] = $carbonMax;
$qb->insert($row);

$this->assertSame(1, $qb->where('timestampTest', '=', Carbon::maxValue())->count());
$this->assertSame(1, $qb->where('timestampTest', '<=', Carbon::maxValue())->count());
$this->assertSame(0, $qb->where('timestampTest', '<', Carbon::maxValue())->count());
$this->assertSame(1, $qb->where('timestampTest', '=', $carbonMax)->count());
$this->assertSame(1, $qb->where('timestampTest', '<=', $carbonMax)->count());
$this->assertSame(0, $qb->where('timestampTest', '<', $carbonMax)->count());
}

/**
Expand Down

0 comments on commit 4caec32

Please sign in to comment.