From e7441d559d0e985c2dfa7bd568c97de5d243c193 Mon Sep 17 00:00:00 2001 From: Luigi <63158371+pellecchialuigi@users.noreply.github.com> Date: Fri, 17 Jan 2025 16:00:24 +0100 Subject: [PATCH] Moving basil.db to db/sqlite3 to avoid issues with db/models updating BASIL to new versions. Refers to #62 (#66) Signed-off-by: Luigi Pellecchia Co-authored-by: Luigi Pellecchia --- db/README.md | 4 ++-- db/db_orm.py | 9 +++++---- db/models/init_db.py | 4 +--- docs/source/e2e_testing.rst | 2 +- docs/source/how_to_run_it.rst | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/db/README.md b/db/README.md index 9f10f84..0fd82d1 100644 --- a/db/README.md +++ b/db/README.md @@ -13,14 +13,14 @@ It is possible to use a different type of database configuring the SQLAlchemy ** The database is not shipped with the source code because can be generated running a python script. -To generate the default empty **db/basil.db** database: +To generate the default empty **db/sqlite3/basil.db** database: ```sh # Move to the db/models directory cd db && cd models -# Initialize the sqlite database, you will find it in db/basil.db +# Initialize the sqlite database, you will find it in db/sqlite3/basil.db pdm run python3 init_db.py ``` diff --git a/db/db_orm.py b/db/db_orm.py index c04359d..1458b72 100644 --- a/db/db_orm.py +++ b/db/db_orm.py @@ -1,4 +1,4 @@ -import os.path +import os from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine @@ -6,11 +6,12 @@ class DbInterface(): - engine = None - session = None + DB_TYPE = "sqlite3" def __init__(self, db_name="basil.db"): currentdir = os.path.dirname(os.path.realpath(__file__)) - self.engine = create_engine(f"sqlite:///{currentdir}/{db_name}", echo=False) + if not os.path.exists(os.path.join(currentdir, self.DB_TYPE)): + os.mkdir(os.path.join(currentdir, self.DB_TYPE)) + self.engine = create_engine(f"sqlite:///{currentdir}/{self.DB_TYPE}/{db_name}", echo=False) Session = sessionmaker(bind=self.engine) self.session = Session() diff --git a/db/models/init_db.py b/db/models/init_db.py index 880448b..64ab3f1 100644 --- a/db/models/init_db.py +++ b/db/models/init_db.py @@ -42,10 +42,8 @@ def initialization(db_name='basil.db'): if os.path.exists(db_path): os.unlink(db_path) - engine = create_engine(f"sqlite:///{db_path}", echo=True) - Base.metadata.create_all(bind=engine) - dbi = db_orm.DbInterface(db_name) + Base.metadata.create_all(bind=dbi.engine) if os.getenv('BASIL_ADMIN_PASSWORD', '') != '': admin_count = dbi.session.query(UserModel).filter( diff --git a/docs/source/e2e_testing.rst b/docs/source/e2e_testing.rst index 4241c45..0e7c7a6 100644 --- a/docs/source/e2e_testing.rst +++ b/docs/source/e2e_testing.rst @@ -17,7 +17,7 @@ From BASIL project root directory: pdm run api/api.py --testing -That will create a test database **db/test.db** preventing the modification of your production db **db/basil.db** +That will create a test database **db/test.db** preventing the modification of your production db **db/sqlite3/basil.db** ----------- diff --git a/docs/source/how_to_run_it.rst b/docs/source/how_to_run_it.rst index d075824..228e244 100644 --- a/docs/source/how_to_run_it.rst +++ b/docs/source/how_to_run_it.rst @@ -149,7 +149,7 @@ You can copy the db file locally with the following docker command: .. code-block:: bash - docker cp basil-api-container:/BASIL-API/db/basil.db + docker cp basil-api-container:/BASIL-API/db/sqlite3/basil.db # Stop Containers ^^^^^^^^^^^^^^^^^