django-squash icon indicating copy to clipboard operation
django-squash copied to clipboard

Optional auto adding namespace the functions used in the code and reverse_code arguments of RunPython

Open luzfcb opened this issue 3 years ago • 4 comments

Problem

In a medium complexity project, I have several migrations whose function names referenced in code and reverse_code of RunPython are respectively forward and backward. django-squash manages to create these functions in the new migration file, however it creates them with duplicate names. As there are more than a hundred of them per app, it is not so good to analyze and correct errors manually.

A possible solution

I guess maybe modify django-squash instead of using the same name as the original function, change it to generate the function definition name for something like

app_name + _ + migration_file_name_without_extension + _ + function_name + (apps, schema_editor):  # noqa: E501

example: app_name is user migration_file is 0106_populate_foobar_field the code function is forward

def user_0106_populate_foobar_field_forward(apps, schema_editor):  # noqa: E501
    # ....
    # ...

Thoughts?

luzfcb avatar May 26 '22 14:05 luzfcb

That makes sense, PRs are welcome :D

kingbuzzman avatar May 26 '22 14:05 kingbuzzman

@kingbuzzman Since you know the codebase, can you please point me to the relevant parts of the source code that I should study in order to change and implement the proposed feature? This will probably save me some debug time....

luzfcb avatar May 26 '22 16:05 luzfcb

Inside the extract_function(), it gets called here.

Please add an option to turn function renaming on/off. In case someone wants to opt out.

Lastly. Please add tests. I can't stress this last one enough.

kingbuzzman avatar May 26 '22 16:05 kingbuzzman

@luzfcb I really want to close this issue, but didnt want to leave you hanging either.

I started https://github.com/kingbuzzman/django-squash/pull/42/files to address this, im not sold on the name DJANGO_SQUASH_CUSTOM_RENAME_FUNCTION -- but its a working draft.

The idea is that you define a function somewhere in your code, and add DJANGO_SQUASH_CUSTOM_RENAME_FUNCTION='dot.path.location.where.is.the.function' in your settings.py and implement it obviously.. then this function would be called every time the squash finds an non-elidable operation and you can there dictate the name of the function. Still need to write an integration test for it.

But, does this work? is this what you had in mind? So far here are the variables you would have available migration (where the function is), name (name of function), app (name of the app), operation (operation object). From there you can name the functinon as you see fit.

Please let me know what you think. Thanks

kingbuzzman avatar Jan 30 '24 12:01 kingbuzzman