toto_tico
toto_tico

Reputation: 19027

How to clean Django database after install and uninstall packages?

I just learned that I should be more careful after installing any package in Django. This is I should dump my database (or simply copy the the database file) before migrating or syncdb it when I install a new plugin.

Now, it is to late. I have installed and removed several packages (pip install package and python manage.py migrate) and I would like to clean or purge my database, so I get rid of tables and fields no longer used.

Is there any way of search and/or remove fields and tables that are no longer in use by the INSTALLED_APPS?

It seems possible to iterate over all the INSTALLED_APPS models.py files and compare them with the current state of the database. Another option would be to generate a completely new database and compare it with the old one.

Upvotes: 2

Views: 1569

Answers (1)

Nicolás
Nicolás

Reputation: 297

I think you have 2 options:

1 - Use the sqlclear django command.

    $ python manage.py sqlclear appToUninstall > droppingApp.sql

   BEGIN;

   DROP TABLE "appToUninstall_table";

   ...

   COMMIT;

and execute the query manually in your database.

2 - Maybe check the South API and create your own Django command

from south.db import db

...

db.delete_table(Table, cascade=True)

Upvotes: 2

Related Questions