postgresql - Make Migration Conditional in Django -


the concrete use case is:

a django module wants create extension during migration. if db user used run migration not superuser, fails.

there several ways solve this, 1 way being (hypothetically) check in migration file whether extension installed , run sql code if not installed.

after research, however, not seem django's runsql can return results , subsequently excluding operations depending on result of previous operation not possible. there other ways achieve this? (e.g. subclassing runsql?)

any solution based on django migrations, django settings or postgres internal (one sql statement achieves run create extension if condition true) fine.

(note mentioned django-pgcrypto-fields illustration. i'm interested know whether such solution exists in general.)

edit in answer anentropic's comment: solution has work when running test or jenkins commands. means, manually calling --fake-initial or similar avoid running migration not option. if can explain how make test fake migrations, welcome.

my current solution add following settings:

migration_modules = {     'pgcrypto_fields': 'do-not-run-migrations' } 

but disable of migrations in question , not offending one. in case, might work, see lucky , ugly work around.

actually, django's hstoreextension run if necessary. of course, still requires superuser access. pgcrypto, class analogous hstoreextension created.

the setup making run in tests still requires superuser access, settings have this:

installed_apps = (     ...     'django.contrib.postgres',     ... )  if sys.argv[1] in ['test', 'jenkins']:     # install extension hstore (requires superuser)     installed_apps = ('test_init',) + installed_apps      # superuser     databases["default"]["user"] = 'xxx'     databases["default"]["password"] = 'xxx' 

and 'test_init' django app contains required __init.py__ , migrations module following file 0001_initial.py:

# -*- coding: utf-8 -*- __future__ import unicode_literals django.contrib.postgres.operations import hstoreextension  django.db import migrations   class migration(migrations.migration):      run_before = [         ('some_app_that_requires_hstore', '0001_initial'),     ]      operations = [         hstoreextension(),     ] 

it not required check test mode (via sys.argv) make migration then. mentioned above, hstoreextension silent errors. if run migrate on production without superuser not fail, no matter if hstore installed yet. see source code.

see django's documentation of hstorefield.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -