Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"php": "^8.2",
"ext-grpc": "*",
"ext-json": "*",
"laravel/framework": "^11.31.0",
"laravel/framework": "^12.0",
"google/cloud-spanner": "^1.58.4",
"grpc/grpc": "^1.42",
"symfony/cache": "~7",
"symfony/lock": "~7"
},
"require-dev": {
"orchestra/testbench": "~9",
"orchestra/testbench": "~10",
"phpunit/phpunit": "~11.0",
"phpstan/phpstan": "^2"
},
Expand Down
10 changes: 2 additions & 8 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ public function disconnect()
*/
protected function getDefaultQueryGrammar(): QueryGrammar
{
$grammar = new QueryGrammar();
$grammar->setConnection($this);
$this->withTablePrefix($grammar);
return $grammar;
return new QueryGrammar($this);
}

/**
Expand All @@ -214,10 +211,7 @@ protected function getDefaultQueryGrammar(): QueryGrammar
*/
protected function getDefaultSchemaGrammar(): SchemaGrammar
{
$grammar = new SchemaGrammar();
$grammar->setConnection($this);
$this->withTablePrefix($grammar);
return $grammar;
return new SchemaGrammar($this);
}

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

/**
* @param null $schema
* @inheritDoc Adds a parent key, for tracking interleaving
*
* @return list<array{ name: string, type: string, parent: string }>
*/
public function getTables()
public function getTables($schema = null)
{
/** @var list<array{ name: string, type: string, parent: string }> */
return $this->connection->select(
$this->grammar->compileTables(),
$this->grammar->compileTables(null),
);
}

Expand Down Expand Up @@ -87,7 +88,7 @@ protected function createBlueprint($table, ?Closure $callback = null)
/** @phpstan-ignore isset.property */
return isset($this->resolver)
? ($this->resolver)($table, $callback)
: new Blueprint($table, $callback);
: new Blueprint($this->connection, $table, $callback);
}

/**
Expand Down Expand Up @@ -135,7 +136,7 @@ public function dropAllTables()
foreach ($foreigns as $foreign) {
$blueprint->dropForeign($foreign);
}
array_push($queries, ...$blueprint->toSql($connection, $this->grammar));
array_push($queries, ...$blueprint->toSql());
}
/** @var Connection $connection */
$connection->runDdlBatch($queries);
Expand All @@ -153,7 +154,7 @@ public function dropAllTables()
$blueprint->dropIndex($index);
}
$blueprint->drop();
array_push($queries, ...$blueprint->toSql($connection, $this->grammar));
array_push($queries, ...$blueprint->toSql());
}
$connection->runDdlBatch($queries);
}
Expand Down
21 changes: 12 additions & 9 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ class Grammar extends BaseGrammar
/**
* Compile the query to determine the tables.
*
* @param $schema
* @return string
*/
public function compileTables()
public function compileTables($schema)
{
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\'';
}

