sb32134
sb32134

Reputation: 430

how to initialize PostgreSQL database in openshift only once

Inorder to deploy my flask postgresql based app, i want to initialize my database with few items. Hence i created a manage.py for this. This is because i want to enter item into database only once. After that, i want to go as usual(git push my project). My project works fine, problem is i don't know where should i place my manage.py and how do i use it as an admin or make sure it just run once. I use manage.py as python manage.py create_tables.

this is my wsgi folder:

├── application
├── app.py
├── auth.py
├── flaskapp.cfg
├── forms.py
├── main.py
├── models.py
├── result.json
├── templates
│   ├── base.html
│   ├── evaluate.html
│   └── show_all.html
└── views.py

and this is my project folder :

manage.py  readme.md  requirements.txt  setup.py  venv  wsgi  wsgi.py

Obviously, i want to initialize database in such a way that everytime i change code and push, i don't want to re-initialize the database with same entries but only when i want to and also, as an admin.

This is my application file looks like :

#!/usr/bin/python
import os

virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
os.environ['PYTHON_EGG_CACHE'] = os.path.join(virtenv, 'lib/python2.7/site-packages')
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except IOError:
    pass
#
# IMPORTANT: Put any additional includes below this line.  If placed above this
# line, it's possible required libraries won't be in your searchable path
# 

from main import app as application
from main import *
db.create_all()

The application file in openshift isn't documented well and its not clear as to what's the right place to do one time database initialization.

Upvotes: 0

Views: 900

Answers (1)

slim_chebbi
slim_chebbi

Reputation: 818

Place this code under .openshift/action_hooks in deploy file:

source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate
python "$OPENSHIFT_REPO_DIR"wsgi/manage.py

This script will run manage.py every time that you deploy your app.

Upvotes: 2

Related Questions