From 7a911b3a3d6d3cdd6384e0064f8c268cb15f19eb Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Wed, 14 Aug 2019 14:49:23 +0200 Subject: [PATCH] =?UTF-8?q?Nukedb:=20p=C5=99=C3=ADkaz,=20kter=C3=BD=20v?= =?UTF-8?q?=C5=A1echny=20aplikace=20odmigruje?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Uvede tedy databázi do naprosto čistého stavu, bez tabulek ap. Není závislá na databázi (narozdíl od 'rm db-local.sqlite3') a nepotřebuje práva vytvářet nové databáze (narozdíl od 'DROP DATABASE' nebo './manage.py reset_db' nad Postgresem) Jinak je to prostě for-cyklus přes INSTALLED_APPS. --- seminar/management/commands/nukedb.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 seminar/management/commands/nukedb.py diff --git a/seminar/management/commands/nukedb.py b/seminar/management/commands/nukedb.py new file mode 100644 index 00000000..f253841a --- /dev/null +++ b/seminar/management/commands/nukedb.py @@ -0,0 +1,20 @@ +from mamweb.settings import INSTALLED_APPS +from django.core.management.base import BaseCommand, CommandError +from django.core.management import call_command + +class Command(BaseCommand): + help = "Odmigruje všechny moduly (i.e. smaže všechny tabulky, ale databázi nechá)" + + def add_arguments(self, parser): + # TODO: --force (makat a neblábolit) + pass + def handle(self, *args, **options): + # TODO: zeptat se + for app in INSTALLED_APPS: + app = app.split('.')[-1] + try: + call_command('migrate', app, 'zero') + except CommandError: + # app nemá migrace (aspoň typicky) + pass + call_command('showmigrations')