/**
* Compile the query to determine the list of indexes.
*
* @param string $table
* @param string|null $schema
* @param $table
* @return string
*/
public function compileIndexes($table)
public function compileIndexes($schema, $table)
{
return sprintf(
'select index_name as `index_name` from information_schema.indexes where table_schema = \'\' and table_name = %s',
Expand All @@ -66,10 +68,11 @@ public function compileIndexes($table)
/**
* Compile the query to determine the list of foreign keys.
*
* @param string $table
* @param string|null $schema
* @param $table
* @return string
*/
public function compileForeignKeys($table)
public function compileForeignKeys($schema, $table)
{
return sprintf(
'select constraint_name as `key_name` from information_schema.table_constraints where constraint_type = "FOREIGN KEY" and table_schema = \'\' and table_name = %s',
Expand All @@ -80,10 +83,11 @@ public function compileForeignKeys($table)
/**
* Compile the query to determine the columns.
*
* @param string $table
* @param string|null $schema
* @param $table
* @return string
*/
public function compileColumns($table)
public function compileColumns($schema, $table)
{
return sprintf(
'select * from information_schema.columns where table_schema = \'\' and table_name = %s',
Expand Down Expand Up @@ -134,10 +138,9 @@ public function compileAdd(Blueprint $blueprint, Fluent $command)
*
* @param Blueprint $blueprint
* @param Fluent<string, mixed>&object{ column: ColumnDefinition } $command
* @param Connection $connection
* @return string
*/
public function compileChange(Blueprint $blueprint, Fluent $command, Connection $connection)
public function compileChange(Blueprint $blueprint, Fluent $command)
{
$column = $command->column;

Expand Down
7 changes: 3 additions & 4 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Colopl\Spanner\Connection;
use Colopl\Spanner\Events\MutatingData;
use Colopl\Spanner\Query\Nested;
use Colopl\Spanner\Schema\Grammar;
use Colopl\Spanner\Session\SessionInfo;
use Colopl\Spanner\TimestampBound\ExactStaleness;
use Colopl\Spanner\TimestampBound\MaxStaleness;
Expand Down Expand Up @@ -619,17 +620,15 @@ public function test_getTablePrefix(): void

public function test_getQueryGrammar(): void
{
config()->set('database.connections.main.prefix', 'test_');
$conn = $this->getConnection('main');
$this->assertSame('test_', $conn->getQueryGrammar()->getTablePrefix());
$this->assertInstanceOf(\Colopl\Spanner\Query\Grammar::class, $conn->getQueryGrammar());
}

public function test_getSchemaGrammar(): void
{
config()->set('database.connections.main.prefix', 'test_');
$conn = $this->getConnection('main');
$conn->useDefaultSchemaGrammar();
$this->assertSame('test_', $conn->getSchemaGrammar()->getTablePrefix());
$this->assertInstanceOf(Grammar::class, $conn->getSchemaGrammar());
}

public function test_binding_mutable_carbon_doesnt_change_timezone(): void
Expand Down
5 changes: 1 addition & 4 deletions tests/Eloquent/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Tag extends Model
protected $keyType = 'string';
public $timestamps = false;

public function items()
public function items(): BelongsToMany
{
return $this->belongsToMany(Item::class, 'ItemTag', 'itemId', 'tagId');
}
Expand Down Expand Up @@ -244,7 +244,6 @@ protected function createTestTest(string $stringTestValue): Test
public function testCRUD(): void
{
// create
/** @var User $user */
$user = $this->createTestUser();
$user->save();
$this->assertDatabaseHas($user->getTable(), ['userId' => $user->userId, 'name' => $user->name]);
Expand All @@ -262,13 +261,11 @@ public function testCRUD(): void

public function testBelongsTo(): void
{
/** @var User $user */
$user = $this->createTestUser();
$user->save();

$itemId = $this->generateUuid();
$itemCount = 99;
/** @var UserItem $userItem */
$userItem = $this->createTestUserItem($user->userId, $itemId, $itemCount);
$userItem->save();

Expand Down
26 changes: 12 additions & 14 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
use BadMethodCallException;
use Colopl\Spanner\Query\Builder;
use Colopl\Spanner\Schema\Blueprint;
use Colopl\Spanner\Schema\Grammar;
use Colopl\Spanner\Schema\TokenizerFunction;
use Colopl\Spanner\Tests\Eloquent\IdentityTest;
use Colopl\Spanner\Tests\TestCase;
use Colopl\Spanner\TimestampBound\ExactStaleness;
use Colopl\Spanner\TimestampBound\StrongRead;
Expand Down Expand Up @@ -1137,15 +1135,15 @@ public function test_search(): void
{
$tableName = $this->generateTableName('FTS_Search');
$conn = $this->getDefaultConnection();
$grammar = new Grammar();
$blueprint = new Blueprint($tableName, function (Blueprint $table) {
$conn->useDefaultSchemaGrammar();
$blueprint = new Blueprint($conn, $tableName, function (Blueprint $table) {
$table->create();
$table->uuid('id')->primary();
$table->string('name');
$table->tokenList('nameTokens', TokenizerFunction::FullText, 'name');
$table->fullText('nameTokens');
});
$blueprint->build($conn, $grammar);
$blueprint->build();

$conn->table($tableName)->insert([
['id' => $this->generateUuid(), 'name' => 'test1'],
Expand All @@ -1161,15 +1159,15 @@ public function test_search_with_options(): void
{
$tableName = $this->generateTableName('FTS_Search');
$conn = $this->getDefaultConnection();
$grammar = new Grammar();
$blueprint = new Blueprint($tableName, function (Blueprint $table) {
$conn->useDefaultSchemaGrammar();
$blueprint = new Blueprint($conn, $tableName, function (Blueprint $table) {
$table->create();
$table->uuid('id')->primary();
$table->string('name');
$table->tokenList('nameTokens', TokenizerFunction::FullText, 'name');
$table->fullText('nameTokens');
});
$blueprint->build($conn, $grammar);
$blueprint->build();

$conn->table($tableName)->insert([
['id' => $this->generateUuid(), 'name' => 'test1'],
Expand All @@ -1185,15 +1183,15 @@ public function test_searchNgrams(): void
{
$tableName = $this->generateTableName('FTS_Search_Ngrams');
$conn = $this->getDefaultConnection();
$grammar = new Grammar();
$blueprint = new Blueprint($tableName, function (Blueprint $table) {
$conn->useDefaultSchemaGrammar();
$blueprint = new Blueprint($conn, $tableName, function (Blueprint $table) {
$table->create();
$table->uuid('id')->primary();
$table->string('name');
$table->tokenList('nameTokens', TokenizerFunction::Ngrams, 'name');
$table->fullText('nameTokens');
});
$blueprint->build($conn, $grammar);
$blueprint->build();

$conn->table($tableName)->insert([
['id' => $this->generateUuid(), 'name' => 'test1'],
Expand All @@ -1209,15 +1207,15 @@ public function test_searchSubstring(): void
{
$tableName = $this->generateTableName('FTS_Search_Substring');
$conn = $this->getDefaultConnection();
$grammar = new Grammar();
$blueprint = new Blueprint($tableName, function (Blueprint $table) {
$conn->useDefaultSchemaGrammar();
$blueprint = new Blueprint($conn, $tableName, function (Blueprint $table) {
$table->create();
$table->uuid('id')->primary();
$table->string('name');
$table->tokenList('nameTokens', TokenizerFunction::Substring, 'name');
$table->fullText('nameTokens');
});
$blueprint->build($conn, $grammar);
$blueprint->build();

$conn->table($tableName)->insert([
['id' => $this->generateUuid(), 'name' => 'test1'],
Expand Down
Loading