2

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.

toto_tico
  • 17,977
  • 9
  • 97
  • 116

1 Answers1

2

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)
Nicolás
  • 297
  • 2
  • 5
  • Thanks, I tried the first one and it work for tables but not changed fields. In the latter case, it will drop complete tables that belongs to another plugin instead of just removing the column that the plugin added to an existent table. – toto_tico Jan 20 '14 at 02:59
  • I have South installed. I ll see if I can come up with something. Thanks – toto_tico Jan 20 '14 at 03:01
  • 1
    aah!!, ok, for Fields modification you need have South Installed. for example if you add or delete a field in any model of an app named 'Totos', you need migrate the new changes: >> python manage.py schemamigration Totos --auto >> python manage.py migrate Totos – Nicolás Jan 20 '14 at 03:08
  • Please..for more information ..see the South Documentation: http://south.readthedocs.org/en/latest/ Django will integrate the schemamigrations in the next version (1.7). – Nicolás Jan 20 '14 at 03:12
  • The problem is that I didn't add those fields. The fields where added by the application I install. For example, the table `totos` existed because `app1` created when I installed. Then, I installed `app2` which added the column/field `tico` to the table `totos`. The problem is that when I remove the `app2` but the column `tico` is still there. Moreover `tico` was created as `not null`, so I kept getting an error that I fixed manually. Unfortunately it is more than one application that I installed. – toto_tico Jan 20 '14 at 03:58