Skip to content

Commit ba05b63

Browse files
authored
Add support for Cockroach Serverless Cluster identifier (#11)
* chore: database connector with cluster options support * docs: add sample snippet in README for the cluster option * Fix PHP styling * fix: laravel 8 dsn quotes * chore: PR code review fixes * Fix PHP styling Co-authored-by: tryvin <[email protected]>
1 parent fbb3380 commit ba05b63

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ search meaning the feature cannot be used when using this driver.
7171
At current if you try to create a Fulltext index using the Schema builder or try to use the `whereFulltext`
7272
method of the Query builder a `YlsIdeas\CockroachDb\Exceptions\FeatureNotSupportedException` exception will be thrown.
7373

74+
### Serverless Support
75+
Cockroach Serverless requires you to add an `options` parameter to the connection string.
76+
Laravel doesn't provide this out of the box, so, it's being implemented as an extra `cluster` parameter in the database config. Just pass the cluster identification from CockroachDB Serverless.
77+
78+
Sample config snippet:
79+
80+
```php
81+
'crdb' => [
82+
'driver' => 'crdb',
83+
'url' => env('DATABASE_URL'),
84+
'host' => env('DB_HOST', '127.0.0.1'),
85+
'port' => env('DB_PORT', '26257'),
86+
'database' => env('DB_DATABASE', 'forge'),
87+
'username' => env('DB_USERNAME', 'forge'),
88+
'password' => env('DB_PASSWORD', ''),
89+
'charset' => 'utf8',
90+
'prefix' => '',
91+
'prefix_indexes' => true,
92+
'schema' => 'public',
93+
'sslmode' => 'prefer',
94+
'cluster' => env('COCKROACHDB_CLUSTER', ''),
95+
]
96+
```
97+
7498
## Testing
7599

76100
The tests try to closely follow the same functionality of the grammar provided by Laravel

src/CockroachDbConnector.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,22 @@
77

88
class CockroachDbConnector extends PostgresConnector implements ConnectorInterface
99
{
10+
/**
11+
* Usually the normal PostgresConnector would suffice for Cockroach,
12+
* but Cockroach Serverless Clusters need an extra parameter `options`.
13+
*/
14+
protected function getDsn(array $config)
15+
{
16+
return $this->addClusterOptions(parent::getDsn($config), $config);
17+
}
18+
19+
protected function addClusterOptions(string $dsn, array $config)
20+
{
21+
if (isset($config['cluster']) && ! empty($config['cluster'])) {
22+
$clusterNameEscaped = addslashes($config['cluster']);
23+
$dsn .= ";options='--cluster={$clusterNameEscaped}'";
24+
}
25+
26+
return $dsn;
27+
}
1028
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace YlsIdeas\CockroachDb\Tests\Database;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use YlsIdeas\CockroachDb\CockroachDbConnector;
7+
8+
class DatabaseCockroachDbConnectorTest extends TestCase
9+
{
10+
public function test_dsn_params_with_cluster()
11+
{
12+
$connector = $this->getConnector();
13+
14+
$dsnConfig = $connector->exposeGetDsnMethod(
15+
[
16+
'host' => 'localhost',
17+
'database' => 'defaultdb',
18+
'port' => '23456',
19+
'cluster' => 'cluster-1234',
20+
],
21+
);
22+
23+
$this->assertStringContainsString("options='--cluster=cluster-1234'", $dsnConfig);
24+
}
25+
26+
public function test_dsn_params_without_cluster()
27+
{
28+
$connector = $this->getConnector();
29+
30+
$dsnConfig = $connector->exposeGetDsnMethod(
31+
[
32+
'host' => 'localhost',
33+
'database' => 'defaultdb',
34+
'port' => '23456',
35+
'cluster' => '',
36+
],
37+
);
38+
39+
$this->assertStringNotContainsString("options=", $dsnConfig);
40+
}
41+
42+
protected function getConnector()
43+
{
44+
return new class () extends CockroachDbConnector {
45+
public function exposeGetDsnMethod(array $config)
46+
{
47+
return $this->getDsn($config);
48+
}
49+
};
50+
}
51+
}

0 commit comments

Comments
 (0)