Skip to content

Commit bd0adbe

Browse files
committed
Merge branch '8.x'
2 parents ec89601 + e17aa0a commit bd0adbe

7 files changed

Lines changed: 77 additions & 20 deletions

File tree

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.28"
11+
image: "gcr.io/cloud-spanner-emulator/emulator:1.5.30"

src/Query/Grammar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function compileInsertGetId(Builder $query, $values, $sequence)
6565
*/
6666
protected function compileLock(Builder $query, $value)
6767
{
68-
return '';
68+
return $value === true ? 'for update' : '';
6969
}
7070

7171
/**

src/Query/Processor.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,21 @@ public function processColumns($results)
133133

134134
/**
135135
* {@inheritDoc}
136-
* @param array{ index_name: string }&array<string, mixed> $results
137-
* @return array<array-key, string>
136+
* @param list<array<string, mixed>> $results
137+
* @return list<array{name: string, columns: list<string>, type: string, unique: bool, primary: bool}>
138138
*/
139139
public function processIndexes($results)
140140
{
141141
return array_map(function ($result) {
142-
return ((object) $result)->index_name;
142+
$result = (object) $result;
143+
144+
return [
145+
'name' => $name = $result->name,
146+
'columns' => $result->columns ? explode(',', $result->columns) : [],
147+
'type' => strtolower($result->type),
148+
'unique' => (bool) $result->unique,
149+
'primary' => $name === 'PRIMARY_KEY',
150+
];
143151
}, $results);
144152
}
145153

src/Schema/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function dropAllTables()
145145
$queries = [];
146146
foreach ($sortedTables as $tableData) {
147147
$tableName = $tableData['name'];
148-
$indexes = $this->getIndexes($tableName);
148+
$indexes = $this->getIndexListing($tableName);
149149
$blueprint = $this->createBlueprint($tableName);
150150
foreach ($indexes as $index) {
151151
if ($index === 'PRIMARY_KEY') {

src/Schema/Grammar.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,20 @@ public function compileTables($schema)
5959
*/
6060
public function compileIndexes($schema, $table)
6161
{
62-
return sprintf(
63-
'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = %s',
64-
$this->quoteString($table),
65-
);
62+
return implode(' ', [
63+
'select',
64+
implode(', ', [
65+
'i.index_name as `name`',
66+
'string_agg(c.column_name, \',\') as `columns`',
67+
'i.index_type as `type`',
68+
'i.is_unique as `unique`',
69+
]),
70+
'from information_schema.indexes as i',
71+
'join information_schema.index_columns as c on i.table_schema = c.table_schema and i.table_name = c.table_name and i.index_name = c.index_name',
72+
'where i.table_schema = ' . $this->quoteString(''),
73+
'and i.table_name = ' . $this->quoteString($table),
74+
'group by i.index_name, i.index_type, i.is_unique',
75+
]);
6676
}
6777

6878
/**

tests/Query/BuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,15 +994,15 @@ public function test_lock(): void
994994
$conn = $this->getDefaultConnection();
995995
$qb = $conn->table(self::TABLE_NAME_USER);
996996
$sql = $qb->lock()->toRawSql();
997-
$this->assertSame('select * from `User`', $sql);
997+
$this->assertSame('select * from `User` for update', $sql);
998998
}
999999

10001000
public function test_lockForUpdate(): void
10011001
{
10021002
$conn = $this->getDefaultConnection();
10031003
$qb = $conn->table(self::TABLE_NAME_USER);
10041004
$sql = $qb->lockForUpdate()->toRawSql();
1005-
$this->assertSame('select * from `User`', $sql);
1005+
$this->assertSame('select * from `User` for update', $sql);
10061006
}
10071007

10081008
public function test_sharedLock(): void

tests/Schema/BuilderTestLast.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public function test_getColumns(): void
245245
], Arr::first($sb->getColumns($table)));
246246
}
247247

248-
public function test_getAllTables(): void
248+
public function test_getTableListing(): void
249249
{
250250
$conn = $this->getDefaultConnection();
251251
$sb = $conn->getSchemaBuilder();
@@ -256,14 +256,53 @@ public function test_getAllTables(): void
256256
$table->primary('id');
257257
});
258258

259-
/** @var array{ name: string, type: string } $row */
260-
$row = Arr::first(
261-
$sb->getTables(),
262-
static fn(array $row): bool => $row['name'] === $table,
263-
);
259+
$this->assertContains($table, $sb->getTableListing());
260+
}
264261

265-
$this->assertSame($table, $row['name']);
266-
$this->assertSame('BASE TABLE', $row['type']);
262+
public function test_getIndexes(): void
263+
{
264+
$conn = $this->getDefaultConnection();
265+
$sb = $conn->getSchemaBuilder();
266+
$table = $this->generateTableName(class_basename(__CLASS__));
267+
$sb->create($table, function (Blueprint $table) {
268+
$table->uuid('id')->primary();
269+
$table->uuid('something');
270+
$table->index('something');
271+
});
272+
273+
$this->assertSame([
274+
[
275+
'name' => strtolower($table) . '_something_index',
276+
'columns' => ['something'],
277+
'type' => 'index',
278+
'unique' => false,
279+
'primary' => false,
280+
],
281+
[
282+
'name' => 'PRIMARY_KEY',
283+
'columns' => ['id'],
284+
'type' => 'primary_key',
285+
'unique' => true,
286+
'primary' => true,
287+
],
288+
], $sb->getIndexes($table));
289+
}
290+
291+
public function test_getIndexListing(): void
292+
{
293+
$conn = $this->getDefaultConnection();
294+
$sb = $conn->getSchemaBuilder();
295+
$table = $this->generateTableName(class_basename(__CLASS__));
296+
$sb->create($table, function (Blueprint $table) {
297+
$table->uuid('id')->primary();
298+
$table->uuid('something');
299+
$table->index('something');
300+
});
301+
302+
$this->assertSame([
303+
strtolower($table) . '_something_index',
304+
'PRIMARY_KEY',
305+
], $sb->getIndexListing($table));
267306
}
268307

269308
public function test_dropAllTables(): void

0 commit comments

Comments
 (0)