Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add automatic DB migrations
Description
This adds automatic DB migrations to the SHARP server.
This works like this:
On startup the array
MIGRATIONSis 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
needsMigrationfunction, 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 😄