Complete django DB reset

Viewed 24449

How do I completely reset my Django (1.2 alpha) DB (dropping all tables, rather than just clearing them)?

manage.py flush does too little (won't work if there are schema changes) and manage.py reset requires me to specify all apps (and appears to take a format that is different from just " ".join(INSTALLED_APPS)). I can obviously achieve this in a DB specific way, but I figured there must be a sane, DB backend agnostic way to do this.

[Edit: I'm looking for something that I can call from a script, e.g. a Makefile and that continues to work if I change the backend DB or add to settings.INSTALLED_APPS]

7 Answers

You want sqlreset:

% python manage.py help sqlreset
Usage: manage.py sqlreset [options] <appname appname ...>

Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s).

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=all output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  --version             show program's version number and exit
  -h, --help            show this help message and exit

Just like when you modify a model, Django will not automatically do this for you. It will only output the commands for you to copy and paste.

Just assign a new database and drop this db from the db console. Seems to me to be the simplest.

Hm, maybe you lie to manage.py, pretending to make fixtures, but only to look for apps:

apps=$(python manage.py makefixture 2>&1 | egrep -v '(^Error|^django)'|awk -F . '{print $2}'|uniq); for i in $apps; do python manage.py sqlreset $i; done| grep DROP

That prints out a list of DROP TABLE statements for all apps tables of your project, excluding django tables itself. If you want to include them, remove the |^django pattern vom egrep.

But how to feed the correct database backend? sed/awk-ing through settings.conf? Or better by utilizing a little settings.conf-reading python script itself.

In Django 3.1.1

if you want to totally reset the database and the schema then put this code in any app like this

app/management/commands/resetdb.py

import os
import glob
import shutil
from django.conf import settings
from django.core.management.base import BaseCommand
from django.db import connection


class Command(BaseCommand):
    help = 'Resets the database'

    def handle(self, *args, **options):
        dbname = settings.DATABASES["default"]["NAME"]
        with connection.cursor() as cursor:
            cursor.execute("DROP DATABASE %s" % dbname)
            cursor.execute("CREATE DATABASE %s" % dbname)

        base = str(settings.BASE_DIR)
        migrations = glob.glob(os.path.join(base, "*", "migrations"))

        for migration in migrations:
            shutil.rmtree(migration)

        apps = [migration.split("\\")[-2] for migration in migrations]
        for app in apps:
            os.system("python manage.py makemigrations %s" % app)
        os.system("python manage.py migrate")

now reset it using :

python manage.py resetdb

this will

  • drop the database
  • delete the migrations folders
  • make migrations
  • create a new db
  • migrate the fresh tables

take a look at reset command in django's code, and write your own which drops/creates DB first.

Related