Skip to content

Commit

Permalink
Moving basil.db to db/sqlite3 to avoid issues with db/models updating…
Browse files Browse the repository at this point in the history
… BASIL to new versions. Refers to #62

Signed-off-by: Luigi Pellecchia <[email protected]>
  • Loading branch information
Luigi Pellecchia committed Jan 17, 2025
1 parent 721faf6 commit 06e54b9
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
9 changes: 5 additions & 4 deletions db/db_orm.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import os.path
import os

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine


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()
4 changes: 1 addition & 3 deletions db/models/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion docs/source/e2e_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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**


-----------
Expand Down
2 changes: 1 addition & 1 deletion docs/source/how_to_run_it.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 </YOUR/LOCAL/LOCATION>
docker cp basil-api-container:/BASIL-API/db/sqlite3/basil.db </YOUR/LOCAL/LOCATION>
# Stop Containers
^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 06e54b9

Please sign in to comment.