Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Vacuum with Flask SQLAlchemy

I'm trying to limit the size of a database in flask using Flask_SQLAlchemy. I want to start removing the oldest rows when the file size nears some limit set. I figured out how to remove rows from the beginning of the database, but the file size doesn't shrink because vacuuming isn't on (I think).

Is there a way to enable that or a better way to manage filesizes for a database in SQLAlchemy? I'm new to databases so any help is appreciated.

Thanks!

like image 866
S. Python Avatar asked Nov 15 '25 09:11

S. Python


2 Answers

So I found a way to do this using SQLite3 in python, I'll add it here in case anyone finds this page and needs help. To vacuum after deleting, I just ran these three commands.

self.con = sqlite3.connect(databasename) # Open the database in sqlite
self.con.execute("VACUUM") # Execute the vacuum command
self.con.close() # Close the database

If anyone has a better way to do this which is native to SQLAlchemy please let us know!

like image 172
S. Python Avatar answered Nov 18 '25 21:11

S. Python


I use my db = SQLAlchemy() object
and call

with db.engine.begin() as conn:
    conn.execute("VACUUM")
like image 40
Greg Avatar answered Nov 18 '25 20:11

Greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!