Skip to content

Commit 5fa9267

Browse files
authored
[9.x] Use write connection on Schema::getColumnListing() and Schema::hasTable() for MySQL and PostgreSQL (#44946)
* [9.x] Use write connection on Schema::hasColumn() and Schema::hasTable() * add tests * fix more tests * clean up unused imports Co-authored-by: 尾山貴康 <[email protected]>
1 parent 213e07e commit 5fa9267

File tree

5 files changed

+119
-15
lines changed

5 files changed

+119
-15
lines changed

src/Illuminate/Database/Schema/MySqlBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function hasTable($table)
4040
{
4141
$table = $this->connection->getTablePrefix().$table;
4242

43-
return count($this->connection->select(
43+
return count($this->connection->selectFromWriteConnection(
4444
$this->grammar->compileTableExists(), [$this->connection->getDatabaseName(), $table]
4545
)) > 0;
4646
}
@@ -55,7 +55,7 @@ public function getColumnListing($table)
5555
{
5656
$table = $this->connection->getTablePrefix().$table;
5757

58-
$results = $this->connection->select(
58+
$results = $this->connection->selectFromWriteConnection(
5959
$this->grammar->compileColumnListing(), [$this->connection->getDatabaseName(), $table]
6060
);
6161

src/Illuminate/Database/Schema/PostgresBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function hasTable($table)
4848

4949
$table = $this->connection->getTablePrefix().$table;
5050

51-
return count($this->connection->select(
51+
return count($this->connection->selectFromWriteConnection(
5252
$this->grammar->compileTableExists(), [$database, $schema, $table]
5353
)) > 0;
5454
}
@@ -187,7 +187,7 @@ public function getColumnListing($table)
187187

188188
$table = $this->connection->getTablePrefix().$table;
189189

190-
$results = $this->connection->select(
190+
$results = $this->connection->selectFromWriteConnection(
191191
$this->grammar->compileColumnListing(), [$database, $schema, $table]
192192
);
193193

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use Illuminate\Database\Connection;
6+
use Illuminate\Database\Query\Processors\MySqlProcessor;
7+
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
8+
use Illuminate\Database\Schema\MySqlBuilder;
9+
use Mockery as m;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class DatabaseMySQLSchemaBuilderTest extends TestCase
13+
{
14+
protected function tearDown(): void
15+
{
16+
m::close();
17+
}
18+
19+
public function testHasTable()
20+
{
21+
$connection = m::mock(Connection::class);
22+
$grammar = m::mock(MySqlGrammar::class);
23+
$connection->shouldReceive('getDatabaseName')->andReturn('db');
24+
$connection->shouldReceive('getSchemaGrammar')->andReturn($grammar);
25+
$builder = new MySqlBuilder($connection);
26+
$grammar->shouldReceive('compileTableExists')->once()->andReturn('sql');
27+
$connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_');
28+
$connection->shouldReceive('selectFromWriteConnection')->once()->with('sql', ['db', 'prefix_table'])->andReturn(['prefix_table']);
29+
30+
$this->assertTrue($builder->hasTable('table'));
31+
}
32+
33+
public function testGetColumnListing()
34+
{
35+
$connection = m::mock(Connection::class);
36+
$grammar = m::mock(MySqlGrammar::class);
37+
$processor = m::mock(MySqlProcessor::class);
38+
$connection->shouldReceive('getDatabaseName')->andReturn('db');
39+
$connection->shouldReceive('getSchemaGrammar')->andReturn($grammar);
40+
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
41+
$grammar->shouldReceive('compileColumnListing')->once()->andReturn('sql');
42+
$processor->shouldReceive('processColumnListing')->once()->andReturn(['column']);
43+
$builder = new MySqlBuilder($connection);
44+
$connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_');
45+
$connection->shouldReceive('selectFromWriteConnection')->once()->with('sql', ['db', 'prefix_table'])->andReturn(['column']);
46+
47+
$this->assertEquals(['column'], $builder->getColumnListing('table'));
48+
}
49+
}

tests/Database/DatabasePostgresBuilderTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testHasTableWhenSchemaUnqualifiedAndSearchPathMissing()
5454
$grammar = m::mock(PostgresGrammar::class);
5555
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
5656
$grammar->shouldReceive('compileTableExists')->andReturn("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'");
57-
$connection->shouldReceive('select')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'public', 'foo'])->andReturn(['countable_result']);
57+
$connection->shouldReceive('selectFromWriteConnection')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'public', 'foo'])->andReturn(['countable_result']);
5858
$connection->shouldReceive('getTablePrefix');
5959
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
6060
$builder = $this->getBuilder($connection);
@@ -69,7 +69,7 @@ public function testHasTableWhenSchemaUnqualifiedAndSearchPathFilled()
6969
$grammar = m::mock(PostgresGrammar::class);
7070
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
7171
$grammar->shouldReceive('compileTableExists')->andReturn("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'");
72-
$connection->shouldReceive('select')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
72+
$connection->shouldReceive('selectFromWriteConnection')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
7373
$connection->shouldReceive('getTablePrefix');
7474
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
7575
$builder = $this->getBuilder($connection);
@@ -85,7 +85,7 @@ public function testHasTableWhenSchemaUnqualifiedAndSearchPathFallbackFilled()
8585
$grammar = m::mock(PostgresGrammar::class);
8686
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
8787
$grammar->shouldReceive('compileTableExists')->andReturn("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'");
88-
$connection->shouldReceive('select')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
88+
$connection->shouldReceive('selectFromWriteConnection')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
8989
$connection->shouldReceive('getTablePrefix');
9090
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
9191
$builder = $this->getBuilder($connection);
@@ -101,7 +101,7 @@ public function testHasTableWhenSchemaUnqualifiedAndSearchPathIsUserVariable()
101101
$grammar = m::mock(PostgresGrammar::class);
102102
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
103103
$grammar->shouldReceive('compileTableExists')->andReturn("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'");
104-
$connection->shouldReceive('select')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'foouser', 'foo'])->andReturn(['countable_result']);
104+
$connection->shouldReceive('selectFromWriteConnection')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'foouser', 'foo'])->andReturn(['countable_result']);
105105
$connection->shouldReceive('getTablePrefix');
106106
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
107107
$builder = $this->getBuilder($connection);
@@ -116,7 +116,7 @@ public function testHasTableWhenSchemaQualifiedAndSearchPathMismatches()
116116
$grammar = m::mock(PostgresGrammar::class);
117117
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
118118
$grammar->shouldReceive('compileTableExists')->andReturn("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'");
119-
$connection->shouldReceive('select')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
119+
$connection->shouldReceive('selectFromWriteConnection')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
120120
$connection->shouldReceive('getTablePrefix');
121121
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
122122
$builder = $this->getBuilder($connection);
@@ -131,7 +131,7 @@ public function testHasTableWhenDatabaseAndSchemaQualifiedAndSearchPathMismatche
131131
$grammar = m::mock(PostgresGrammar::class);
132132
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
133133
$grammar->shouldReceive('compileTableExists')->andReturn("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'");
134-
$connection->shouldReceive('select')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['mydatabase', 'myapp', 'foo'])->andReturn(['countable_result']);
134+
$connection->shouldReceive('selectFromWriteConnection')->with("select * from information_schema.tables where table_catalog = ? and table_schema = ? and table_name = ? and table_type = 'BASE TABLE'", ['mydatabase', 'myapp', 'foo'])->andReturn(['countable_result']);
135135
$connection->shouldReceive('getTablePrefix');
136136
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
137137
$builder = $this->getBuilder($connection);
@@ -147,7 +147,7 @@ public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathMissing()
147147
$grammar = m::mock(PostgresGrammar::class);
148148
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
149149
$grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?');
150-
$connection->shouldReceive('select')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'public', 'foo'])->andReturn(['countable_result']);
150+
$connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'public', 'foo'])->andReturn(['countable_result']);
151151
$connection->shouldReceive('getTablePrefix');
152152
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
153153
$processor = m::mock(PostgresProcessor::class);
@@ -165,7 +165,7 @@ public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathFilled()
165165
$grammar = m::mock(PostgresGrammar::class);
166166
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
167167
$grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?');
168-
$connection->shouldReceive('select')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
168+
$connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
169169
$connection->shouldReceive('getTablePrefix');
170170
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
171171
$processor = m::mock(PostgresProcessor::class);
@@ -184,7 +184,7 @@ public function testGetColumnListingWhenSchemaUnqualifiedAndSearchPathIsUserVari
184184
$grammar = m::mock(PostgresGrammar::class);
185185
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
186186
$grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?');
187-
$connection->shouldReceive('select')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'foouser', 'foo'])->andReturn(['countable_result']);
187+
$connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'foouser', 'foo'])->andReturn(['countable_result']);
188188
$connection->shouldReceive('getTablePrefix');
189189
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
190190
$processor = m::mock(PostgresProcessor::class);
@@ -202,7 +202,7 @@ public function testGetColumnListingWhenSchemaQualifiedAndSearchPathMismatches()
202202
$grammar = m::mock(PostgresGrammar::class);
203203
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
204204
$grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?');
205-
$connection->shouldReceive('select')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
205+
$connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['laravel', 'myapp', 'foo'])->andReturn(['countable_result']);
206206
$connection->shouldReceive('getTablePrefix');
207207
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
208208
$processor = m::mock(PostgresProcessor::class);
@@ -220,7 +220,7 @@ public function testGetColumnWhenDatabaseAndSchemaQualifiedAndSearchPathMismatch
220220
$grammar = m::mock(PostgresGrammar::class);
221221
$connection->shouldReceive('getSchemaGrammar')->once()->andReturn($grammar);
222222
$grammar->shouldReceive('compileColumnListing')->andReturn('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?');
223-
$connection->shouldReceive('select')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['mydatabase', 'myapp', 'foo'])->andReturn(['countable_result']);
223+
$connection->shouldReceive('selectFromWriteConnection')->with('select column_name from information_schema.columns where table_catalog = ? and table_schema = ? and table_name = ?', ['mydatabase', 'myapp', 'foo'])->andReturn(['countable_result']);
224224
$connection->shouldReceive('getTablePrefix');
225225
$connection->shouldReceive('getConfig')->with('database')->andReturn('laravel');
226226
$processor = m::mock(PostgresProcessor::class);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use Illuminate\Database\Connection;
6+
use Illuminate\Database\Query\Processors\PostgresProcessor;
7+
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
8+
use Illuminate\Database\Schema\PostgresBuilder;
9+
use Mockery as m;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class DatabasePostgresSchemaBuilderTest extends TestCase
13+
{
14+
protected function tearDown(): void
15+
{
16+
m::close();
17+
}
18+
19+
public function testHasTable()
20+
{
21+
$connection = m::mock(Connection::class);
22+
$grammar = m::mock(PostgresGrammar::class);
23+
$connection->shouldReceive('getDatabaseName')->andReturn('db');
24+
$connection->shouldReceive('getSchemaGrammar')->andReturn($grammar);
25+
$connection->shouldReceive('getConfig')->with('database')->andReturn('db');
26+
$connection->shouldReceive('getConfig')->with('schema')->andReturn('schema');
27+
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('public');
28+
$builder = new PostgresBuilder($connection);
29+
$grammar->shouldReceive('compileTableExists')->once()->andReturn('sql');
30+
$connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_');
31+
$connection->shouldReceive('selectFromWriteConnection')->once()->with('sql', ['db', 'public', 'prefix_table'])->andReturn(['prefix_table']);
32+
33+
$this->assertTrue($builder->hasTable('table'));
34+
}
35+
36+
public function testGetColumnListing()
37+
{
38+
$connection = m::mock(Connection::class);
39+
$grammar = m::mock(PostgresGrammar::class);
40+
$processor = m::mock(PostgresProcessor::class);
41+
$connection->shouldReceive('getDatabaseName')->andReturn('db');
42+
$connection->shouldReceive('getSchemaGrammar')->andReturn($grammar);
43+
$connection->shouldReceive('getPostProcessor')->andReturn($processor);
44+
$connection->shouldReceive('getConfig')->with('database')->andReturn('db');
45+
$connection->shouldReceive('getConfig')->with('schema')->andReturn('schema');
46+
$connection->shouldReceive('getConfig')->with('search_path')->andReturn('public');
47+
$grammar->shouldReceive('compileColumnListing')->once()->andReturn('sql');
48+
$processor->shouldReceive('processColumnListing')->once()->andReturn(['column']);
49+
$builder = new PostgresBuilder($connection);
50+
$connection->shouldReceive('getTablePrefix')->once()->andReturn('prefix_');
51+
$connection->shouldReceive('selectFromWriteConnection')->once()->with('sql', ['db', 'public', 'prefix_table'])->andReturn(['column']);
52+
53+
$this->assertEquals(['column'], $builder->getColumnListing('table'));
54+
}
55+
}

0 commit comments

Comments
 (0)