Skip to content

Commit 2d9b9eb

Browse files
committed
database
1 parent 61b9df0 commit 2d9b9eb

7 files changed

Lines changed: 93 additions & 2 deletions

File tree

build/lib/opengeodeweb_microservice/__init__.py

Whitespace-only changes.

build/lib/opengeodeweb_microservice/database/__init__.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Database connection management"""
2+
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
8+
from ..microservice.base import Base
9+
10+
DATABASE_FILENAME = "project.db"
11+
db: Optional[SQLAlchemy] = None
12+
13+
14+
def init_database(app: Flask, db_filename: str = DATABASE_FILENAME) -> SQLAlchemy:
15+
global db
16+
if db is None:
17+
db = SQLAlchemy(model_class=Base)
18+
db.init_app(app)
19+
with app.app_context():
20+
db.create_all()
21+
return db
22+
23+
24+
def get_database() -> Optional[SQLAlchemy]:
25+
return db
26+
27+
28+
get_database_connection = get_database
29+
30+
31+
def get_session() -> Optional[scoped_session[Session]]:
32+
return db.session if db else None

build/lib/opengeodeweb_microservice/microservice/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
from sqlalchemy.orm import DeclarativeBase
4+
5+
6+
class Base(DeclarativeBase):
7+
pass
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from sqlalchemy import String, JSON
2+
from sqlalchemy.orm import Mapped, mapped_column
3+
from ..database.connection import get_session
4+
from .base import Base
5+
import uuid
6+
7+
8+
class Data(Base):
9+
__tablename__ = "datas"
10+
11+
id: Mapped[str] = mapped_column(
12+
String, primary_key=True, default=lambda: str(uuid.uuid4()).replace("-", "")
13+
)
14+
native_file_name: Mapped[str] = mapped_column(String, nullable=False)
15+
viewable_file_name: Mapped[str] = mapped_column(String, nullable=False)
16+
geode_object: Mapped[str] = mapped_column(String, nullable=False)
17+
18+
light_viewable: Mapped[str | None] = mapped_column(String, nullable=True)
19+
input_file: Mapped[str | None] = mapped_column(String, nullable=True)
20+
additional_files: Mapped[list[str] | None] = mapped_column(JSON, nullable=True)
21+
22+
@staticmethod
23+
def create(
24+
geode_object: str,
25+
input_file: str | None = None,
26+
additional_files: list[str] | None = None,
27+
) -> "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 []
30+
31+
data_entry = Data(
32+
geode_object=geode_object,
33+
input_file=input_file,
34+
additional_files=additional_files,
35+
native_file_name="",
36+
viewable_file_name="",
37+
light_viewable=None,
38+
)
39+
40+
session = get_session()
41+
if session:
42+
session.add(data_entry)
43+
session.flush()
44+
session.commit()
45+
return data_entry
46+
47+
@staticmethod
48+
def get(data_id: str) -> "Data | None":
49+
session = get_session()
50+
if session:
51+
return session.get(Data, data_id)
52+
return None

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22
import pytest
33
from flask import Flask
4-
from src.opengeodeweb_microservice.database.connection import init_database, get_session
5-
from src.opengeodeweb_microservice.microservice.data import Data
4+
from opengeodeweb_microservice.database.connection import init_database, get_session
5+
from opengeodeweb_microservice.microservice.data import Data
66

77

88
@pytest.fixture(scope="session")

0 commit comments

Comments
 (0)