-
Notifications
You must be signed in to change notification settings - Fork 0
Fix/database #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix/database #9
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
54d1c97
renamed var database & cleaned imports and README
MaxNumerique 827284c
sql alchemy replacing flask sql alchemy
MaxNumerique 167d5de
pyproject version 0.0.0
MaxNumerique b66eb63
fix(database): using SQL Alchemy instead of Flask-SQLAlchemy
MaxNumerique 9ee333b
test to insure that we do have the required paramaters to add dataa …
MaxNumerique d1d541d
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique fdd2dd7
Merge branch 'fix/database' of https://github.com/Geode-solutions/Ope…
MaxNumerique 90a7d36
Apply prepare changes
MaxNumerique 920cdfa
mypy type
MaxNumerique 631f9d8
Session type
MaxNumerique 72c2f15
update requirements.in
MaxNumerique f8c5bd1
Apply prepare changes
MaxNumerique d9f9d7a
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique d02aa19
Apply prepare changes
MaxNumerique a16c26d
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 77257f1
Merge branch 'fix/database' of https://github.com/Geode-solutions/Ope…
MaxNumerique 8c4a3ec
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 4aaca60
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 89a9350
Apply prepare changes
MaxNumerique f823e41
added logs + get_session as session
MaxNumerique 0cbdf4b
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique d1047b4
Apply prepare changes
MaxNumerique 8cd055c
mypy
MaxNumerique 52405f5
Merge branch 'fix/database' of https://github.com/Geode-solutions/Ope…
MaxNumerique f535b04
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique c801bdb
Apply prepare changes
MaxNumerique 3cba6eb
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 834ccae
Merge branch 'fix/database' of https://github.com/Geode-solutions/Ope…
MaxNumerique dee349c
Apply prepare changes
MaxNumerique 15a768e
Merge branch 'next' of https://github.com/Geode-solutions/OpenGeodeWe…
MaxNumerique 05e206b
Merge branch 'fix/database' of https://github.com/Geode-solutions/Ope…
MaxNumerique 844d53f
Apply prepare changes
MaxNumerique f44c1aa
adding bool for init_database function to know if tables have already…
MaxNumerique c32cf09
Merge branch 'fix/database' of https://github.com/Geode-solutions/Ope…
MaxNumerique f9cc61a
typed get()
MaxNumerique File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| sqlalchemy==2.0.43 | ||
| flask-sqlalchemy==3.1.1 | ||
| SQLAlchemy==2.0.43 | ||
| fastjsonschema==2.21.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| from flask import Flask | ||
| from flask_sqlalchemy import SQLAlchemy | ||
| from sqlalchemy.orm import DeclarativeBase | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,37 @@ | ||
| """Database connection management""" | ||
|
|
||
| from typing import Optional | ||
| from sqlalchemy.orm import scoped_session | ||
| from flask import Flask | ||
| from flask_sqlalchemy import SQLAlchemy | ||
| from flask_sqlalchemy.session import Session | ||
| from sqlalchemy import create_engine | ||
| from sqlalchemy.orm import sessionmaker, scoped_session, Session | ||
| from .base import Base | ||
|
|
||
| DATABASE_FILENAME = "project.db" | ||
| db: Optional[SQLAlchemy] = None | ||
|
|
||
|
|
||
| def init_database(app: Flask, db_filename: str = DATABASE_FILENAME) -> SQLAlchemy: | ||
| global db | ||
| if db is None: | ||
| db = SQLAlchemy(model_class=Base) | ||
| db.init_app(app) | ||
| with app.app_context(): | ||
| db.create_all() | ||
| return db | ||
|
|
||
|
|
||
| def get_database() -> Optional[SQLAlchemy]: | ||
| return db | ||
|
|
||
|
|
||
| def get_session() -> Optional[scoped_session[Session]]: | ||
| return db.session if db else None | ||
| engine = None | ||
| session_factory = None | ||
| scoped_session_registry = None | ||
|
|
||
|
|
||
| def init_database(db_path: str = DATABASE_FILENAME, create_tables: bool = True) -> None: | ||
| global engine, session_factory, scoped_session_registry | ||
|
|
||
| if engine is None: | ||
| engine = create_engine( | ||
| f"sqlite:///{db_path}", | ||
| connect_args={"check_same_thread": False}, | ||
| ) | ||
| print(f"Database engine created for {db_path}", flush=True) | ||
| session_factory = sessionmaker(bind=engine) | ||
| scoped_session_registry = scoped_session(session_factory) | ||
| if create_tables: | ||
| Base.metadata.create_all(engine) | ||
| print(f"Database tables created for {db_path}", flush=True) | ||
| else: | ||
| print(f"Database connected (tables not created) for {db_path}", flush=True) | ||
| else: | ||
| print(f"Database engine already exists for {db_path}, reusing", flush=True) | ||
|
|
||
|
|
||
| def get_session() -> Session: | ||
| if scoped_session_registry is None: | ||
| raise RuntimeError() | ||
| return scoped_session_registry() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,32 @@ | ||
| from src.opengeodeweb_microservice.database.connection import get_session | ||
| from src.opengeodeweb_microservice.database.data import Data | ||
|
|
||
|
|
||
| def test_data_crud_operations(): | ||
| data = Data.create(geode_object="test_object", input_file="test.txt") | ||
| def test_data_crud_operations(clean_database): | ||
| data = Data.create( | ||
| geode_object="test_object", input_file="test.txt", additional_files=[] | ||
| ) | ||
| print("id", data.id, flush=True) | ||
| assert data.id is not None | ||
| session = get_session() | ||
| session.commit() | ||
| assert isinstance(data.id, str) | ||
|
|
||
| retrieved = Data.get(data.id) | ||
| assert retrieved is not None | ||
| assert isinstance(retrieved, Data) | ||
| assert retrieved.geode_object == "test_object" | ||
| assert retrieved.input_file == "test.txt" | ||
| assert retrieved.id == data.id | ||
| non_existent = Data.get("fake_id") | ||
| assert non_existent is None | ||
|
|
||
|
|
||
| def test_data_with_additional_files(): | ||
| def test_data_with_additional_files(clean_database): | ||
| files = ["file1.txt", "file2.txt"] | ||
| data = Data.create(geode_object="test_files", additional_files=files) | ||
| session = get_session() | ||
| session.commit() | ||
| assert data.id is not None | ||
| assert isinstance(data.id, str) | ||
|
|
||
| retrieved = Data.get(data.id) | ||
| assert retrieved is not None | ||
| assert isinstance(retrieved, Data) | ||
| assert retrieved.additional_files == files | ||
| assert retrieved.geode_object == "test_files" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.