Replies: 2 comments 1 reply
-
I use option 4, but I don't add methods or anything like that, just small changes. I did think about what I'd do if I wanted to add methods, and I thought I'd probably just have functions in a separate module. I assume you've considered that as well, are there advantages to defining methods right on the ORM classes? |
Beta Was this translation helpful? Give feedback.
-
@doulighan am make heavy use of extension methods (that also require specific imports) that I want to retain when I update the models. I came up with a (very ugly) python script that merges the model definitions from a staging file (sqlacodegen out) into a destination file from which I import the models in my app. I might me willing to invest a bit more into this approach. Here's the script: ` MODEL_IMPORTS_HINT = '## model imports' def process_models(model_code: str):
def extract_class_name(line: str): if name == 'main':
` |
Beta Was this translation helpful? Give feedback.
-
Just wondering what strategy people are using to make their production life the most stable.
In a perfect world sqacodegen would output exactly what you need, making the need to manually manage a list of models obsolete. You could stick it in a CI pipeline and feel comfortable that your models are a perfect representation of the database at that point. But of course it doesn't quite work like that, there are going to be modifications you need to make to the output.
Strategy 1) would be to generate your models once, and after that uninstall sqlacodegen from your pyenv relinquish control to the code editor. Any new changes added should be added manually. This feels the most stable, but with dozens of tables and thousands of columns, its really a pain to manage.
would be to allow regeneration of the models, but write very particular unit tests ensuring that the proper manual edits have been applied after the fact. Relies on the developers having a good memory. Also feels unstable. But at least huge database changes can be re-imported fairly smoothly.
would be to go all-in and create your own generators that give perfect output. I haven't spent any time with the source code, but I'm worried about the time investment, or running into walls that are simply not solvable without massive changes to the source code. In general I have very little experience writing code that outputs other code, I have no idea what I'm in for.
Maybe some kind of post-processing, with sed or something? This seems the most complicated to me, especially if you need to add something more dense like methods or hybrid_properties on your models.
Does anyone have opinions on this? How do you set things up?
Beta Was this translation helpful? Give feedback.
All reactions