Skip to content

Commit

Permalink
fix: laravel 10.34.0 changed database internals (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
taka-oyama committed Nov 29, 2023
1 parent 724159a commit c2782e4
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Added
- Add support for [NUMERIC](https://cloud.google.com/spanner/docs/reference/standard-sql/data-types#numeric_type) column type. (#145)

Fixed
- Match internals so that it lines up with laravel 10.34.0. (#150)

# v6.0.0 (2023-11-22)

Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"php": "^8.1",
"ext-grpc": "*",
"ext-json": "*",
"laravel/framework": "^10.32.0",
"laravel/framework": "^10.34.2",
"google/cloud-spanner": "^1.58.4",
"grpc/grpc": "^1.42",
"symfony/cache": "~6",
Expand Down
14 changes: 9 additions & 5 deletions src/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,20 @@ protected function performSpannerCommit(): void
$this->currentTransaction->commit();
}

$this->transactionsManager?->stageTransactions($this->getName());
[$levelBeingCommitted, $this->transactions] = [
$this->transactions,
max(0, $this->transactions - 1),
];

$this->transactions = max(0, $this->transactions - 1);
if ($this->isTransactionFinished()) {
$this->currentTransaction = null;
}

if ($this->afterCommitCallbacksShouldBeExecuted()) {
$this->transactionsManager?->commit($this->getName());
}
$this->transactionsManager?->commit(
$this->getName(),
$levelBeingCommitted,
$this->transactions
);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Query/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ public function processColumnListing($results)
}, $results);
}

/**
* Process the results of a columns query.
*
* @inheritDoc
*/
public function processColumns($results)
{
return array_map(static function (array $result) {
return [
'name' => $result['COLUMN_NAME'],
'type_name' => preg_replace("/\([^)]+\)/", "", $result['SPANNER_TYPE']),
'type' => $result['SPANNER_TYPE'],
'collation' => null,
'nullable' => $result['IS_NULLABLE'] !== 'NO',
'default' => $result['COLUMN_DEFAULT'],
'auto_increment' => false,
'comment' => null,
];
}, $results);
}

/**
* @param array $results
* @return array
Expand Down
16 changes: 2 additions & 14 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class Builder extends BaseBuilder


/**
* @deprecated Will be removed in a future Laravel version.
*
* @return list<array{ name: string, type: string }>
*/
public function getAllTables()
Expand All @@ -44,20 +46,6 @@ public function getAllTables()
);
}

/**
* @inheritDoc
*/
public function getColumnListing($table)
{
$table = $this->connection->getTablePrefix().$table;

$results = $this->connection->select(
$this->grammar->compileColumnListing(), [$table]
);

return $this->connection->getPostProcessor()->processColumnListing($results);
}

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

/**
* Compile the query to determine if a table exists.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileTableExists()
Expand All @@ -48,6 +52,18 @@ public function compileTableExists()
}

/**
* Compile the query to determine the tables.
*
* @return string
*/
public function compileTables()
{
return 'select `table_name` as name 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()
Expand All @@ -56,6 +72,10 @@ public function compileGetAllTables()
}

/**
* Compile the query to determine the list of columns.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return string
*/
public function compileColumnListing()
Expand All @@ -73,6 +93,20 @@ 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 columns.
*
* @param string $table
* @return string
*/
public function compileColumns($table)
{
return sprintf(
'select * from information_schema.columns where table_schema = \'\' and table_name = %s',
$this->quoteString($table),
);
}

/**
* Compile a create table command.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

class BuilderTest extends TestCase
{
public function testInsert(): void
public function test_insert(): void
{
$conn = $this->getDefaultConnection();
$tableName = self::TABLE_NAME_USER;
Expand Down
50 changes: 47 additions & 3 deletions tests/Schema/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,67 @@ public function testCreateInterleavedTable(): void
$this->assertTrue($sb->hasTable(self::TABLE_NAME_RELATION_CHILD_INTERLEAVED));
}

public function test_getTables(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));

$sb->create($table, function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
});

/** @var array{ name: string, type: string } $row */
$row = Arr::first(
$sb->getTables(),
static fn (array $row): bool => $row['name'] === $table,
);

$this->assertSame($table, $row['name']);
}

public function test_getColumns(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));

$sb->create($table, function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
});

$this->assertSame([
'name' => 'id',
'type_name' => 'STRING',
'type' => 'STRING(36)',
'collation' => null,
'nullable' => false,
'default' => null,
'auto_increment' => false,
'comment' => null,
], Arr::first($sb->getColumns($table)));
}

public function test_getAllTables(): void
{
$conn = $this->getDefaultConnection();
$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));

$sb->create(self::TABLE_NAME_CREATED, function (Blueprint $table) {
$sb->create($table, function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
});

/** @var array{ name: string, type: string } $row */
$row = Arr::first(
$sb->getAllTables(),
static fn (array $row): bool => $row['name'] === self::TABLE_NAME_CREATED,
static fn (array $row): bool => $row['name'] === $table,
);

$this->assertSame(self::TABLE_NAME_CREATED, $row['name']);
$this->assertSame($table, $row['name']);
$this->assertSame('BASE TABLE', $row['type']);
}
}
5 changes: 5 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
protected const TABLE_NAME_ITEM_TAG = 'ItemTag';
protected const TABLE_NAME_ARRAY_TEST = 'ArrayTest';

protected function generateTableName(string $prefix): string
{
return $prefix . '_' . date('Ymd_His_v');
}

/**
* @return string
*/
Expand Down

0 comments on commit c2782e4

Please sign in to comment.