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

Certain database names results in 404: "Database not found: None" #1181

Closed
jieter opened this issue Jan 7, 2021 · 4 comments
Closed

Certain database names results in 404: "Database not found: None" #1181

jieter opened this issue Jan 7, 2021 · 4 comments
Labels

Comments

@jieter
Copy link

jieter commented Jan 7, 2021

I have a file named test-database (1).sqlite. When requesting the home route /, I see datasette is able to read it correctly:

Screenshot 2021-01-07 at 12 54 46

However, if I click any of the links, datasette replies with: Error 404 Database not found: None

It seems the hash is crucial, as renaming the file to database (1).sqlite makes the error go away.

This lines checks for a single dash:

if db_name not in self.ds.databases and "-" in db_name:

$ datasette test-database\ \(1\).sqlite 
INFO:     Started server process [68314]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8001 (Press CTRL+C to quit)
INFO:     127.0.0.1:54043 - "GET /favicon.ico HTTP/1.1" 200 OK
INFO:     127.0.0.1:54043 - "GET / HTTP/1.1" 200 OK
...
INFO:     127.0.0.1:54044 - "GET /favicon.ico HTTP/1.1" 200 OK
INFO:     127.0.0.1:54044 - "GET /test-database (1) HTTP/1.1" 404 Not Found

Version:

$ datasette --version
datasette, version 0.53
@simonw simonw added the bug label Jan 8, 2021
@simonw
Copy link
Owner

simonw commented Jan 8, 2021

Yes, that logic is definitely at fault. It looks like it applies urllib.parse.unquote_plus() AFTER it's tried to do the - hash splitting thing, which is why it's failing here:

if db_name not in self.ds.databases and "-" in db_name:
# No matching DB found, maybe it's a name-hash?
name_bit, hash_bit = db_name.rsplit("-", 1)
if name_bit not in self.ds.databases:
raise NotFound(f"Database not found: {name}")
else:
name = name_bit
hash = hash_bit
else:
name = db_name
name = urllib.parse.unquote_plus(name)
try:
db = self.ds.databases[name]
except KeyError:
raise NotFound(f"Database not found: {name}")

@simonw
Copy link
Owner

simonw commented Jan 8, 2021

I'm going to add a unit test that tries a variety of weird database names.

@simonw
Copy link
Owner

simonw commented Jan 17, 2021

Here's the incomplete sketch of a test - to go at the bottom of test_cli.py.

@pytest.mark.parametrize(
    "filename", ["test-database (1).sqlite", "database (1).sqlite"]
)
def test_weird_database_names(ensure_eventloop, tmpdir, filename):
    # https://github.com/simonw/datasette/issues/1181
    runner = CliRunner()
    db_path = str(tmpdir / filename)
    sqlite3.connect(db_path).execute("vacuum")
    result1 = runner.invoke(cli, [db_path, "--get", "/"])
    assert result1.exit_code == 0, result1.output
    homepage_html = result1.output
    assert False

@simonw simonw added this to the Datasette 0.54 milestone Jan 24, 2021
@simonw simonw closed this as completed in a5ede3c Jan 25, 2021
This was referenced Jan 25, 2021
simonw added a commit that referenced this issue Jan 25, 2021
@rayvoelker
Copy link

I wonder if I'm encountering the same bug (or something related). I had previously been using the .csv feature to run queries and then fetch results for the pandas read_csv() function, but it seems to have stopped working recently.

https://ilsweb.cincinnatilibrary.org/collection-analysis/collection-analysis/current_collection-3d56dbf.csv?sql=select%0D%0A++*%0D%0Afrom%0D%0A++bib%0D%0Alimit%0D%0A++100&_size=max

Datasette v0.59.4
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants