tbicr / django-pg-zero-downtime-migrations

Django postgresql backend that apply migrations with respect to database locks
MIT License
525 stars 25 forks source link

Unchanged `db_table` in `RenameModel` raises `Unsafe.ALTER_TABLE_RENAME` #26

Closed tony closed 2 years ago

tony commented 2 years ago

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: