Migrations
See original GitHub issue(edited by @grigi ) Migrations is currently planned as a post-v1.0 feature.
Some useful resources for getting this working right now:
- https://github.com/tortoise/tortoise-orm/issues/8#issuecomment-534946871 (Native python, MySQL)
- https://github.com/tortoise/tortoise-orm/issues/8#issuecomment-575982472 (Go, MySQL/PostgreSQL/SQLite)
Forward migrations
Best guess at this time for a complete solution, not a “quick” solution:
- Make the
describe_model()
contain all the data one needs to generate DDL from. - Generate a sequence of high-level DDL instructions from
describe_model()
- Port
generate_schema
to use this high-level DDL instructions to generate a schema in the requested dialect. (This would require some decoupling from the driver instance, look at #72) - Build a persistance model (read/write) for high-level DDL insctuctions much like Django Migrations
- And need a way to run “management” commands. So possibly a standard command-line utility.
- Build a full model (e.g. same format as
describe_model()
)from a series of high-level DDL instructions (as in read the migration persistance model) - Diff models, and generate a diff DDL instruction set.
- If a Diff requires a default not pre-specified, we need to ask the user for something.
- Have some way of determining the “version” of the models persisted in the DB.
- Put it all together and make it work
I’m not particularily happy about many migration systems storing state in the DB itself, as for some environments that won’t work, but it is a very good place from a consistency PoV. We should have a backup for when we generate DDL that one would pass on to the DB team in cases where DDL changes have an established process (e.g. enterprise/consulting)
Data migrations
Proper data migration management makes this very useful:
- Allow custom user scripts as migrations
- Allow data-migrations to get a version of a model as it was at that time. So ability to build a model class from the intermediate
describe_model()
data - Handle DB edge cases, like Postgres being able to do DDL changes in a transaction OR data changes in a transaction, but not both.
Backward migrations
There is some cases where backwards migrations cannot be done as a forward migration clobbered/destroyed some data. In cases where we detect that we need mising data, and request a sane default. What do we do about data migrations? Is it safe to just emit a warning?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:23
- Comments:32 (23 by maintainers)
Top GitHub Comments
Hey guys, I’m excited to announce that now we have a migrate tool written by pure python and just for tortoise-orm.🎉
Which named
aerich
.😜Now it supportNow it supportMySQL
, but is easy to extendpostgres
andsqlite
.💪MySQL
&sqlite
&Postgres
, but the latter two need more tests.That is a young project and must be lacking and exists bugs, PR and issues and star are welcome!
repo: https://github.com/long2ice/aerich
I write a manage.py script to make migrations for tortoise-orm,depend on dbmate and schemalex.
Requirements
brew install dbmate
.brew install schemalex
.Usage
just support
makemigrations,migrate,rollback,initdb
nowpython manage.py -h
Link
https://gist.github.com/long2ice/0c9153d70fa2a08ec177fb2643820f3b
Hope to useful.