I'm trying to reset my database while Django project's running but I'm having a trouble. Django is constantly interacting with the database due to his mapping between database and models.
I'm using the module reset_db as following :
from django.core import management
management.call_command('reset_db',router='default',interactive = False)
I'm using postgres and, when this command is running, I have this error :
OperationalError: database "XXX" is being accessed by other users
DETAIL: There are 1 other session(s) using the database.
I tried to drop the database from psycopg2 but I still have this problem. I also tried the brute force, launch an external script who's killing all connexions :
os.system("bash myscript.sh")
My script :
psql -U myuser -d mybase -c "select pg_terminate_backend(pg_stat_activity.procpid) from pg_stat_activity where pg_stat_activity.datname='mybase';"
./manage.py reset_db --router=default --noinput
This one worked. However, as I killed all connections to database, my application doesn't answer (which is logical).
I have to reset all my base from the application. For example, in my administration pannel (which is not the one from Django), I have a button "Reset data" and, when clicked, it has to delete all data, do a syncdb and migrate.
Does anyone know how can I do it ?
Thanks