带有烧瓶、sqlalchemy 和 sqlite 的 Openshift 应用程序-数据库还原问题

Openshift app with flask, sqlalchemy and sqlite - problems with database reverting

我遇到的问题与此非常相似: How to preserve a SQLite database from being reverted after deploying to 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

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()

文件结构:http://sv.tinypic.com/r/121xseh/8(无法附加图片..)

通过 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."

您可以保持现有项目结构不变,只需使用部署挂钩将数据库移动到持久存储。

创建部署操作挂钩(可执行文件).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

正如另一个人指出的那样,还要确保您确实 committing/pushing 您的数据库(确保您的数据库不包含在您的 .gitignore 中)。