diff --git a/README.md b/README.md index 3e2d4bd..ba3cfd8 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,30 @@ search meaning the feature cannot be used when using this driver. At current if you try to create a Fulltext index using the Schema builder or try to use the `whereFulltext` method of the Query builder a `YlsIdeas\CockroachDb\Exceptions\FeatureNotSupportedException` exception will be thrown. +### Serverless Support +Cockroach Serverless requires you to add an `options` parameter to the connection string. +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. + +Sample config snippet: + +```php +'crdb' => [ + 'driver' => 'crdb', + 'url' => env('DATABASE_URL'), + 'host' => env('DB_HOST', '127.0.0.1'), + 'port' => env('DB_PORT', '26257'), + 'database' => env('DB_DATABASE', 'forge'), + 'username' => env('DB_USERNAME', 'forge'), + 'password' => env('DB_PASSWORD', ''), + 'charset' => 'utf8', + 'prefix' => '', + 'prefix_indexes' => true, + 'schema' => 'public', + 'sslmode' => 'prefer', + 'cluster' => env('COCKROACHDB_CLUSTER', ''), +] +``` + ## Testing The tests try to closely follow the same functionality of the grammar provided by Laravel diff --git a/src/CockroachDbConnector.php b/src/CockroachDbConnector.php index 9f11b4a..d99b8e8 100644 --- a/src/CockroachDbConnector.php +++ b/src/CockroachDbConnector.php @@ -7,4 +7,22 @@ class CockroachDbConnector extends PostgresConnector implements ConnectorInterface { + /** + * Usually the normal PostgresConnector would suffice for Cockroach, + * but Cockroach Serverless Clusters need an extra parameter `options`. + */ + protected function getDsn(array $config) + { + return $this->addClusterOptions(parent::getDsn($config), $config); + } + + protected function addClusterOptions(string $dsn, array $config) + { + if (isset($config['cluster']) && ! empty($config['cluster'])) { + $clusterNameEscaped = addslashes($config['cluster']); + $dsn .= ";options='--cluster={$clusterNameEscaped}'"; + } + + return $dsn; + } } diff --git a/tests/Database/DatabaseCockroachDbConnectorTest.php b/tests/Database/DatabaseCockroachDbConnectorTest.php new file mode 100644 index 0000000..8a3897c --- /dev/null +++ b/tests/Database/DatabaseCockroachDbConnectorTest.php @@ -0,0 +1,51 @@ +getConnector(); + + $dsnConfig = $connector->exposeGetDsnMethod( + [ + 'host' => 'localhost', + 'database' => 'defaultdb', + 'port' => '23456', + 'cluster' => 'cluster-1234', + ], + ); + + $this->assertStringContainsString("options='--cluster=cluster-1234'", $dsnConfig); + } + + public function test_dsn_params_without_cluster() + { + $connector = $this->getConnector(); + + $dsnConfig = $connector->exposeGetDsnMethod( + [ + 'host' => 'localhost', + 'database' => 'defaultdb', + 'port' => '23456', + 'cluster' => '', + ], + ); + + $this->assertStringNotContainsString("options=", $dsnConfig); + } + + protected function getConnector() + { + return new class () extends CockroachDbConnector { + public function exposeGetDsnMethod(array $config) + { + return $this->getDsn($config); + } + }; + } +}