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

Fixes [Bug]: Incorrect connection creation #4 #13

Closed
wants to merge 17 commits into from
Closed
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
36 changes: 33 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
"illuminate/contracts": "^10.0||^11.0"
},
"require-dev": {
"larastan/larastan": "^2.9",
"laravel/pint": "^1.14",
"nunomaduro/collision": "^8.1.1||^7.10.0",
"larastan/larastan": "^2.9",
"orchestra/testbench": "^9.1",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
Expand All @@ -36,11 +37,40 @@
"autoload": {
"psr-4": {
"Turso\\Driver\\Laravel\\": "src/",
"Turso\\Driver\\Laravel\\Tests\\": "tests/",
"Turso\\Driver\\Laravel\\Database\\Factories\\": "database/factories/"
}
},
"scripts": {
"format": "vendor/bin/pint"
"format": "vendor/bin/pint",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/pint",
"@php vendor/bin/phpstan analyse"
],
"phpstan-baseline": [
"@php vendor/bin/phpstan -b"
],
"test": [
"@php vendor/bin/phpunit"
],
"test-feature": [
"@php vendor/bin/phpunit --testsuite=Feature"
],
"test-unit": [
"@php vendor/bin/phpunit --testsuite=Unit"
]
},
"config": {
"sort-packages": true,
Expand All @@ -58,4 +88,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
171 changes: 171 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
parameters:
ignoreErrors:
-
message: "#^Call to function is_null\\(\\) with Illuminate\\\\Database\\\\Schema\\\\Grammars\\\\Grammar will always evaluate to false\\.$#"
count: 2
path: src/Database/LibSQLConnection.php

-
message: "#^Method Turso\\\\Driver\\\\Laravel\\\\Database\\\\LibSQLConnection\\:\\:getDefaultSchemaGrammar\\(\\) should return Illuminate\\\\Database\\\\Schema\\\\Grammars\\\\Grammar but returns Illuminate\\\\Database\\\\Grammar\\.$#"
count: 1
path: src/Database/LibSQLConnection.php

-
message: "#^Return type \\(Turso\\\\Driver\\\\Laravel\\\\Database\\\\LibSQLDatabase\\) of method Turso\\\\Driver\\\\Laravel\\\\Database\\\\LibSQLConnector\\:\\:connect\\(\\) should be compatible with return type \\(PDO\\) of method Illuminate\\\\Database\\\\Connectors\\\\ConnectorInterface\\:\\:connect\\(\\)$#"
count: 1
path: src/Database/LibSQLConnector.php

-
message: "#^Constructor of class LibSQL has an unused parameter \\$config\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQL has an unused parameter \\$encryption_key\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQL has an unused parameter \\$flags\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQLResult has an unused parameter \\$config\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQLResult has an unused parameter \\$parameters\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQLResult has an unused parameter \\$sql\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQLStatement has an unused parameter \\$conn_id\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQLStatement has an unused parameter \\$sql\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQLTransaction has an unused parameter \\$conn_id\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Constructor of class LibSQLTransaction has an unused parameter \\$trx_mode\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:changes\\(\\) should return int but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:execute\\(\\) should return int but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:executeBatch\\(\\) should return bool but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:isAutocommit\\(\\) should return bool but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:prepare\\(\\) should return LibSQLStatement but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:query\\(\\) should return LibSQLResult but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:transaction\\(\\) should return LibSQLTransaction but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQL\\:\\:version\\(\\) should return string but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLResult\\:\\:columnName\\(\\) should return string but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLResult\\:\\:columnType\\(\\) should return string but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLResult\\:\\:fetchArray\\(\\) should return array but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLResult\\:\\:numColumns\\(\\) should return int but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLStatement\\:\\:columns\\(\\) should return array but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLStatement\\:\\:execute\\(\\) should return int but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLStatement\\:\\:parameterCount\\(\\) should return int but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLStatement\\:\\:parameterName\\(\\) should return string but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLStatement\\:\\:query\\(\\) should return LibSQLResult but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLTransaction\\:\\:changes\\(\\) should return int but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLTransaction\\:\\:execute\\(\\) should return int but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLTransaction\\:\\:isAutocommit\\(\\) should return bool but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php

-
message: "#^Method LibSQLTransaction\\:\\:query\\(\\) should return array but return statement is missing\\.$#"
count: 1
path: src/libsql_php_extension.stubs.php
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ parameters:
level: 5
paths:
- src
- config
- database
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
Expand Down
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
backupStaticProperties="false"
>
<testsuites>
<testsuite name="VendorName Test Suite">
<directory>tests</directory>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage>
Expand Down
17 changes: 4 additions & 13 deletions src/Database/LibSQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ class LibSQLConnection extends Connection

public function __construct(LibSQLDatabase $db, string $database = ':memory:', string $tablePrefix = '', array $config = [])
{
$libsqlDb = function () use ($db) {
$libsqlDatabase = function () use ($db) {
return $db;
};
parent::__construct($libsqlDb, $database, $tablePrefix, $config);

parent::__construct($libsqlDatabase, $database, $tablePrefix, $config);
$this->db = $db;
$this->setReadPdo($libsqlDatabase);

$this->schemaGrammar = $this->getDefaultSchemaGrammar();
}

Expand Down Expand Up @@ -94,16 +95,6 @@ public function useDefaultSchemaGrammar()
}
}

public function createReadPdo(array $config): ?LibSQLDatabase
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, createReadPdo() is used only in ConnectionFactory, which we are not using, and it's ConnectionFactory method, not connection.

{
$db = function () use ($config) {
return new LibSQLDatabase($config);
};
$this->setReadPdo($db);

return $db();
}

protected function escapeBinary(mixed $value): string
{
$hex = bin2hex($value);
Expand Down
27 changes: 0 additions & 27 deletions src/Database/LibSQLConnectionFactory.php

This file was deleted.

20 changes: 18 additions & 2 deletions src/Database/LibSQLConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

namespace Turso\Driver\Laravel\Database;

class LibSQLConnector
use Illuminate\Database\Connectors\ConnectorInterface;

class LibSQLConnector implements ConnectorInterface
{
/**
* Establish a database connection.
*/
public function connect(array $config): LibSQLDatabase
{
return new LibSQLDatabase($config);
$this->checkConfig($config);
$db = new LibSQLDatabase($config);
$db->init();

return $db;
}

protected function checkConfig(array $config): void
{
if (empty($config['driver']) || $config['driver'] !== 'libsql') {
throw new \InvalidArgumentException('Got driver - '.$config['driver'].', please check your URL and driver config');
}
if (empty($config['url']) && empty($config['database'])) {
throw new \InvalidArgumentException('URL and database not set, please check your configuration');
}
}
}
Loading