1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Use relative db dir from .env

This commit is contained in:
Mo 2022-05-07 16:48:28 +02:00
parent b5917f85eb
commit 3b07efd1af
4 changed files with 21 additions and 13 deletions

View file

@ -3,7 +3,7 @@ SECRET_KEY=
SECURITY_PASSWORD_SALT= SECURITY_PASSWORD_SALT=
# Database # Database
SQLALCHEMY_DATABASE_URI=sqlite:///../db/advLab.db RELATIVE_DB_DIR=db
# Server URL # Server URL
SERVER_NAME= SERVER_NAME=

View file

@ -1,4 +1,4 @@
from os import environ from os import environ, makedirs
from flask import Flask from flask import Flask
from flask_admin import Admin from flask_admin import Admin
@ -28,7 +28,9 @@ set_from_env(app, "SERVER_NAME")
set_from_env(app, "SECRET_KEY") set_from_env(app, "SECRET_KEY")
set_from_env(app, "SECURITY_PASSWORD_SALT") set_from_env(app, "SECURITY_PASSWORD_SALT")
set_from_env(app, "SQLALCHEMY_DATABASE_URI") app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///../{environ['RELATIVE_DB_DIR']}/advlab.db"
makedirs(environ["RELATIVE_DB_DIR"], exist_ok=True)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["FLASK_ADMIN_FLUID_LAYOUT"] = True app.config["FLASK_ADMIN_FLUID_LAYOUT"] = True

View file

@ -1,4 +1,6 @@
from datetime import datetime from datetime import datetime
from os import environ
from pathlib import Path
from shutil import copy2 from shutil import copy2
from flask import flash, has_request_context from flask import flash, has_request_context
@ -20,6 +22,16 @@ from advlabdb.models import (
User, User,
) )
relative_db_dir = Path(environ["RELATIVE_DB_DIR"])
relative_db_path = relative_db_dir / "adblab.db"
relative_db_bk_dir = relative_db_dir / "backups"
relative_db_bk_dir.mkdir(exist_ok=True)
def now():
return datetime.now().strftime("%d_%m_%Y_%H_%M_%S")
def importFromFile(filePath): def importFromFile(filePath):
if filePath[-4:] != ".txt": if filePath[-4:] != ".txt":
@ -259,18 +271,15 @@ def importFromFile(filePath):
db.session.add(dbAppointment) db.session.add(dbAppointment)
# Backup # Backup
# TODO: Check existing dirs dest = relative_db_bk_dir / f"before_{dbSemester.repr()}_import_{now()}.db"
src = "db/advLab.db" copy2(relative_db_path, dest)
dest = f"db/backups/before_{dbSemester.repr()}_import_{datetime.now().strftime('%d_%m_%Y_%H_%M_%S')}.db"
copy2(src, dest)
show(f"Made a backup of the database before the import at {dest}") show(f"Made a backup of the database before the import at {dest}")
# Auto commit from the transaction context # Auto commit from the transaction context
src = "db/advLab.db" dest = relative_db_bk_dir / f"after_{dbSemester.repr()}_import_{now()}.db"
dest = f"db/backups/after_{dbSemester.repr()}_import_{datetime.now().strftime('%d_%m_%Y_%H_%M_%S')}.db" copy2(relative_db_path, dest)
copy2(src, dest)
show(f"Made a backup of the database after the import at {dest}") show(f"Made a backup of the database after the import at {dest}")

View file

@ -1,13 +1,10 @@
from datetime import date from datetime import date
from os import makedirs
from flask_security import hash_password from flask_security import hash_password
from advlabdb import app, db, user_datastore from advlabdb import app, db, user_datastore
from advlabdb.models import * from advlabdb.models import *
makedirs("db", exist_ok=True)
with app.app_context(): with app.app_context():
db.drop_all() db.drop_all()
db.create_all() db.create_all()