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

[Bug]: URL is not defined! #27

Open
elymatos opened this issue Jan 22, 2025 · 5 comments
Open

[Bug]: URL is not defined! #27

elymatos opened this issue Jan 22, 2025 · 5 comments
Labels
bug Something isn't working question Further information is requested

Comments

@elymatos
Copy link

What happened?

Hi!
I'm trying to access a local database. Following the instructions, I've ignores the other variables at .env and I just defined
DB_DATABASE=database.chinook

How to reproduce the bug

I create a minimal script
print_r(DB::table('albums')->get());

But I'm receiving:

 Exception 

  URL is not defined!

  at vendor/tursodatabase/turso-driver-laravel/src/Database/LibSQLDatabase.php:35
     31▕         $this->setConnectionMode($config);
     32▕ 
     33▕         $this->in_transaction = false;
     34▕ 
  ➜  35▕         $this->db = new LibSQL($config);
     36▕     }
     37▕ 
     38▕     private function createConfig(array $config): array
     39▕     {

      +8 vendor frames 

As far I understood URL is necessary just to remote connection.
What is necessary to configure for local connection?
Tks,
Ely

Package Version

last (from composer)

PHP Version

8.3

Laravel Version

11.0

Which operating systems does with happen with?

Linux

Notes

No response

@elymatos elymatos added the bug Something isn't working label Jan 22, 2025
@elymatos
Copy link
Author

The problem also happens when I set DB_DATABASE to configuration for other database (e.g. mariadb).
Ely

@mentamarindos
Copy link

this is not libsql related, you just doing it wrong, see: https://laravel.com/docs/11.x/database#sqlite-configuration

use:
DB_DATABASE=/absolute/path/to/database.sqlite

*make sure the database.sqlite file exists. by the way I encourage you to search on Google or use AI to address these kinds of things before submitting a GitHub issue :)

@darkterminal
Copy link
Collaborator

@elymatos you can see the TestCase.php how to configure if the readme is not enough.

@elymatos
Copy link
Author

Thanks for responses. I'm sorry if the questions seem so fool, but I really want to understand what is going on (I tried to open a issue as "ask a question" and not a "bug", but the page is not working).
The fact is: after installing turso-driver-laravel even if I don´t have any sqlite databases configured, it seems to alter the "normal" behavior of DatabaseManager from Illuminate, in especial the conf "db.factory".

DatabaseManager:

    protected function registerConnectionServices()
    {
        // The connection factory is used to create the actual connection instances on
        // the database. We will inject the factory into the manager so that it may
        // make the connections while they are actually needed and not of before.
        $this->app->singleton('db.factory', function ($app) {
            return new ConnectionFactory($app);
        });

LibSQLDriverServiceProvider:


    public function register(): void
    {
        parent::register();
        $this->app->singleton('db.factory', function ($app) {
            return new LibSQLConnectionFactory($app);
        });

So, following the trace it is possible to see that the driver for my default connection (mariadb) is changed to (sqlite). So LibSQLDatabase is constructed with wrong config (all the variables are from my mariadb connection, but the driver is sqlite). As the config has no "URL" configured, I get the error above.

I just need to know if this driver only allows to work with SQLite or if it is possible to work with multiple databases (with different drivers). In the last case, what is necessary to change (if something) to make this work.
Thanks for attention,
Ely

@darkterminal
Copy link
Collaborator

Thank you @elymatos for your explanations, I am try to test following your traces to make sure and here is the test:

<?php

it('uses the default connection factory for non-libsql drivers', function () {
    $dbManager = $this->app->make(Illuminate\Database\DatabaseManager::class);

    // Set the default connection to mariadb
    config(['database.default' => 'mariadb']);

    // Add mariadb connection configuration
    config([
        'database.connections.mariadb' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
        ]
    ]);

    $connection = $dbManager->connection('mariadb');

    expect($connection->getConfig('driver'))->toBe('mysql');
    expect($connection)->toBeInstanceOf(\Illuminate\Database\MySqlConnection::class);
})->group('ConnectDifferentDatabaseDriver', 'FeatureTest');

it('uses the libsql connection factory for libsql driver', function () {
    $dbManager = $this->app->make(Illuminate\Database\DatabaseManager::class);

    // Set the default connection to libsql
    config(['database.default' => 'libsql']);

    // Add libsql connection configuration
    config([
        'database.connections.libsql' => [
            'driver' => 'libsql',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'url' => env('DB_SYNC_URL', ''),
            'authToken' => env('DB_AUTH_TOKEN', ''),
            'syncInterval' => env('DB_SYNC_INTERVAL', 5),
            'read_your_writes' => env('DB_READ_YOUR_WRITES', true),
            'encryptionKey' => env('DB_ENCRYPTION_KEY', ''),
        ]
    ]);

    $connection = $dbManager->connection('libsql');

    expect($connection->getConfig('driver'))->toBe('libsql');
    expect($connection)->toBeInstanceOf(Turso\Driver\Laravel\Database\LibSQLConnection::class);
})->group('ConnectDifferentDatabaseDriver', 'FeatureTest');

The LibSQL driver's service provider replaces the db.factory but should correctly delegate non-LibSQL drivers to Laravel's default factory, as verified by tests. Ensure your configuration doesn't mistakenly set the driver to libsql for non-LibSQL connections.

When accessing a non-default (if the default is mysql/mariadb connection, specify it explicitly:

DB::connection('mariadb')->table('albums')->get();

or when the default connection is libsql, then use:

DB::connection('libsql')->table('albums')->get();

Please correct me if I am wrong.

@darkterminal darkterminal added the question Further information is requested label Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants