Switch to Postgres database

This commit is contained in:
Michał Gdula 2023-05-04 17:14:41 +03:00
parent fda3fcc28c
commit 5248ae0123
6 changed files with 67 additions and 19 deletions

View file

@ -1,10 +1,21 @@
import os
SECRET_KEY = os.getenv('SECRET_KEY')
BEARER_TOKEN = os.getenv('BEARER_TOKEN')
# Purely to make the code a bit more readable
def env(key):
return os.getenv(key)
SQLALCHEMY_DATABASE_URI = 'sqlite:///db.sqlite'
SECRET_KEY = env('SECRET_KEY')
BEARER_TOKEN = env('BEARER_TOKEN')
user = env('DB_USER')
password = env('DB_PASSWORD')
host = env('DB_HOST')
port = env('DB_PORT')
database = env('DB_NAME')
SQLALCHEMY_DATABASE_URI = f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}"
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_POOL_RECYCLE = 621
MIGRATION_DIR = '/data/storage/migrations'
INSTANCE_DIR = '/data/storage/instance'

View file

@ -0,0 +1,19 @@
#!/bin/sh
until pg_isready -d $DB_NAME -h $DB_HOST -p $DB_PORT -U $DB_USER
do
echo "Waiting for database to start... (5s)"
sleep 5
done
echo "Database is ready! Creating tables..."
flask --app server db init
if [ -n flask --app server db check ]; then
echo "Database changes detected! Migrating..."
flask --app server db migrate
flask --app server db upgrade
fi
echo "Starting server..."
gunicorn --bind highscore:8080 server:app

View file

@ -1,7 +1,5 @@
import os
from flask import Flask
from flask_migrate import init as migrate_init
from extensions import db, migrate, cache
from config import MIGRATION_DIR, INSTANCE_DIR
from views import blueprint
@ -13,12 +11,7 @@ db.init_app(app)
migrate.init_app(app, db, directory=MIGRATION_DIR)
cache.init_app(app)
if not os.path.exists(os.path.join(INSTANCE_DIR, 'db.sqlite')):
with app.app_context():
db.create_all()
if not os.path.exists(MIGRATION_DIR):
with app.app_context():
migrate_init(directory=MIGRATION_DIR)
with app.app_context():
db.create_all()
app.register_blueprint(blueprint)