Commit 70e003f0 authored by Konstantin Schulz's avatar Konstantin Schulz

recreate database tables only if necessary

parent a57fd5d5
Pipeline #11603 passed with stages
in 3 minutes and 39 seconds
......@@ -5,7 +5,7 @@ import sys
from logging.handlers import RotatingFileHandler
from threading import Thread
from time import strftime
from typing import Type
from typing import Type, List
import connexion
import flask
from connexion import FlaskApp
......@@ -91,7 +91,10 @@ def init_app_common(cfg: Type[Config] = Config, is_csm: bool = False) -> Flask:
if is_csm:
from mcserver.app.services.databaseService import DatabaseService
DatabaseService.init_db_alembic()
db.create_all()
tables: List[str] = [Config.DATABASE_TABLE_ALEMBIC, Config.DATABASE_TABLE_CORPUS, Config.DATABASE_TABLE_EXERCISE,
Config.DATABASE_TABLE_UPDATEINFO]
if any(not db.engine.dialect.has_table(db.engine, x) for x in tables):
db.create_all()
from mcserver.app.services.textService import TextService
TextService.init_proper_nouns_list()
TextService.init_stop_words_latin()
......
......@@ -42,7 +42,7 @@ class DatabaseService:
@staticmethod
def init_db_corpus() -> None:
"""Initializes the corpus list if it is not already there and up to date."""
if db.engine.dialect.has_table(db.engine, "Corpus"):
if db.engine.dialect.has_table(db.engine, Config.DATABASE_TABLE_CORPUS):
CorpusService.existing_corpora = db.session.query(Corpus).all()
db.session.commit()
urn_dict: Dict[str, int] = {v.source_urn: i for i, v in enumerate(CorpusService.existing_corpora)}
......@@ -66,7 +66,7 @@ class DatabaseService:
@staticmethod
def init_db_update_info() -> None:
"""Initializes update entries for all resources that have not yet been created."""
if db.engine.dialect.has_table(db.engine, "UpdateInfo"):
if db.engine.dialect.has_table(db.engine, Config.DATABASE_TABLE_UPDATEINFO):
for rt in ResourceType:
ui_cts: UpdateInfo = db.session.query(UpdateInfo).filter_by(resource_type=rt.name).first()
if ui_cts is None:
......@@ -93,7 +93,7 @@ class DatabaseService:
@staticmethod
def update_exercises(is_csm: bool) -> None:
"""Deletes old exercises."""
if db.engine.dialect.has_table(db.engine, "Exercise"):
if db.engine.dialect.has_table(db.engine, Config.DATABASE_TABLE_EXERCISE):
exercises: List[Exercise] = db.session.query(Exercise).all()
now: datetime = datetime.utcnow()
for exercise in exercises:
......
......@@ -58,6 +58,9 @@ class Config(object):
CUSTOM_CORPUS_VIVA_URN = "urn:custom:latinLit:viva.lat"
CUSTOM_CORPUS_PROIEL_URN_TEMPLATE = "urn:custom:latinLit:proiel.{0}.lat"
DATABASE_TABLE_ALEMBIC = "alembic_version"
DATABASE_TABLE_CORPUS = "Corpus"
DATABASE_TABLE_EXERCISE = "Exercise"
DATABASE_TABLE_UPDATEINFO = "UpdateInfo"
DATABASE_URL_DOCKER = "postgresql://postgres@db:5432/"
DATABASE_URL_LOCAL = "postgresql://postgres@0.0.0.0:5432/postgres"
DATABASE_URL_SQLITE = f"sqlite:///{os.path.join(basedir, 'app.db')}"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment