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

Opening local files not working #31

Open
jiri-matejka opened this issue Dec 9, 2024 · 3 comments
Open

Opening local files not working #31

jiri-matejka opened this issue Dec 9, 2024 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers
Milestone

Comments

@jiri-matejka
Copy link

When I open file with absolute path, I get InvalidOperationException: Unsupported scheme: file.
I am using Linux.

This happens because your logic for opening files is problematic:

try
{
    var uri = new Uri(options.Url);
    
    switch (uri.Scheme)
    {
        case "http":
        case "https":
        case "ws":
        case "wss":
            _type = _options.ReplicaPath != null ? DatabaseType.EmbeddedReplica : DatabaseType.Remote;
            break;
        default:
            throw new InvalidOperationException($"Unsupported scheme: {uri.Scheme}");
    }
}
catch (UriFormatException)
{
    _type = DatabaseType.File;
}

Sending an absolute path like /home/user/database.sqlite will happily create a valid URI like file:///home/user/database.sqlite.

Please do not rely that URI is malformed for local files and provide a reliable way to open files from local disk.

Workaround is to pass a file with relative path like ../../database.sqlite which makes the new Uri() throw exception which will be catched and _type correctly set to DatabaseType.File.

@tvandinther tvandinther added the bug Something isn't working label Dec 12, 2024
@tvandinther tvandinther added this to the Version 1.0 milestone Dec 12, 2024
@tvandinther
Copy link
Owner

Thank you for reporting this. Would adding a case for the file scheme be a sufficient resolution for you? I also think it would be worth adding the URL parsing to the test suite to avoid this bug in the future.

@jiri-matejka
Copy link
Author

I missed that local files path is specified as file:/path/to/file in libSQL. Which actually works in your library, albeit it would maybe deserve a special handling in the code.

I found hard to understand that the options class has property Url where I am not actually passing a real URL. But it seems it is just a way how libSQL has it.

In case you would provide with an example with filename in your README.md and/or a documentation comment in the options class, I am fine with that :)

@tvandinther
Copy link
Owner

To ensure clarity I agree it would be a good idea to add a special case for handling the file: prefix explicitly for the connection URL rather than fully relying on the default fallback of using the filesystem. Added documentation on how to open different databases will also be a good addition.

Action points:

  • Add explicit case for connection URLs prefixed with file: (Go docs example)
  • Update documentation to show how to open various database locations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants