datasette / datasette-export-database

Export a copy of a mutable SQLite database on demand
Apache License 2.0
5 stars 0 forks source link

Initial plugin #1

Closed simonw closed 6 months ago

simonw commented 6 months ago

Goal is a datbase action menu item that can request a SQLite database copy. If there is enough space in /tmp it runs a subprocess that does a VACUUM INTO to create an atomic snapshot in a temporary file, then lets you download it, then deletes the file.

simonw commented 6 months ago

I got Claude 3 Opus to write the first version: https://gist.github.com/simonw/4cecde4a729f4da0b5059b50c8e01359

simonw commented 6 months ago

For checking space:

import shutil

# Get the total, used, and free space in bytes for the /tmp directory
total, used, free = shutil.disk_usage('/tmp')

The free will be in bytes.

simonw commented 6 months ago

Still needed:

simonw commented 6 months ago

I'm going to test for available space when the user requests a download, not every time I show the export button. I don't think it's worth the extra computation on every page load for a button that is hardly ever pressed. It would also be confusing to the user if the button sometimes isn't there.

simonw commented 6 months ago

One thing that bothers me here is that /db/-/export-database is an expensive operation - and someone malicious could trick logged in users into triggering it by sending them links to that full URL.

Since database actions cannot perform POST requests just yet I'm going to add a signed parameter to the URL instead, to ensure users cannot have export links generated for them.