I try to write a script that will reset and reinitialize the database for a new django application. In order to detect any error I want to check the return code of each command.
#! /bin/env python
import sys, os
def execute⌘:
print(cmd)
ret = os.system(cmd)
if not ret:
sys.exit("Last command failed")
if __name__ == "__main__":
if os.path.isfile('app.sqlite'):
os.unlink('app.sqlite')
execute('python manage.py syncdb --noinput --all') # << this fails
execute('python manage.py migrate --noinput --all')
My problem is that I wasn't able to find a way to safely re-initialize the database. Running migrate fails because it requires syncdb and syncdb fails because it requires migrate.
Do not ask me to ignore the return codes from the commands, I want a solution that is able to properly deal with error codes.