BezBartek / django-db-views

Creating automatic migrations for Views models witch working reverse and full command options like in normal makemigrations
MIT License
62 stars 12 forks source link

conflict with `ViewRunPython` and regular view's `CreateModel` #20

Closed gonzalomolbar closed 2 years ago

gonzalomolbar commented 2 years ago

When I try to create a view with this package, the next migration is created as expected through makeviewmigrations:

    operations = [
        django_db_views.operations.ViewRunPython(
            code=django_db_views.migration_functions.ForwardViewMigration(
                "SQL TO RUN IN THE VIEW",
                "TABLE_NAME",
                engine="django.db.backends.postgresql_psycopg2",
            ),
            reverse_code=django_db_views.migration_functions.BackwardViewMigration(
                "",
                "TABLE_NAME",
                engine="django.db.backends.postgresql_psycopg2",
            ),
            atomic=False,
        ),
    ]

(I've obfuscated names and sql)

But then, when I run makemigration

    operations = [
        migrations.CreateModel(
            name="MODEL_NAME",
            fields=[ # EXAMPLE FILEDS
                (
                    "total_discount",
                    models.DecimalField(decimal_places=2, max_digits=12),
                ),
                ("total_returns", models.DecimalField(decimal_places=2, max_digits=12)),
                ("balance", models.DecimalField(decimal_places=2, max_digits=12)),
            ],
            options={
                "db_table": "TABLE_NAME",
                "managed": False,
            },
        ),
        migrations.DeleteModel(
            name="TABLE_NAME_django.db.backends.postgresql_psycopg2",
        ),
    ]

I've dug into the library a little and looks like this is because of the way ViewRunPython implements state.add_model, providing a lot of different fields that won't be detected as the model created in makemigrations and such it will create the "actual good one" and delete this "bad one", the one created through this library.

Look out at the name of the Model is deleting TABLE_NAME_django.db.backends.postgresql_psycopg2, which it comes from get_table_engine_name_hash. Yet, the actual DB name is TABLE_NAME, which is even different from the actual MODEL_NAME

I wonder if this is something wrong with my setup or an actual issue of the package. I would find this hard to believe since it looks like it's quite used...

I have an idea of what could be fixed if this package is open for contributions! And of course, is this if an actual issue and not something with my own setup...

Thanks in advance!

Run with Python 3.8.13 and Django 3.2.15

BezBartek commented 2 years ago

Hello @gonzalomolbar !

Thank you for the comment :) Yes package is open for contributors

Yes, you probably have right, it seems to be caused by state_forwards implemented from the last lib upgrade

If you have a good solution please share it :) I will try to solve it during the weekend

BezBartek commented 2 years ago

@gonzalomolbar Please try new version from https://pypi.org/project/django-db-views/0.1.4/ or here from master branch.

I fixed issue by detecting whether it is view migration or regular make migration command. This was the best what I was able to do during the weekend to make lib work again.

Thank you once again for your comment, and please let me know if you have better solution. I will try to find one as soon as I get some more free time