与瓶,sqlalchemy和sqlite的Openshift应用程序 - 与数据库还原的问题 [英] Openshift app with flask, sqlalchemy and sqlite - problems with database reverting

查看:170
本文介绍了与瓶,sqlalchemy和sqlite的Openshift应用程序 - 与数据库还原的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题非常像这样:



我不明白他的意思回答完全清楚,不足以将其应用到我自己的应用程序,因为我不能评论他的答案(没有足够的代表)我想我不得不问我自己的问题。

问题是当推送我的本地文件(不包括数据库文件)时,openshift上的数据库变成了我本地的数据库(通过服务器进行的所有更改都被还原了)。

我已经使用了很多东西,并且非常了解数据库应该位于其他位置的问题,但是我无法完全掌握将其放置在何处以及如何将其部署在回购站之外。



编辑:快速解决方案:如果您遇到此问题,尝试使用 rhc ssh appname
连接到openshift应用程序,然后使用 cp app-root / repo / database.db app-root / data /database.db
,如果您将openshift数据目录作为对SQLALCHEMY_DATABASE_URI的引用。我推荐以下接受的答案!



我已经附加了我的文件结构,这里有一些相关的代码:

config.py

  import os 
basedir = os.path.abspath(os.path.dirname(__ file__))

SQLALCHEMY_DATABASE_URI ='sqlite:///'+ os.path.join(basedir,'database.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir,'db_repository')

app / __ init.py __

  from flask import Flask 
from flask.ext.sqlalchemy import SQLAlchemy
$ b $ app = Flask(__ name__)
#so那个瓶子没有吞下错误信息
app.config ['PROPAGATE_EXCEPTIONS'] = True
app.config.from_object('config')
db = SQLAlchemy(app)

from


$ b $ p $ $ $ $ $ $ $

 #!/ usr / bin / env python 
import os

virtenv = os.path.join(os.environ.get(' OPENSHIFT_PYTH ON_DIR','。'),'virtenv')


#重要提示:在此行下面添加任何附加内容。如果放置在
#行之上,可能需要的库不在您的可搜索路径中


从应用程序导入应用程序作为应用程序

##在本地运行服务器
if __name__ =='__main__':
from wsgiref.simple_server import make_server $ b $ httpd = make_server('localhost',4599,application)
httpd.serve_forever ()

filestructure: http://sv.tinypic.com/r/121xseh/8 (不能附上图片..)

解决方案

通过 OpenShift Cartridge Guide(OpenShift磁带盒指南):



磁带盒和永久存储:每当您推送时,远程磁带库目录中的所有内容都将被重新创建。一个sqlite数据库)在OpenShift数据目录中,这个数据目录之间会一直存在您可以通过环境变量$ OPENSHIFT_DATA_DIR找到OpenShift数据目录。



您可以保留原有的项目结构,只需使用deploy钩子将数据库移动到持久性存储中。
$ b

创建一个deploy动作钩子(可执行文件) .openshift / action_hooks / deploy
$ b $ $ $ $ $ $ $ $ $


$ b $这个部署钩子在依赖关系之后被执行解决了,并且
#build hook已经运行,但是在应用程序再次启动
#之前。

#如果这是初始安装,将数据库从repo复制到永久存储目录
if [! -f $ {OPENSHIFT_DATA_DIR} database.db];然后
cp -rf $ {OPENSHIFT_REPO_DIR} database.db $ {OPENSHIFT_DATA_DIR} /database.db 2> / dev / null
fi

#在回购期间从数据库中删除数据库如果[-d $ {OPENSHIFT_REPO_DIR} database.db]都将部署
;然后
rm -rf $ {OPENSHIFT_REPO_DIR} database.db
fi

#从repo目录创建符号链接到持久存储中的新数据库位置
ln -sf $ { OPENSHIFT_DATA_DIR} database.db $ {OPENSHIFT_REPO_DIR} database.db

另一位人士指出,你实际上正在提交/推送你的数据库(确保你的数据库不包含在你的.gitignore中)。

I have a problem pretty much exactly like this: How to preserve a SQLite database from being reverted after deploying to OpenShift?

I don't understand his answer fully and clearly not enough to apply it to my own app and since I can't comment his answer (not enough rep) I figured I had to make ask my own question.

Problem is that when pushing my local files (not including the database file) my database on openshift becomes the one I have locally (all changes made through the server are reverted).

I've googled alot and pretty much understand the problem being that the database should be located somewhere else but I can't grasp fully where to place it and how to deploy it if it's outside the repo.

EDIT: Quick solution: If you have this problem, try connecting to your openshift app with rhc ssh appname and then cp app-root/repo/database.db app-root/data/database.db if you have the openshift data dir as reference to SQLALCHEMY_DATABASE_URI. I recommend the accepted answer below though!

I've attached my filestructure and here's some related code:

config.py

import os
basedir = os.path.abspath(os.path.dirname(__file__))

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir,     'database.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')

app/__ init.py__

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
#so that flask doesn't swallow error messages
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config.from_object('config')
db = SQLAlchemy(app)

from app import rest_api, models

wsgi.py:

#!/usr/bin/env python
import os

virtenv = os.path.join(os.environ.get('OPENSHIFT_PYTHON_DIR', '.'), 'virtenv')

#
# 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 app import app as application

## runs server locally
if __name__ == '__main__':
    from wsgiref.simple_server import make_server
    httpd = make_server('localhost', 4599, application)
    httpd.serve_forever()

filestructure: http://sv.tinypic.com/r/121xseh/8 (can't attach image..)

解决方案

Via the note at the top of the OpenShift Cartridge Guide:

"Cartridges and Persistent Storage: Every time you push, everything in your remote repo directory is recreated. Store long term items (like an sqlite database) in the OpenShift data directory, which will persist between pushes of your repo. The OpenShift data directory can be found via the environment variable $OPENSHIFT_DATA_DIR."

You can keep your existing project structure as-is and just use a deploy hook to move your database to persistent storage.

Create a deploy action hook (executable file) .openshift/action_hooks/deploy:

#!/bin/bash

# This deploy hook gets executed after dependencies are resolved and the
# build hook has been run but before the application has been started back
# up again.

# if this is the initial install, copy DB from repo to persistent storage directory
if [ ! -f ${OPENSHIFT_DATA_DIR}database.db ]; then
  cp -rf ${OPENSHIFT_REPO_DIR}database.db ${OPENSHIFT_DATA_DIR}/database.db 2>/dev/null
fi

# remove the database from the repo during all deploys
if [ -d ${OPENSHIFT_REPO_DIR}database.db ]; then
  rm -rf ${OPENSHIFT_REPO_DIR}database.db
fi

# create symlink from repo directory to new database location in persistent storage
ln -sf ${OPENSHIFT_DATA_DIR}database.db ${OPENSHIFT_REPO_DIR}database.db

As another person pointed out, also make sure you are actually committing/pushing your database (make sure your database isn't included in your .gitignore).

这篇关于与瓶,sqlalchemy和sqlite的Openshift应用程序 - 与数据库还原的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