diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..e4fba21 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12 diff --git a/.woodpecker/test.yaml b/.woodpecker/test.yaml index 495cbec..5d32a38 100644 --- a/.woodpecker/test.yaml +++ b/.woodpecker/test.yaml @@ -20,14 +20,11 @@ steps: image: code.philo.ydns.eu/philorg/uv-geo environment: GISAF__DB__HOST: gisaf-database - GISAF__GISAF_LIVE__REDIS: redis://redis commands: # Initialize database - .venv/bin/gisaf create-db - .venv/bin/pytest -s tests/basic.py services: - redis: - image: docker.io/redis:alpine gisaf-database: - image: code.philo.ydns.eu/philorg/gisaf-database + image: code.philo.ydns.eu/philorg/treetrail-database diff --git a/src/gisaf/__init__.py b/src/gisaf/__init__.py index 0028fe7..710f782 100644 --- a/src/gisaf/__init__.py +++ b/src/gisaf/__init__.py @@ -4,7 +4,7 @@ try: from dunamai import Version, Style __version__ = Version.from_git().serialize(style=Style.SemVer, dirty=True) -except (ImportError, RuntimeError): +except ImportError: # __name__ could be used if the package name is the same # as the directory - not the case here # __version__ = importlib.metadata.version(__name__) diff --git a/src/gisaf/cli.py b/src/gisaf/cli.py index 55a5752..abb26cf 100644 --- a/src/gisaf/cli.py +++ b/src/gisaf/cli.py @@ -1,4 +1,6 @@ #!/usr/bin/env python +from importlib.metadata import version as importlib_version +from sqlalchemy.engine import create from typing_extensions import Annotated import typer @@ -9,6 +11,7 @@ cli = typer.Typer(no_args_is_help=True, help="Gisaf GIS backend") @cli.command() def create_db(): """Populate the database with a functional empty structure""" + from gisaf.application import app from gisaf.database import create_db from asyncio import run @@ -33,15 +36,15 @@ def serve(host: str = "localhost", port: int = 8000): def version_callback(show_version: bool): if show_version: - from gisaf import __version__ - - typer.echo(__version__) + print(importlib_version("gisaf-backend")) raise typer.Exit() @cli.callback() def main( - version: Annotated[bool | None, typer.Option("--version", callback=version_callback)] = None, + version: Annotated[ + bool | None, typer.Option("--version", callback=version_callback) + ] = None ): pass diff --git a/src/gisaf/database.py b/src/gisaf/database.py index f1e8d63..0717c92 100644 --- a/src/gisaf/database.py +++ b/src/gisaf/database.py @@ -166,24 +166,9 @@ async def create_db(drop=False): async with engine.begin() as conn: await create_schemas(conn) async with engine.begin() as conn: - # Import modules to populate the SQLModel metadata - from gisaf.models import admin - from gisaf.models import authentication - from gisaf.models import category - from gisaf.models import dashboard - from gisaf.models import geo_models_base - from gisaf.models import info - from gisaf.models import misc - from gisaf.models import project - from gisaf.models import raw_survey - from gisaf.models import reconcile - from gisaf.models import survey - from gisaf.models import tags - if drop: await conn.run_sync(SQLModel.metadata.drop_all) - resp = await conn.run_sync(SQLModel.metadata.create_all) - pass + await conn.run_sync(SQLModel.metadata.create_all) logger.debug(f"Connect to database with config: {conf.db}") while attempts > 0: @@ -201,7 +186,9 @@ async def create_db(drop=False): await populate_init_db() return else: - logger.warning(f"Cannot connect to database after {CREATE_DB_TIMEOUT}, giving up.") + logger.warning( + f"Cannot connect to database after {CREATE_DB_TIMEOUT}, giving up." + ) exit(1) @@ -209,15 +196,6 @@ async def is_fresh_install() -> bool: """Detect is the database is newly created, without data""" from gisaf.models.authentication import User - async with engine.begin() as conn: - has_table_query = """ - SELECT count(tablename) - FROM pg_catalog.pg_tables - WHERE schemaname = 'gisaf_admin' and tablename='user'""" - resp = await conn.execute(text(has_table_query)) - if resp.scalar() == 0: - return True - async with db_session() as session: nb_users = (await session.exec(select(func.count(col(User.username))))).one() return nb_users == 0 @@ -244,7 +222,9 @@ async def populate_init_db(): from gisaf.models.map_bases import BaseStyle logger.info("Populating initial database") + async with db_session() as session: + user = await create_user( session=session, username="admin",