72 words
1 minute
migrations.RunSQL.noop for reversible SQL migrations
Anubhav Gain
2024-03-12

migrations.RunSQL.noop for reversible SQL migrations#

migrations.RunSQL.noop provides an easy way to create “reversible” Django SQL migrations, where the reverse operation does nothing (but keeps it possible to reverse back to a previous migration state without being blocked by an irreversible migration).

from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("app", "0114_last_migration"),
]
operations = [
migrations.RunSQL(
sql="""
update concordance_identifier
set authority = replace(authority, ':', '_')
where authority like '%:%'
""",
reverse_sql=migrations.RunSQL.noop,
)
]
migrations.RunSQL.noop for reversible SQL migrations
https://mranv.pages.dev/posts/migrationsrunsqlnoop-for-reversible-sql-migrations/
Author
Anubhav Gain
Published at
2024-03-12
License
CC BY-NC-SA 4.0