Skip to content

Commit 827284c

Browse files
committed
sql alchemy replacing flask sql alchemy
1 parent 54d1c97 commit 827284c

7 files changed

Lines changed: 49 additions & 80 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "OpenGeodeWeb-Microservice"
7-
version = "0.0.0"
7+
version = "1.0.0"
88
dynamic = ["dependencies"]
99
authors = [{ name = "Geode-solutions", email = "team-web@geode-solutions.com" }]
1010
description = "Database model and ORM layer for OpenGeodeWeb ecosystem"

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
SQLAlchemy==2.0.43
2-
Flask-SQLAlchemy==3.1.1
32
fastjsonschema==2.21.1
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
"""Database connection management"""
22

3-
from typing import Optional
4-
from sqlalchemy.orm import scoped_session
5-
from flask import Flask
6-
from flask_sqlalchemy import SQLAlchemy
7-
from flask_sqlalchemy.session import Session
3+
from sqlalchemy import create_engine
4+
from sqlalchemy.orm import sessionmaker, scoped_session
85
from .base import Base
96

107
DATABASE_FILENAME = "project.db"
11-
database: Optional[SQLAlchemy] = None
128

9+
engine = None
10+
session_factory = None
11+
scoped_session_registry = None
1312

14-
def init_database(app: Flask, db_filename: str = DATABASE_FILENAME) -> SQLAlchemy:
15-
global database
16-
if database is None:
17-
database = SQLAlchemy(model_class=Base)
18-
database.init_app(app)
19-
with app.app_context():
20-
database.create_all()
21-
return database
2213

14+
def init_database(db_path: str = DATABASE_FILENAME) -> None:
15+
global engine, session_factory, scoped_session_registry
2316

24-
def get_database() -> Optional[SQLAlchemy]:
25-
return database
17+
if engine is None:
18+
engine = create_engine(
19+
f"sqlite:///{db_path}", connect_args={"check_same_thread": False}
20+
)
21+
session_factory = sessionmaker(bind=engine)
22+
scoped_session_registry = scoped_session(session_factory)
23+
Base.metadata.create_all(engine)
2624

2725

28-
def get_session() -> Optional[scoped_session[Session]]:
29-
return database.session if database else None
26+
def get_session():
27+
if scoped_session_registry is None:
28+
raise RuntimeError()
29+
return scoped_session_registry

src/opengeodeweb_microservice/database/data.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def create(
2525
input_file: str | None = None,
2626
additional_files: list[str] | None = None,
2727
) -> "Data":
28-
input_file = input_file if input_file is not None else ""
29-
additional_files = additional_files if additional_files is not None else []
28+
input_file = input_file or ""
29+
additional_files = additional_files or []
3030

3131
data_entry = Data(
3232
geode_object=geode_object,
@@ -38,15 +38,12 @@ def create(
3838
)
3939

4040
session = get_session()
41-
if session:
42-
session.add(data_entry)
43-
session.flush()
44-
session.commit()
41+
session.add(data_entry)
42+
session.flush()
43+
session.commit()
4544
return data_entry
4645

4746
@staticmethod
4847
def get(data_id: str) -> "Data | None":
4948
session = get_session()
50-
if session:
51-
return session.get(Data, data_id)
52-
return None
49+
return session.get(Data, data_id)

tests/conftest.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,40 @@
11
import os
22
import pytest
3-
from flask import Flask
43
from src.opengeodeweb_microservice.database.connection import init_database, get_session
54
from src.opengeodeweb_microservice.database.data import Data
65

76

8-
@pytest.fixture(scope="session")
9-
def app():
10-
app = Flask(__name__)
11-
app.config.update(
12-
{
13-
"TESTING": True,
14-
"SQLALCHEMY_DATABASE_URI": f"sqlite:///{os.path.join(os.path.dirname(__file__), 'test_project.db')}",
15-
"SQLALCHEMY_TRACK_MODIFICATIONS": False,
16-
}
17-
)
18-
with app.app_context():
19-
init_database(app, "test_project.db")
20-
yield app
21-
_cleanup_database()
7+
DB_PATH = os.path.join(os.path.dirname(__file__), "test_project.db")
228

239

24-
def _cleanup_database():
10+
@pytest.fixture(scope="session", autouse=True)
11+
def setup_database():
12+
init_database(DB_PATH)
13+
yield
14+
_cleanup_database(DB_PATH)
15+
16+
17+
def _cleanup_database(db_path: str):
2518
try:
2619
session = get_session()
27-
if session:
28-
session.close()
20+
session.close()
2921
except Exception:
3022
pass
31-
db_path = os.path.join(os.path.dirname(__file__), "test_project.db")
23+
3224
if os.path.exists(db_path):
3325
try:
3426
os.remove(db_path)
3527
except PermissionError:
3628
pass
3729

3830

39-
@pytest.fixture
40-
def app_context(app):
41-
with app.app_context():
42-
yield
43-
44-
45-
@pytest.fixture
46-
def clean_database(app_context):
31+
@pytest.fixture(autouse=True)
32+
def clean_database():
4733
session = get_session()
48-
if session:
49-
session.query(Data).delete()
50-
session.commit()
34+
session.query(Data).delete()
35+
session.commit()
5136
yield
52-
if session:
53-
try:
54-
session.rollback()
55-
except Exception:
56-
pass
37+
try:
38+
session.rollback()
39+
except Exception:
40+
pass

tests/test_connection.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/test_database.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
from src.opengeodeweb_microservice.database.connection import get_session
21
from src.opengeodeweb_microservice.database.data import Data
32

43

5-
def test_data_crud_operations():
4+
def test_data_crud_operations(clean_database):
65
data = Data.create(geode_object="test_object", input_file="test.txt")
76
assert data.id is not None
8-
session = get_session()
9-
session.commit()
7+
108
retrieved = Data.get(data.id)
119
assert retrieved is not None
1210
assert retrieved.geode_object == "test_object"
1311
non_existent = Data.get("fake_id")
1412
assert non_existent is None
1513

1614

17-
def test_data_with_additional_files():
15+
def test_data_with_additional_files(clean_database):
1816
files = ["file1.txt", "file2.txt"]
1917
data = Data.create(geode_object="test_files", additional_files=files)
20-
session = get_session()
21-
session.commit()
18+
2219
retrieved = Data.get(data.id)
20+
assert retrieved is not None
2321
assert retrieved.additional_files == files

0 commit comments

Comments
 (0)