Make restored migrations 0003-0005 skip existing columns

Installations that applied 0003-0005 before they were removed from the repository still have the columns, but may no longer have the django_migrations rows. Running migrate then failed with DuplicateColumn on "provider". Use an AddField variant that only adds missing columns.

Verified on NetBox 4.7.2 / PostgreSQL 17 / Python 3.13 with a fresh install, with all columns present but no migration rows, and with only some columns present. Bump version to 0.3.3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-10-14 16:37:56 +02:00
1 parent edba20a723
commit a7d27336b6
7 files changed
+45 -10

No files matched your search

@@ -0,0 +1,23 @@
from django.db import migrations
class AddFieldIfMissing(migrations.AddField):
"""
AddField that leaves an already existing column alone.
Migrations 0003-0005 were temporarily removed from the repository. Installations that
applied them before that still have the columns, but may no longer have the matching
django_migrations rows, so a plain AddField would fail with DuplicateColumn.
"""
def database_forwards(self, app_label, schema_editor, from_state, to_state):
model = to_state.apps.get_model(app_label, self.model_name)
column = model._meta.get_field(self.name).column
connection = schema_editor.connection
with connection.cursor() as cursor:
existing = {
col.name for col in connection.introspection.get_table_description(cursor, model._meta.db_table)
}
if column in existing:
return
super().database_forwards(app_label, schema_editor, from_state, to_state)