home / tils / til

Menu
  • GraphQL API

til: django_migrations-runsql-noop.md

Export data

til

This row where path = "django_migrations-runsql-noop.md"

TSV
Tab-separated values, for pasting into Excel or Google Sheets
CSV
Comma-separated values, for spreadsheets and other tools
  • JSON
    The row inside an object, with extra details
    View Download

path
django_migrations-runsql-noop.md
topic
django
title
migrations.RunSQL.noop for reversible SQL migrations
url
https://github.com/simonw/til/blob/main/django/migrations-runsql-noop.md
body
`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). ```python 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, ) ] ```
html
<p><code>migrations.RunSQL.noop</code> 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).</p> <div class="highlight highlight-source-python"><pre><span class="pl-k">from</span> <span class="pl-s1">django</span>.<span class="pl-s1">db</span> <span class="pl-k">import</span> <span class="pl-s1">migrations</span> <span class="pl-k">class</span> <span class="pl-v">Migration</span>(<span class="pl-s1">migrations</span>.<span class="pl-v">Migration</span>): <span class="pl-s1">dependencies</span> <span class="pl-c1">=</span> [ (<span class="pl-s">"app"</span>, <span class="pl-s">"0114_last_migration"</span>), ] <span class="pl-s1">operations</span> <span class="pl-c1">=</span> [ <span class="pl-s1">migrations</span>.<span class="pl-v">RunSQL</span>( <span class="pl-s1">sql</span><span class="pl-c1">=</span><span class="pl-s">"""</span> <span class="pl-s"> update concordance_identifier</span> <span class="pl-s"> set authority = replace(authority, ':', '_')</span> <span class="pl-s"> where authority like '%:%'</span> <span class="pl-s"> """</span>, <span class="pl-s1">reverse_sql</span><span class="pl-c1">=</span><span class="pl-s1">migrations</span>.<span class="pl-v">RunSQL</span>.<span class="pl-s1">noop</span>, ) ]</pre></div>
shot
<Binary: 51,466 bytes>
created
2021-05-02T10:48:46-07:00
created_utc
2021-05-02T17:48:46+00:00
updated
2021-05-02T10:48:46-07:00
updated_utc
2021-05-02T17:48:46+00:00
shot_hash
03f66f89626893e5a1a0374109ec84e8
slug
migrations-runsql-noop
Powered by Datasette · How this site works · Code of conduct