home / tils / til

Menu
  • GraphQL API

til: django_migrations-runsql-noop.md

This data as json

path topic title url body html shot created created_utc updated updated_utc shot_hash slug
django_migrations-runsql-noop.md django migrations.RunSQL.noop for reversible SQL migrations https://github.com/simonw/til/blob/main/django/migrations-runsql-noop.md `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, ) ] ``` <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> <Binary: 51,466 bytes> 2021-05-02T10:48:46-07:00 2021-05-02T17:48:46+00:00 2021-05-02T10:48:46-07:00 2021-05-02T17:48:46+00:00 03f66f89626893e5a1a0374109ec84e8 migrations-runsql-noop
Powered by Datasette · How this site works · Code of conduct