3

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.

1 Answer 1

2

You're using sys.exit() improperly. You could raise Exception("error message").

Also, an error message as to what you're seeing would be helpful to better answer your question.

Does:

./manage.py syncdb --migrate --noinput

solve your issue?

Perhaps you should be checking:

if ret != 0:
    raise Exception("error")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.