Skip to content

Adding a "recreate" flag to the Database constructor #97

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

Closed
betatim opened this issue Apr 4, 2020 · 4 comments
Closed

Adding a "recreate" flag to the Database constructor #97

betatim opened this issue Apr 4, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@betatim
Copy link

betatim commented Apr 4, 2020

I have a script that imports data into a sqlite DB. When I re-run that script I'd like to remove the existing sqlite DB, instead of adding to it. The pragmatic answer is to add the check and file deletion to my script.

However I thought it would be easy and useful for others to add a recreate=True flag to db = sqlite_utils.Database("binder-launches.db"). After taking a look at the code for it I am not so sure any more. This is because the connection string could be a URL (or "connection string") like "file:///tmp/foo.db". I don't know what the equivalent of os.path.exists() is for a connection string or how to detect that something is a connection string and raise an error "can't use recreate=True and conn_string at the same time".

Does anyone have an idea/suggestion where to start investigating?

@simonw
Copy link
Owner

simonw commented Apr 13, 2020

Interesting thought. I've run into this myself a lot - many of my scripts intend to create the database from scratch, so I end up running !rm /tmp/blah.db in Jupyter and occasionally getting errors if the file doesn't exist.

I think adding recreate=True could make sense. It could throw an error if you attempt to use it after passing in something other than a path to a file on disk.

@simonw
Copy link
Owner

simonw commented Apr 13, 2020

I wonder if it should delete an recreate the file or if it would be safer to drop every table instead?

Dropping tables gets messy: then you need to drop triggers and views, and you need to run vacuum to clean up the space.

My worry with deleting and recreating the file is that it could trigger errors in other processes that are currently attached to that database file. But... if you know that's going to be likely, maybe you shouldn't use the recreate=True feature?

@simonw simonw closed this as completed in ad6ac19 Apr 13, 2020
@simonw simonw added the enhancement New feature or request label Apr 13, 2020
@betatim
Copy link
Author

betatim commented Apr 15, 2020

Woah! Thanks a lot. Next time I will add a more obvious/explicit "if you like this idea let me know I'd love to work on it to get my feet wet here" :D

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

No branches or pull requests

2 participants