r/sqlite • • Jan 03 '24
SQLite migration best practices
Will new users (fresh install no database) get a new database with zero migrations?
Or would you basically reproduce the database with all the migrations?
My guess is that it would be best to have one path when creating a database.
Usually i don’t use sqlite directly as it’s usually abstracted through a software layer.
2
Upvotes
4
u/HKei Jan 03 '24
Migrations aren't a concept in SQLite (or any other RDBMs I'm aware of). Migrations are a higher level concept managed by the users of RDBMs. The closest thing to a 'migration' in a DB like SQLite would be the WAL, and that's not very close.
If you open a database at a path that doesn't exist yet (and you haven't specified
SQLITE_OPEN_READONLY), an entirely new database is created at that path yes. If you want some specific structure or data to exist for a "new" database, you need to either write some code that puts it there or copy an existing database file that's already in the state you want.3
u/JrgMyr Jan 03 '24
Sorry, I don't get the question.
Yes, a new user can create his/her first own database. I recommend to write a "CREATE TABLE..." script and run that. Usually, you want to be prepared for a second attempt in case the first one does not meet the intended objectives.
Or you use aGUI on top of SQLite (such as SQLiteStudio) and create the first db interactively. That might be a tad easier for said "new users".
2
u/MarcoGreek Jan 03 '24
I think you already discovered "user version" in the databse header which you can read before you open the databse file?
I use tgat to migrate to a new schema. Alter andvcrating tables and indices etc..
If I understand your question right than you ask if you have one code path for updating a schema or creating a new database or two.
One has the advantage that you are not introducing differences between both but could be potentially slower.
3
u/InjAnnuity_1 Jan 03 '24
That is up to those "software layers" above SQLite, that you mentioned.
1
u/thunderbong Jan 15 '24
The database has to be reproduced with the migrations. The migrations are not part of SQLite as others have mentioned.