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 ...@@ -5,7 +5,7 @@ import sys
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
from threading import Thread from threading import Thread
from time import strftime from time import strftime
from typing import Type from typing import Type, List
import connexion import connexion
import flask import flask
from connexion import FlaskApp from connexion import FlaskApp
...@@ -91,7 +91,10 @@ def init_app_common(cfg: Type[Config] = Config, is_csm: bool = False) -> Flask: ...@@ -91,7 +91,10 @@ def init_app_common(cfg: Type[Config] = Config, is_csm: bool = False) -> Flask:
if is_csm: if is_csm:
from mcserver.app.services.databaseService import DatabaseService from mcserver.app.services.databaseService import DatabaseService
DatabaseService.init_db_alembic() 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 from mcserver.app.services.textService import TextService
TextService.init_proper_nouns_list() TextService.init_proper_nouns_list()
TextService.init_stop_words_latin() TextService.init_stop_words_latin()
......
...@@ -42,7 +42,7 @@ class DatabaseService: ...@@ -42,7 +42,7 @@ class DatabaseService:
@staticmethod @staticmethod
def init_db_corpus() -> None: def init_db_corpus() -> None:
"""Initializes the corpus list if it is not already there and up to date.""" """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() CorpusService.existing_corpora = db.session.query(Corpus).all()
db.session.commit() db.session.commit()
urn_dict: Dict[str, int] = {v.source_urn: i for i, v in enumerate(CorpusService.existing_corpora)} urn_dict: Dict[str, int] = {v.source_urn: i for i, v in enumerate(CorpusService.existing_corpora)}
...@@ -66,7 +66,7 @@ class DatabaseService: ...@@ -66,7 +66,7 @@ class DatabaseService:
@staticmethod @staticmethod
def init_db_update_info() -> None: def init_db_update_info() -> None:
"""Initializes update entries for all resources that have not yet been created.""" """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: for rt in ResourceType:
ui_cts: UpdateInfo = db.session.query(UpdateInfo).filter_by(resource_type=rt.name).first() ui_cts: UpdateInfo = db.session.query(UpdateInfo).filter_by(resource_type=rt.name).first()
if ui_cts is None: if ui_cts is None:
...@@ -93,7 +93,7 @@ class DatabaseService: ...@@ -93,7 +93,7 @@ class DatabaseService:
@staticmethod @staticmethod
def update_exercises(is_csm: bool) -> None: def update_exercises(is_csm: bool) -> None:
"""Deletes old exercises.""" """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() exercises: List[Exercise] = db.session.query(Exercise).all()
now: datetime = datetime.utcnow() now: datetime = datetime.utcnow()
for exercise in exercises: for exercise in exercises:
......
...@@ -58,6 +58,9 @@ class Config(object): ...@@ -58,6 +58,9 @@ class Config(object):
CUSTOM_CORPUS_VIVA_URN = "urn:custom:latinLit:viva.lat" CUSTOM_CORPUS_VIVA_URN = "urn:custom:latinLit:viva.lat"
CUSTOM_CORPUS_PROIEL_URN_TEMPLATE = "urn:custom:latinLit:proiel.{0}.lat" CUSTOM_CORPUS_PROIEL_URN_TEMPLATE = "urn:custom:latinLit:proiel.{0}.lat"
DATABASE_TABLE_ALEMBIC = "alembic_version" 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_DOCKER = "postgresql://postgres@db:5432/"
DATABASE_URL_LOCAL = "postgresql://postgres@0.0.0.0:5432/postgres" DATABASE_URL_LOCAL = "postgresql://postgres@0.0.0.0:5432/postgres"
DATABASE_URL_SQLITE = f"sqlite:///{os.path.join(basedir, 'app.db')}" 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