Skip to content
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

Incorrect Migration Order when renaming triggers and model #188

Open
dominikbak opened this issue Nov 13, 2024 · 0 comments
Open

Incorrect Migration Order when renaming triggers and model #188

dominikbak opened this issue Nov 13, 2024 · 0 comments

Comments

@dominikbak
Copy link

Currently when trigger is renamed, it is removed and added with the new name.
When running python manage.py makemigrations, the generated migration order is incorrect when renaming models that have associated triggers. This causes issues. Specifically, after a model rename, triggers still reference the old model name in the trigger.uninstall_sql, resulting in the trigger not being removed from the database and causing errors when the trigger is subsequently triggered.

Here is the current problematic migration order generated by Django's makemigrations:

migrations.RenameModel(
    old_name="PartnerToBuyerDSPIntegration",
    new_name="SupplyToDemandAdPlatformIntegration",
),
pgtrigger.migrations.RemoveTrigger(
    model_name="supplytodemandadplatformintegration",
    name="unique_integration_type_for_partner_buyer",
),
pgtrigger.migrations.RemoveTrigger(
    model_name="supplytodemandadplatformintegration",
    name="check_account_partner_to_buyer_dsp_integration",
),
pgtrigger.migrations.AddTrigger(
    model_name="supplytodemandadplatformintegration",
    trigger=pgtrigger.compiler.Trigger(
        name="unique_integration_type_for_partner_buyer",
        # Trigger SQL here...
    ),
),
pgtrigger.migrations.AddTrigger(
    model_name="supplytodemandadplatformintegration",
    trigger=pgtrigger.compiler.Trigger(
        name="check_account_partner_to_buyer_dsp_integration",
        # Trigger SQL here...
    ),
),

Problem
In the pgtrigger.migrations.RemoveTrigger migration step, the function trigger.uninstall_sql still references the old model name PartnerToBuyerDSPIntegration. Since the model has already been renamed to SupplyToDemandAdPlatformIntegration, the trigger cannot be properly removed, resulting in migration errors when the trigger tries to act on the non-existent table.

Working Migration Order
The issue can be resolved by adjusting the migration order to remove triggers before renaming the model, as shown below:

pgtrigger.migrations.RemoveTrigger(
    model_name="partnertobuyerdspintegration",
    name="unique_integration_type_for_partner_buyer",
),
pgtrigger.migrations.RemoveTrigger(
    model_name="partnertobuyerdspintegration",
    name="check_account_partner_to_buyer_dsp_integration",
),
migrations.RenameModel(
    old_name="PartnerToBuyerDSPIntegration",
    new_name="SupplyToDemandAdPlatformIntegration",
),
pgtrigger.migrations.AddTrigger(
    model_name="supplytodemandadplatformintegration",
    trigger=pgtrigger.compiler.Trigger(
        name="unique_integration_type_for_partner_buyer",
        # Trigger SQL here...
    ),
),
pgtrigger.migrations.AddTrigger(
    model_name="supplytodemandadplatformintegration",
    trigger=pgtrigger.compiler.Trigger(
        name="check_account_partner_to_buyer_dsp_integration",
        # Trigger SQL here...
    ),
),

Proposed Solution
To fix this issue, the pgtrigger library should either adjust the migration order or alternatively, uninstall_sql could respect the model rename, referencing the new table name in the SQL uninstall step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant