diff --git a/mc_backend/mcserver/app/__init__.py b/mc_backend/mcserver/app/__init__.py index 9445aaaecab50e2d6a44add58ef529490cd27c87..748933bce6ddf2d4884a391b4e84d0c4c95ede93 100644 --- a/mc_backend/mcserver/app/__init__.py +++ b/mc_backend/mcserver/app/__init__.py @@ -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() diff --git a/mc_backend/mcserver/app/services/databaseService.py b/mc_backend/mcserver/app/services/databaseService.py index 654fd5ac4481a1d43e02af98dec84dec6b3f6fe6..d18b2a66c57e0bd6248a760f8c2cfdc03cba55c1 100644 --- a/mc_backend/mcserver/app/services/databaseService.py +++ b/mc_backend/mcserver/app/services/databaseService.py @@ -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: diff --git a/mc_backend/mcserver/config.py b/mc_backend/mcserver/config.py index 898040310f8ccd550a9b1596ab92bcc844cc2be1..96397ab5849afd76782eb08c910b3f4e54afc48a 100644 --- a/mc_backend/mcserver/config.py +++ b/mc_backend/mcserver/config.py @@ -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')}"