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
Feature request: database migrations #10
Comments
@infogulch I noticed that you are already thinking about this.
|
Yes I agree that tying something like migrations too closely to a specific strategy is not a good idea, but maybe there's some useful general techniques we can use from articles like this. In case you weren't aware, there is already functionality to run initialization routines at startup. Define a new template with a name that matches I use this inside my xrss application: (note it's using old syntax, I'll update it soon tm) https://github.com/infogulch/xrss/blob/master/templates/.schema.html
I will acknowledge that this is not fleshed out, but maybe you can see the basic skeleton of a usable migration process in there. What do you think? |
No, I was not aware of this possibility to run routines at startup. Sure, it can be useful. Question : do all those init templates run at each startup ? Surely, if yes, I suppose we could manage conditional migrations at startup using Another point: I really like the idea of providing the migrations along side the code using it. It should upgrade LOB at the next level. I was thinking about some components that you should just copy/paste to make them work, something like HyperUI but for xtemplate/htmx/tailwind/hyperscript. This seems impossible with the init templates you propose. BTW, I have started a project exploring my ideas about all that stuff. I called it Lazy Lob Web. I'll appreciate your comments about it, if any. |
Yes all init templates run on every startup. Sure you could do it all in a single SQL script like I did with xrss, but templates have plenty of expressiveness to manage the process. Off the top of my head here's how you might iterate over sql migration files in a "migrations" directory and execute new ones:
Some further ideas:
My point is that it should be possible to script anything you want with templates. That said it would be understandable if you'd rather not do this inside templates (don't knock it too quickly, browsing templated migration application result files sounds pretty nice for an admin ui). If you'd rather not use templates, then perhaps a custom DotProvider would be suitable, and if not that then writing your own migration system before starting xtemplate would probably be the next step. I'm more than happy to document various migration strategies, link to custom migration DotProviders, consider adding general features that would make migrations easier to manage, etc. But I don't see how to make migrations a built-in without tightly tying the implementation to specific databases and drivers, which I would really like to avoid. Your LLW project looks interesting, I'll keep an eye out for how you integrate the various components. 😄 |
So I'm updating xrss to use the new xtemplate, and I came up with a migration strategy that is general and simple enough I would consider adding to the db provider. It requires 3 config parameters:
If these values are set, then on start the DB provider will run a migration procedure that looks like this:
Notes:
Thoughts? |
This seems like a great design to me considering the app as a whole. It can be a great choice and if you make it yours, I will accept it and not discuss about it again after this last comment. Let me advocate my initial proposition one last time. First point: your proposition breaks LO Second point: your proposition is inaccurate considering the development of independent components to assemble for building apps. Your migrations are defined as incremental. Adding a component requires the addition of a migration file with an id related to the history of the app, which implies that as component can not be use "as it", just with a copy/paste. Well, we could expect users of xtemplate to be able to manage such process. To conclude: your solution seems a very good one, even if I prefer mine. It's up to you now 😁 |
And, please, let me know. 😁 |
Just in case : by migrations, I mean updating the database schema, not moving from one database to another.
Do you have an idea about how to handle migrations ?
I've just had one. Let me share with you (first draft?).
I was inspired by HyperScript init feature. It is used to launch initialisation script on page load, while preserving Locality Of Behaviour (LOB). I think we could proceed the same way and write migrations in templates, at the meaningful places for the dev. (So, potentially, migrations could be anywhere, even grouped in a dedicated template, even if that does not follow LOB design - should be documented if the dev disagree with LOB in this case).
So we should write migrations somehow like this:
Migrations should be run when parsing templates, when Caddy start or reload config.
The database should have a reserved table
migrations
(or xtemplate should try to create it when meeting the first migration). This table should have 3 columns:id
: the migration id, should be a human friendly timestamp (2024032816100
in my previous example),date_run
: store when the migration ran,log
: store the queries and their output if any.So, this table will allow xtemplate to ensure to run migration only once, and store logs about how they ran.
What do you think ? @infogulch
The text was updated successfully, but these errors were encountered: