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 16 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
61 changes: 11 additions & 50 deletions src/Database/LibSQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,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 All @@ -30,33 +31,6 @@ public function sync(): void
$this->db->sync();
}

public function getDb(): LibSQL
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@darkterminal As we were talking #5 (comment)

I don't think it's good idea to have this methods, because they are ambiguous.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be better if this were reserved for internal derivatives only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can already access $this->db, and even tho, since it's actually public it's not reserved for internal usage only, maybe it would be better to set it to protected.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Make sense... the property are need to be protected.

{
return $this->db->getDb();
}

public function getConnectionMode(): string
{
return $this->db->getConnectionMode();
}

public function statement($query, $bindings = []): bool
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why are you removing actual implementation?

    /**
     * Execute an SQL statement and return the boolean result.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @return bool
     */
    public function statement($query, $bindings = [])
    {
        return $this->run($query, $bindings, function ($query, $bindings) {
            if ($this->pretending()) {
                return true;
            }

            $statement = $this->getPdo()->prepare($query);

            $this->bindValues($statement, $this->prepareBindings($bindings));

            $this->recordsHaveBeenModified();

            return $statement->execute();
        });
    }

This will totally mess up things further, especially if you will work with models inserts/updates.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I had to lie with this to the Connection class:

public function getDb(): LibSQL
{
    return $this->db->getDb();
}

public function getConnectionMode(): string
{
    return $this->db->getConnectionMode();
}

public function statement($query, $bindings = []): bool
{
    $res = $this->select($query, $bindings);

    return !empty($res);
}

public function getPdo(): LibSQLDatabase
{
    return $this->db;
}

public function getReadPdo(): LibSQLDatabase
{
    return $this->db;
}

public function select($query, $bindings = [], $useReadPdo = true)
{
    $result = (array) parent::select($query, $bindings, $useReadPdo);

    $resultArray = array_map(function ($item) {
        return (array) $item;
    }, $result);

    return $resultArray;
}

Because when I used this method:

    /**
     * Execute an SQL statement and return the boolean result.
     *
     * @param  string  $query
     * @param  array  $bindings
     * @return bool
     */
    public function statement($query, $bindings = [])
    {
        return $this->run($query, $bindings, function ($query, $bindings) {
            if ($this->pretending()) {
                return true;
            }

            $statement = $this->getPdo()->prepare($query);

            $this->bindValues($statement, $this->prepareBindings($bindings));

            $this->recordsHaveBeenModified();

            return $statement->execute();
        });
    }

Database connections will use the PDO engine to perform binding, whereas libSQL itself does not have natural binding parameters like PDO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Database connections will use the PDO engine to perform binding, whereas libSQL itself does not have natural binding parameters like PDO.

Yes, that's right, and you are rewriting it inside your Statement class, method execute.

On my dev branch I've changed structure a little, to make concerns for statement/connection classes more clear.
main...ineersa:turso-driver-laravel:dev-branch

Primary I done that to address issue with lastInsertId which was not working and was implemented in the wrong place, and it's actually crucial for Eloquent models.

Also I have some strange problems with bindings on INSERT currently, where TEXT column data is truncated.

But if you will try and look into my dev branch, you can see that everything is working via base methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea problem is more fundamental, and lies in internal not compatibility between this implementation and PDO classes.
#14 this one has roots from there also, I will try to look at it on the next week

Copy link
Collaborator

Choose a reason for hiding this comment

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

So, now it's clearer why I made a lot of unconventional things with this package.

And for reason about the connection string need to follow the Turso conventional, because the binary itself is have a logic to identifying the connection mode like this:

pub fn get_mode(
    url: Option<String>,
    auth_token: Option<String>,
    sync_url: Option<String>,
) -> String {
    match (url, auth_token, sync_url) {
        (Some(ref url), Some(ref auth_token), Some(ref sync_url))
            if (url.starts_with("file:") || url.ends_with(".db") || url.starts_with("libsql:"))
                && !auth_token.is_empty()
                && (sync_url.starts_with("libsql://")
                    || sync_url.starts_with("http://")
                    || sync_url.starts_with("https://")) =>
        {
            "remote_replica".to_string()
        }
        (Some(ref url), Some(ref auth_token), _)
            if !auth_token.is_empty() && url.starts_with("libsql://")
                || url.starts_with("http://")
                || url.starts_with("https://") =>
        {
            "remote".to_string()
        }
        (Some(ref url), _, _)
            if url.starts_with("file:")
                || url.ends_with(".db")
                || url.starts_with("libsql:")
                || url.contains(":memory:") =>
        {
            "local".to_string()
        }
        _ => "Mode is not available!".to_string(),
    }
}

Check if is Remote Replica Connection?

  • url starts with file:, ends with .db, or starts with libsql:.
  • auth_token is not empty.
  • sync_url starts with libsql://, http://, or https://.

Check if is Remote Connection?

  • auth_token is not empty.
  • url starts with libsql://, http://, or https://.

Check if is Local Connection?

  • url starts with file:, ends with .db, starts with libsql:, or contains :memory:.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yea problem is more fundamental, and lies in internal not compatibility between this implementation and PDO classes.

Please remember, the real challenge is to make it compatible without using PDO. So far what I've done is freestyle which may look very strange.

{
$this->select($query, $bindings);

return $this->isRunningMigrations();
}

public function getPdo(): LibSQLDatabase
{
return $this->db;
}

public function getReadPdo(): LibSQLDatabase
{
return $this->db;
}

public function select($query, $bindings = [], $useReadPdo = true)
{
$result = (array) parent::select($query, $bindings, $useReadPdo);
Expand All @@ -68,6 +42,13 @@ public function select($query, $bindings = [], $useReadPdo = true)
return $resultArray;
}

protected function getDefaultQueryGrammar()
darkterminal marked this conversation as resolved.
Show resolved Hide resolved
{
($grammar = new LibSQLQueryGrammar())->setConnection($this);

return $grammar;
}

/**
* Get the default schema grammar instance.
*
Expand All @@ -88,16 +69,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 Expand Up @@ -145,14 +116,4 @@ public function quote(string $value): string
{
return $this->escapeString($value);
}

protected function isRunningMigrations()
darkterminal marked this conversation as resolved.
Show resolved Hide resolved
{
$commands = [
'tenants:migrate',
'tenants:rollback',
];

return App::runningInConsole() && in_array($_SERVER['argv'][1], $commands);
}
}
27 changes: 0 additions & 27 deletions src/Database/LibSQLConnectionFactory.php

This file was deleted.

Loading