Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support table prefixing #172

Merged
merged 4 commits into from
Feb 16, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Added
- Allow pretending for DDL statements (#170)
- Allow `spanner_emulator.disable_query_null_filtered_index_check` to be set (#180)
- Allow default max transaction attempts to be changed (#179)
- Table prefixing support (#172)

Changed
- `Query\Builder::lock()` no longer throw an error and will be ignored instead (#156)
Expand Down
10 changes: 8 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ public function disconnect()
*/
protected function getDefaultQueryGrammar(): QueryGrammar
{
return (new QueryGrammar())->setConnection($this);
$grammar = new QueryGrammar();
$grammar->setConnection($this);
$this->withTablePrefix($grammar);
return $grammar;
}

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

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,4 +555,26 @@ public function test_escape_nested_array(): void
$conn = $this->getDefaultConnection();
self::assertSame('[]', $conn->escape([[]]));
}

public function test_getTablePrefix(): void
{
config()->set('database.connections.main.prefix', 'test_');
$tablePrefix = $this->getConnection('main')->getTablePrefix();
self::assertSame('test_', $tablePrefix);
}

public function test_getQueryGrammar(): void
{
config()->set('database.connections.main.prefix', 'test_');
$conn = $this->getConnection('main');
self::assertSame('test_', $conn->getQueryGrammar()->getTablePrefix());
}

public function test_getSchemaGrammar(): void
{
config()->set('database.connections.main.prefix', 'test_');
$conn = $this->getConnection('main');
$conn->useDefaultSchemaGrammar();
self::assertSame('test_', $conn->getSchemaGrammar()->getTablePrefix());
}
}
7 changes: 7 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,13 @@ public function test_sharedLock(): void
$this->assertSame('select * from `User`', $sql);
}

public function test_prefixing(): void
{
config()->set('database.connections.main.prefix', 'test_');
$conn = $this->getConnection('main');
self::assertSame('select * from `test_User`', $conn->table('User')->toRawSql());
}

public function test_toRawSql(): void
{
$table = 'RawSqlTest';
Expand Down
15 changes: 15 additions & 0 deletions tests/Schema/BuilderTestLast.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ public function testSchemaCreate(): void
$this->assertCount(0, collect(['id', 'name', 'age', 'created_at'])->diff($columnNames));
}

public function test_create_with_prefix(): void
{
config()->set('database.connections.main.prefix', 'test_');
$conn = $this->getConnection('main');

$sb = $conn->getSchemaBuilder();
$table = $this->generateTableName(class_basename(__CLASS__));

$sb->create($table, function (Blueprint $table) {
$table->uuid('id')->primary();
});
$tables = array_map(static fn (array $row) => $row['name'], $sb->getTables());
self::assertContains('test_' . $table, $tables);
}

public function testSchemaDrop(): void
{
$conn = $this->getDefaultConnection();
Expand Down
Loading