Skip to content

Instantly share code, notes, and snippets.

@NotSqrt
Forked from nealtodd/settings_test_snippet.py
Last active May 1, 2022 01:34
Show Gist options
  • Select an option

  • Save NotSqrt/5f3c76cd15e40ef62d09 to your computer and use it in GitHub Desktop.

Select an option

Save NotSqrt/5f3c76cd15e40ef62d09 to your computer and use it in GitHub Desktop.
Another shot at this problem ..
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return "notmigrations"
MIGRATION_MODULES = DisableMigrations()
@rchrd2

rchrd2 commented Dec 11, 2014

Copy link
Copy Markdown

This worked for me, and it is more simple than other solutions I came accross.

@danxshap

Copy link
Copy Markdown

Thank you for this!

@rikva

rikva commented Dec 17, 2014

Copy link
Copy Markdown

Perfect. Thanks!

@chrisbarmonde

Copy link
Copy Markdown

You are a patron saint of testing.

@runekaagaard

Copy link
Copy Markdown

One word: Ahhhhhhhhh

@jordij

jordij commented Feb 4, 2015

Copy link
Copy Markdown

boss!

@maximiliano

Copy link
Copy Markdown

Worked better for me, thanks!

@LegoStormtroopr

Copy link
Copy Markdown

+1 love it.

@LeMeteore

Copy link
Copy Markdown

Thanks a lot, +10000

@troygrosfield

Copy link
Copy Markdown

Just so everyone can support the publicly release app by @henriquebastos that includes this snippet:

Should rename the app to "headache reliever". Thanks guys! 👍

@mannysz

mannysz commented May 7, 2015

Copy link
Copy Markdown

Is there a way to use it with django-nose?

@NotSqrt

NotSqrt commented Jul 7, 2015

Copy link
Copy Markdown
Author

Glad I could help 😃

@adrienemery

Copy link
Copy Markdown

Has anyone used this with django-nose?

@flaugher

Copy link
Copy Markdown

Could someone please explain exactly how you use this snippet when running your tests? Thank you.

@chrisv2

chrisv2 commented Jan 23, 2016

Copy link
Copy Markdown

@flaugher: create a settings_test.py next to your regular settings.py:

import * from settings
# snippet content goes here

Then run your tests with settings_test instead of regular settings:

manage.py test --settings mysite.settings_test [test spec]

@nicholasserra

Copy link
Copy Markdown

Thanks!

@jamescw

jamescw commented Feb 25, 2016

Copy link
Copy Markdown

This is a great tool, but I use the hstore field on some models and the recommend way to install the hstore extension is via a migration: https://docs.djangoproject.com/es/1.9/ref/contrib/postgres/fields/#hstorefield

Any idea how I can run only one migration or run some custom SQL to install the extension?

@roblinton

Copy link
Copy Markdown

@jamescw,

Based on the same source as this app, I use something like this in my settings:

RUN_MODE = sys.argv[1] if len(sys.argv) > 1 else None

if RUN_MODE == 'test':
    class DisableMigrations(dict):
        except_apps = {'app_to_run_migrations_for'}
        def __contains__(self, item):
            return item not in self.except_apps
        def __getitem__(self, item):
            return super(DisableMigrations, self).__getitem__(item) if item in self.except_apps else None

    MIGRATION_MODULES = DisableMigrations()

@Remiz

Remiz commented Aug 3, 2016

Copy link
Copy Markdown

Not sure if someone else has the issue, but when I tried this snipped on my project that contains Cartridge, during the table creation it complains that one of the ManyToMany table already exists... Does anyone else had an issue with that?

@raprasad

Copy link
Copy Markdown

Thanks. Used this snippet with another scenario where there was need to re-create "unmanaged" database tables during tests:

https://gist.github.com/raprasad/f292f94657728de45d1614a741928308

@cjw296

cjw296 commented Oct 7, 2016

Copy link
Copy Markdown

So, how do you use this pattern when you want to test migrations as part of your test suite?

@renzon

renzon commented Jun 7, 2017

Copy link
Copy Markdown

For those who use pytest-django, it already supports commands to define db behaviour on tests:

http://pytest-django.readthedocs.io/en/latest/database.html

@cjw296

You can have a stage env with a different pytest.ini where you test migrations.

@tclancy

tclancy commented Oct 27, 2017

Copy link
Copy Markdown

Looks like you need to alter this slightly on Django 1.11:

def __getitem__(self, item):
        return None

@Jelle28

Jelle28 commented Jul 26, 2018

Copy link
Copy Markdown

Thanks, it still works 👍

@hmzeh

hmzeh commented Jan 11, 2020

Copy link
Copy Markdown

Hello,
Any help please
after running :- ./manage.py test --settings groundup.settings_test
Got this Error :- ModuleNotFoundError: No module named 'notmigrations'

Thanks

@mariuccio

mariuccio commented Jan 30, 2020

Copy link
Copy Markdown

If your Django version is >= 1.9

class DisableMigrations(object):
    def __contains__(self, item):
        return True

    def __getitem__(self, item):
        return None


MIGRATION_MODULES = DisableMigrations()

@bkane11

bkane11 commented Feb 4, 2021

Copy link
Copy Markdown

Awesome, thanks for this

@davidkell

davidkell commented Jul 26, 2021

Copy link
Copy Markdown

This is available in Django 3.1 onwards as a setting https://docs.djangoproject.com/en/3.1/ref/settings/#migrate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment