piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.46k stars 91 forks source link

Backwards migrations not always executed (plus missing template) #1122

Open stronk7 opened 1 month ago

stronk7 commented 1 month ago

Hi,

this comes from https://github.com/piccolo-orm/piccolo/discussions/1121, where it was discussed about piccolo not executing backwards migrations (not sure which ones are and which ones aren't, I'm afraid).

In that discussion, @sinisaos provided a quick-patch that, apparently made those backwards migrations to start working and this issue is about to consider that fix, or a better one.

Basically, this is the migration step:

from piccolo.apps.migrations.auto.migration_manager import MigrationManager

from myapp.tables import TestTable

ID = "2024-10-28T20:07:36:439746"
VERSION = "1.21.0"
DESCRIPTION = "test-add-drop-index"

async def forwards():
    manager = MigrationManager(
        migration_id=ID, app_name="", description=DESCRIPTION,
    )

    async def run():
        await TestTable.create_index(
            columns = ["col1", "col2"], # or ([TestTable.col1, TestTable.col2])
            if_not_exists = True
        ).run()

    manager.add_raw(run)
    return manager

async def backwards():
    manager = MigrationManager(
        migration_id=ID, app_name="", description=DESCRIPTION
    )

    async def run():
        await TestTable.drop_index(
            columns=["col1", "col2"], # or ([TestTable.col1, TestTable.col2])
            if_exists=True
        ).run()

    manager.add_raw(run)
    return manager

(yes, it creates a composite index on migration, because the models don't support them)

And the problem is that, while piccolo migrations forwards myapp works and the index is created, trying to backwards it doesn't work at all, with the code not being invoked ever.

And that's it, so far. Again, the linked discussion has a "working" solution (tested here) and contains other details that may be of interest.

Ciao :-)