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

Unchanged db_table in RenameModel raises Unsafe.ALTER_TABLE_RENAME #26

Closed
tony opened this issue Dec 30, 2021 · 0 comments · Fixed by #27
Closed

Unchanged db_table in RenameModel raises Unsafe.ALTER_TABLE_RENAME #26

tony opened this issue Dec 30, 2021 · 0 comments · Fixed by #27

Comments

@tony
Copy link
Contributor

tony commented Dec 30, 2021

Describe the bug

Renaming a model with a fixed table name (e.g. Book to OldBook) will lead to a migration that raises ALTER_TABLE_RENAME, when no schema changes occur.

To Reproduce

  1. What model did you have?

Django model with Meta.db_table = 'table_name' hard coded

from django.db import models
class Book(models.Model):
    # any fields
    name = models.CharField(max_length=128, blank=True)
    class Meta:
        db_table = 'books`
  1. How did you change the model?

Renaming the class from Book to OldBook will lead to a migration that raises ALTER_TABLE_RENAME, when in actuality no schema changes occur. (Correct me if I'm mistaken!)


❯ poetry run ./manage.py makemigrations
Did you rename the book.Book model to OldBook? [y/N] y                                                                                                                                                           Migrations for 'book':
  src/project/book/migrations/0004_auto_20211230_2157.py                                                                                                                                                                             - Rename model Book to OldBook
  1. What migration were generated?
# Generated by Django 2.2.1 on 2021-12-30 22:18

from django.conf import settings
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('book', '0002_auto_20201018_0102'),
    ]

    operations = [
        migrations.RenameModel(
            old_name='Book',
            new_name='OldBook',
        ),
    ]
  1. What SQL was executed?
poetry run ./manage.py sqlmigrate book 0004
BEGIN;
--
-- Rename model Book to OldBook
--
COMMIT;
  1. What issue did you get?
Traceback (most recent call last):
  File "./manage.py", line 21, in <module>
    main()
  File "./manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "~/project/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "~/project/.venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "~/project/.venv/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "~/project/.venv/lib/python3.7/site-packages/django/core/management/commands/sqlmigrate.py", line 30, in execute
    return super().execute(*args, **options)
  File "~/project/.venv/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "~/project/.venv/lib/python3.7/site-packages/django/core/management/commands/sqlmigrate.py", line 64, in handle
    sql_statements = executor.collect_sql(plan)
  File "~/project/.venv/lib/python3.7/site-packages/django/db/migrations/executor.py", line 225, in collect_sql
    state = migration.apply(state, schema_editor, collect_sql=True)
  File "~/project/.venv/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "~/project/.venv/lib/python3.7/site-packages/django/db/migrations/operations/models.py", line 353, in database_forwards
    new_model._meta.db_table,
  File "~/project/.venv/lib/python3.7/site-packages/django_zero_downtime_migrations/backends/postgres/schema.py", line 394, in alter_db_table
    raise UnsafeOperationException(Unsafe.ALTER_TABLE_RENAME)
django_zero_downtime_migrations.backends.postgres.schema.UnsafeOperationException: ALTER TABLE RENAME is unsafe operation
See details for save alternative https://github.com/tbicr/django-pg-zero-downtime-migrations#changes-for-working-logic

Expected behavior
When database tables are unaltered, don't raise

Versions:

  • Postgres: 12
  • Python: 3.7
  • Django: 2.2
  • django-pg-zero-downtime-migrations library: 0.10
@tbicr tbicr closed this as completed in #27 Dec 31, 2021
tbicr pushed a commit that referenced this issue Dec 31, 2021
* alter_db_table: Improve handling of RenameModel when table name unchanged

* Add test for alter_table when db_table is the same

* Add unit test for changing models when db_table initially specified
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

Successfully merging a pull request may close this issue.

1 participant