Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Totto16
Copy link
Contributor

@Totto16 Totto16 commented May 21, 2025

Add automatic DB migrations

Description

This adds automatic DB migrations to the SHARP server.

This works like this:

On startup the array MIGRATIONS is traversed, and every one is executed, if needed.

Details

We keep track of already executed migrations by a table in the database, the migration process automatically creates it the first time migrations are run.

To run a single migration, we check, if this migration is in the table (by the id we specified in the js object). If it's in that table, we only run it, if it was never executed (that is denoted by the status column)
If it is not present there, an entry for that is created, and it is run, setting the status accordingly.

With this every migration is run, when needed.

An additional feature is, the needsMigration function, That is a function, that queries the database to check if a certain migration is already run, this is helpful, as of now, everybody might (or might not) run those migrations manually. This is not required for later usages, after the migration was run once successfully, and no migration is run manually ever again.

Notes

This was tested on my instance for 2 scenarios: Nothing migrated, everything migrated, it worked in all cases.

I thought about the design of this, and this was the best solution IMO, but feel free to correct me or change this to your needs, if you have a better / another idea how to achieve this.

I had to refactor the general structure of the code, as the migrations HAVE to finish, before we run any other sql query, therefore the diff is quite large 😓 (look at the single commits, I factored it out nicely, so that you can see, what exactly happened)

Feel free to suggest changes either in code or in textual description on what to improve 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant