tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Migrations

Open abondar opened this issue 8 years ago • 32 comments

(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?

abondar avatar Jun 28 '18 12:06 abondar

Which models did you think it's necessary or which estructure design this feature need..

Marlysson avatar Jun 28 '18 12:06 Marlysson

Currently I haven't invested much time into this matter, so I haven't came up with design at the moment. I am afraid that in this task we can't get away with just using some existing tool, because there are none appropriate on horizon.

I think taking a look at Django migrations would be good reference for the start. Other existing migrations engines are either not good enough or locked on some certain db.

One of my main concerns is that system should support running python code for data migrations along with schema migrations. May be it shouldn't be part of early implementation, but it should be designed in way that is ready for it.

abondar avatar Jun 28 '18 13:06 abondar

Considering it took Aeracode essentially three tries before he got Migrations for Django to work nicely, I'd assume that this is a very complex problem. I also think the Django Migrations are one of the more sane implementations of it that I know of.

The core features that I use are:

  • Comprehensive schema in Migrations
  • Automatic Migration building
  • Ability to easily run arb code in a migration
  • Ability to get a Model for an old version as it is at THAT time (so your custom code doesn't fail weirdly later down the line)
  • Cross-DB support

A big help to Django Migrations, is that they are tightly coupled to the Django Management Interface, which auto discovers Models. In Tortoise we need an explicit import, and explicit Schema-creation command. I feel we can only attempt this once we have the ability to manipulate state from outside of your python code.

grigi avatar Jun 28 '18 21:06 grigi

Yea, today I dived a little into django migrations and it seems to be one hell of a task to handle. But that is what makes it exciting :)

Most likely expanding apps conception to have autodiscover and more control over the models outside of python code is good idea by itself, disregarding migrations implementation. At the moment migrations seems to be too faraway dream to really concentrate on them now and we should first make tortoise good enough to use on it's own, hoping that users will be able to handle schema migrations by themselfs.

Summarizing I think we should it take it this way:

  1. Come up with better apps module idea, that will not restrict users to have only certain project structure, so tortoise will stay easily pluggable into any async project, but will give ability to work with models more magically
  2. Make autodiscover of models based on this apps concept.
  3. Implement basic migration tool so users will stay within scope of ORM. I think it should look like system where user can manually create migration which will consist of forward and backward methods, where user can write pure SQL and write some basic python code to migrate it's data (without counting on ORM, because implementing models versioning seems to be hard). It will cover basic needs of users for migrations and give some time to think of something better.

May be there is a way to make dummy implementation of model versioning through pickling models at the moment of creating migration, but this seems to be really strange way to solve the problem :)

P.S. Stumbled across this roadmap on aerocode blog - https://www.aeracode.org/2018/06/04/django-async-roadmap/ - django seems to be taking interesting way :)

abondar avatar Jun 28 '18 22:06 abondar

Interesting roadmap. It seems that roadmap is 2-3 years out for a fully async implementation, but they plan to do quick wins first.

I have been thinking on a way to do a way to manager apps/modules, and I have an idea which I'll document in a new issue (as a discussion)

grigi avatar Jun 29 '18 05:06 grigi

Maybe just write an translator to djangos models and use their migrations?

imbolc avatar Jun 29 '18 09:06 imbolc

That thought flashed through my mind, but I really don't like the idea of making django dependency or copy pasting all migrations code. I pretty sure it's not as easy as it sounds, because django is quite world in itself and all modules a little bit coupled together. May be I will investigate into this matter and if it's turns out to be easy deal than may be implementing such transpiler could be done in separate project which will be pluggable into tortoise

abondar avatar Jun 29 '18 09:06 abondar

I really think this should be Post-1.0 feature. Migrations are not easy, so we should not rush it.

grigi avatar Jun 29 '18 09:06 grigi

Any thoughts on integrating with Alembic project for migrations? There are many projects that use Alembic for managing migrations and SQLAlchemy as the ORM. Integrating with Alembic would help those projects move to Tortoise ORM for the async io benefits with ease. Just thinking out loud here ...

vishalhkd avatar Sep 12 '18 14:09 vishalhkd

The thing is, as far as I see - Alembic it tightly coupled with sqlalchemy and giving it to users of Tortoise would be downright confusing. Making some tool that hides Alembic implementation under our own interface would work better, but it's still would be hard to make much progress with such approach, cause we would be limited to what Alembic could offer and it's still under active development.

Migrations is the feature that I really want to address, but I don't want to make fast and bad solution here. May be after releasing 1.0 I will complement code for migrations that I use in my projects and publish in some kind of way. It will be far fetch from real migration system, but give some basic features for people who struggle with it and reference for developing better system

abondar avatar Sep 12 '18 15:09 abondar

So, after reading the docs of Alembic, I agree that it appears very tightly coupled to SQLAlchemy, and also, not tightly enough. One has to manage a lot of boilerplate.

There is some interesting design decisions that differ quite a bit from how Django does it:

  1. Django keeps a log, Alembic does a diff
  2. Django migration generation is more thorough as it uses data from the defined models.
  3. I like how Alembic allows one to generate SQL for DDL commands, this way we can get separate DBAs to manage that.
  4. Alembic has a more complex and capable DAG.

We will definitely look at both it and Django migrations and see what parts we can use?

If you want to get started on this (even if it is requirements gathering) you are welcome to @vishalhkd

Whilst Andrey wants to tackle Migrations after v1, I would instead like to tackle performance after v1.

grigi avatar Sep 12 '18 16:09 grigi

Any suggestions how to make migrations at the moment? Manually?

danieldaeschle avatar Apr 16 '19 15:04 danieldaeschle

Not as of yet @danieldaeschle I think Manually might be the best option.

Can you think of an intermediate system that would simplify the work of doing a migration manually?

grigi avatar Apr 16 '19 15:04 grigi

I've written a table schema SQL synchronizer. Feel free to use it. Now it only works for MySQL/MariaDB, but in my opinion, it would be not too hard to generalize for other DBs. https://github.com/wallneradam/python_sql_schema_sync

I use it with Tortoise and it works wonderfully. It would be cool to integrate it into.

How I use now...

import schema_sync

# Get MySQL client
client = tortoise.Tortoise.get_connection('default')
# Get current table schemas
tables = await client.execute_query(
    "SELECT DISTINCT TABLE_NAME FROM `information_schema`.`tables` WHERE TABLE_SCHEMA = %s", db)
old_schema = ""
for table in tables:
    old_schema += (await client.execute_query(f"SHOW CREATE TABLE `{table['TABLE_NAME']}`"))[0]['Create Table']
    old_schema += ";\n"
# Get the new schemas
new_schema = tortoise.utils.get_schema_sql(client, safe=False)
# Get queries to update schema
update_queries = schema_sync.sync(old_schema, new_schema)
# Update schema
if update_queries:
    logger.debug("Update queries to be executed:\n%s", update_queries)
    await client.execute_script(update_queries)

Because Tortoise creates separate CREATE INDEX queries, and my script (atm) can only handle KEY lines inside CREATE TABLE, I created a custom schema generator class for MySQL:

from tortoise.backends.mysql.client import MySQLClient
from tortoise.backends.mysql.schema_generator import MySQLSchemaGenerator


class KeyIndexMySQLSchemaGenerator(MySQLSchemaGenerator):
    """
    Changes "CREATE KEY" queries to KEY parameters under CREATE TABLE query (it is needed for schema_sync)
    """
    TABLE_CREATE_TEMPLATE = "CREATE " + "TABLE {exists}`{table_name}` ({fields}__INDEXES__){extra}{comment};"
    INDEX_CREATE_TEMPLATE = "KEY `{index_name}` ({fields})"

    def __init__(self, client) -> None:
        super().__init__(client)
        self._field_indexes = []

    # noinspection PyProtectedMember
    def _get_index_sql(self, model, field_names: List[str], safe: bool) -> str:
        """ Get index SQLs, but keep them for ourselves """
        self._field_indexes.append(self.INDEX_CREATE_TEMPLATE.format(
            exists="IF NOT EXISTS " if safe else "",
            index_name=self._generate_index_name(model, field_names),
            table_name=model._meta.table,
            fields=", ".join(['`' + f + '`' for f in field_names]),
        ))
        return ""

    def _get_table_sql(self, model, safe=True) -> dict:
        res = super()._get_table_sql(model, safe)
        if self._field_indexes:
            self._field_indexes.insert(0, '')
            indexes = ',\n    '.join(self._field_indexes) + "\n"
            self._field_indexes.clear()
        else:
            indexes = '\n'

        res['table_creation_string'] = res['table_creation_string'].replace('\n__INDEXES__', indexes)
        return res


MySQLClient.schema_generator = KeyIndexMySQLSchemaGenerator

wallneradam avatar Sep 25 '19 09:09 wallneradam

There is so much to unpack here. Thank you for all this info.

The schema sync script:

  1. It has a basic SQL parser
  2. It has a diffing system
  3. Based on the diff it can auto add/remove columns/indexes. (but not ALTER them)

The inside/outside create/alter statements.

  1. We definitely needed to handle outside statements, as e.g. PostgreSQL's extensions are always outside statements, whereas MySQL prefers to add it inside.
  2. I was told by our DBA 2 weeks ago that MySQL does alter statements by doing a full table copy. So if I have many small alter statements it is unnecessarily slow. PostgreSQL on the other hand doesn't really care.

Yes, this could be very useful, also for #104.

grigi avatar Sep 25 '19 14:09 grigi

  1. Based on the diff it can auto add/remove columns/indexes. (but not ALTER them)

It can alter fields as well. It creates lines like this to modify fields:

ALTER TABLE `user` MODIFY `full_name` VARCHAR(255) NOT NULL;

wallneradam avatar Sep 25 '19 14:09 wallneradam

Ah, i see. I missed that.

grigi avatar Sep 26 '19 14:09 grigi

Would it be possible to have a least some basic version for fields adding/removal? That would cover 80% of cases. Or any suggestions on what to use instead of a manual approach?

yrik avatar Dec 21 '19 19:12 yrik

Due to my current company requiring us to manually submit DDL migrations I have not had the urgency to implement this. I know that @wallneradam has implemented a lightweight migration script (for MySQL specifically).

I am not opposed to adding such functionality (in fact, I loved how well Django's Migrations post v1.7 worked at my previous job).

What do you propose? A simple way to see if columns need to be added/removed when doing a generate_schema()?

For that to work we need a way of fetching the current table format? I think that is more-or-less what @wallneradam is doing.

grigi avatar Dec 22 '19 09:12 grigi

It would be great if generating migrations doesn't require a database connection, but like Django does it. The generation of migrations would instead look at the fields of the previous migration, compare it to the current set of fields on the model and generate a set of commands required to get from one state to the next. The set of commands would be abstract such as AddField and RenameField and can then be converted into SQL at migrate-time.

antonagestam avatar Dec 22 '19 09:12 antonagestam