Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize django database when using django and south

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.

like image 979
bogdan Avatar asked Dec 19 '25 21:12

bogdan


1 Answers

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")
like image 84
dlamotte Avatar answered Dec 23 '25 15:12

dlamotte