-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
[Bug]: Incorrect connection creation #4
Comments
Figured out with Also in documentation link to database file is provided like this:
which is confusing, because inside https://github.com/tursodatabase/turso-driver-laravel/blob/main/src/Database/LibSQLDatabase.php#L27C1-L27C77 database file is created on path inside |
Thank you for this wonderful feedback! This is 🔥 me up... I will work for that, this is really helpful to me. |
No problem, I'm using it for my pet project as a tenant database, and it's working for now. I'll share some more observations or issues that I'll find, i can try to fix some of them, but I'm not Laravel expert, especailly in packages and it's container. BTW with dynamic connection setting and further unwinding Laravel magic https://github.com/tursodatabase/turso-driver-laravel/blob/main/src/LibSQLDriverServiceProvider.php#L46 you are rewriting using hardcoded config string, which will not work with any connection names except libsql. If you would change this behavior to use actual incoming config, then problem with driver file comes into play...
Which will further translate into driver file error in Connection factory. Then logical decision would be to use:
Then it's parsed into:
Which is awesome, but it gets corruppted here \Illuminate\Support\ConfigurationUrlParser::getDatabase
Which will remove "f" from "file".
Awesome now we get in parsed config:
Which will go into our extend resolving where we can safely remove hardcoded config. I will try to put this all together into PR, but again I'm not an expert in Laravel so can get some mistakes. |
If you're using tenant database with tenancy I am also have repository that implement tenancy for laravel called turso-laravel-tenancy. For now, it's only support local file connection as far I tested. If you use this driver with Tenancy for Laravel then you need to create custom |
Great I'll look into, but my use case is very simple and i don't need a lot of functionality, just per user local DB and a way to migrate them and so i don't use |
So, you plan to create a tenant db per user without using I am really thank you about this section: $this->app->singleton(LibSQLConnector::class, function ($app) {
$app['db.connector.libsql'] = (new LibSQLConnector())->connect(config('database.connections.libsql'));
}); |
This logic can resolved this issue and allow developer to set custom database directory.
$config = config('database.connections.libsql');
$url = str_replace('file:', '', $config['url']);
if ($this->checkPathOrFilename($config['url']) === 'filename') {
$config['url'] = 'file:' . database_path($url);
}
$libsql = $this->checkConnectionMode($config['url'], $config['syncUrl'], $config['authToken'], $config['remoteOnly']); If |
https://github.com/tursodatabase/turso-driver-laravel/pull/5/files Merged my thoughts into PR, added tests for all types of connections. There is some removals, like facades for example, since I don't think it's actually possible to use those here etc. Also phpstan now works) I've tested on my app: ...
\Turso\Driver\Laravel\LibSQLDriverServiceProvider::class,
...
\Config::set('database.connections.'.self::CONNECTION_NAME, [
'driver' => 'libsql',
'url' => 'libsql::file:'.database_path($this->getFilename($db->db_name)),
'encryptionKey' => $this->encrypter->decryptString($db->db_password),
'authToken' => '',
'syncUrl' => '',
'syncInterval' => 5,
'read_your_writes' => true,
'remoteOnly' => false,
'database' => null,
'prefix' => '',
]); Works ok, but maybe need more intensive testing. Feel free to jump into discussion inside PR, or maybe it will bring up some ideas to you. |
I believe I came across the same problem. As soon as I installed the package, things broke down. The following terminal output points out very well where it stems from.
My intention is to consume turso as an additional side database, with remote only read. So almost like the OP's use case. |
@flexchar Thank you for the report! Work in Progress... |
Reopen if need it. Thank you. |
What happened?
I've tried to use this driver in my application, basically I have MySQL database as main database and turso as per user local file database.
I've found several issues while trying to make this driver working:
To implement new connector you should do something like this in ServiceProvider:
This would be the correct way to add new connector, under the hood Laravel checks if connector defined in container and if it is, it will use it.
When you rewriting connection config, you are changing it for all connections, and in my case application was trying to create
libsql
connection withmysql
config, which was failing.Even if I use it with correct settings it will fail on parent call
Since there is no
libsql
in default factory:You shouldn't touch
ConnectionFactory
and correct way would be to extend DBManager with new driver (which is already implemented in https://github.com/tursodatabase/turso-driver-laravel/blob/main/src/LibSQLDriverServiceProvider.php#L45 and working)To resolve this I've registered default connection factory back:
And with correct connector creation this resolved issue.
So in examples you provide this:
which looks good, but idk why when it's parsed by DBManager driver is set to
file
and again it goes to ConnectionFactory which doesn't know anything about file driver.This is my config which worked for local file:
In this case driver correctly parsed to
libsql
.I can create PR to change this behaviors, but I don't know why was decided to implement own ConnectionFactory.
How to reproduce the bug
you might need to create database before that:
Package Version
latest
PHP Version
8.3.8
Laravel Version
11
Which operating systems does with happen with?
Linux
Notes
No response
The text was updated successfully, but these errors were encountered: