Search code examples
pythonherokuflaskflask-sqlalchemyheroku-toolbelt

Why can't I keep my articles published recently when I redeploy my flask app on heroku?


I write a simple script publish.py for publishing articles written in markdown. so i can use:

python3 publish.py -a https://MYNAME.herokuapp.com/publish -p article.md -t MYTOKEN

to upload my article and save it in my sqlite database test.db

my route function:

@main.route('/publish', methods=['GET', 'POST'])
def publish():
  if request.method == 'GET':
      abort(404)

  # authorization
  token = request.form.get('token', '')
  if token != current_app.config['TOKEN']:
      return 'invalid access token', 500

  title = request.form.get('title', None)
  if not title:
      return 'no title found', 500

  summary = request.form.get('summary', None)
  if not summary:
      return 'no summary found', 500

  content = request.form.get('content', None)
  if not content:
      return 'no content found', 500

  content = markdown2html(content)
  pub_time = request.form.get('pub_time', None)
  if pub_time:
      pub_time = datetime.strptime(pub_time, app.config['TIME_FORMAT'])

  tags = request.form.getlist('tags')

  create_article(title, summary, content, pub_time, tags)
  return '', 200

when I publish an artilce by the script it sames normal, my website can show this article. but when i push new changes on Heroku and restart it, my article published just now disppeared!

heroku maintenance:on
git push heroku master
heroku run python3 manage.py db upgrade
heroku restart
heroku maintenance:off 

How can i keep my old articles when i have to push new changes on Heroku. (and it's quite normal if i run my server on my localhost)


Solution

  • Let me guess, create_article writes the post to a file or to a SQLite database? Heroku does not keep data you store on disk, so you cannot use these ways of storing data.

    Instead, use a Postgres database. Heroku provides you one.