Skip to content
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

Database utils updates #36

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app-frame/macrostrat/app_frame/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from macrostrat.app_frame.compose.base import compose

from .control_command import BackendType, CommandBase, ControlCommand
from .core import Application
from .exc import ApplicationError
from .subsystems import Subsystem, SubsystemError, SubsystemManager
from .control_command import ControlCommand, CommandBase, BackendType
5 changes: 3 additions & 2 deletions app-frame/macrostrat/app_frame/compose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from typer import Context, Typer

from macrostrat.utils import get_logger
from .base import check_status, compose
from .follow_logs import Result, command_stream, follow_logs

from ..core import Application
from ..utils import add_click_command
from .base import check_status, compose
from .follow_logs import Result, command_stream, follow_logs

# Typer command-line application

Expand Down
4 changes: 2 additions & 2 deletions app-frame/macrostrat/app_frame/control_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from enum import Enum
from os import environ

from typer import Context, Option
from typer import rich_utils
from typer import Context, Option, rich_utils
from typer.models import TyperInfo

from macrostrat.utils import get_logger

from .compose import add_docker_compose_commands
from .core import Application
from .utils import CommandBase, ControlCommandGroup # noqa
Expand Down
2 changes: 1 addition & 1 deletion app-frame/macrostrat/app_frame/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def load_dotenv(self):

def setup_environment(self, env: EnvironmentDependency):
environ["DOCKER_SCAN_SUGGEST"] = "false"
#environ["DOCKER_BUILDKIT"] = "1"
# environ["DOCKER_BUILDKIT"] = "1"

# Set up environment for docker-compose
# We may need to move this to a context where it is only
Expand Down
4 changes: 3 additions & 1 deletion app-frame/macrostrat/app_frame/subsystems/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def order_plugins(self, store=None):
def __load_plugin(self, plugin, app: ApplicationBase):
if isinstance(plugin, Subsystem):
if plugin.app is not app:
raise SubsystemError(f"Subsystem {plugin.name} was initialized against the wrong app")
raise SubsystemError(
f"Subsystem {plugin.name} was initialized against the wrong app"
)
return plugin
if issubclass(plugin, Subsystem):
return plugin(app)
Expand Down
2 changes: 0 additions & 2 deletions app-frame/macrostrat/app_frame/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def command(self, name=None, **kwargs):
_name = self._name_modifier(name, kwargs.pop("aliases", None))
return super().command(_name, **kwargs)


def _name_modifier(self, name, aliases: list[str] = None):
"""Return a function that modifies the name for a command to include aliases
https://github.com/fastapi/typer/issues/132
Expand Down Expand Up @@ -69,7 +68,6 @@ def get_command(self, ctx, cmd_name):
cmd_name = self._group_cmd_name(cmd_name)
return super().get_command(ctx, cmd_name)


def _group_cmd_name(self, default_name):
for cmd in self.commands.values():
name = cmd.name
Expand Down
176 changes: 169 additions & 7 deletions app-frame/poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions auth-system/macrostrat/auth_system/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .main import (
get_groups,
get_groups_from_header_token,
get_user_id,
get_user_token_from_cookie,
get_groups,
has_access,
get_user_id
)
6 changes: 3 additions & 3 deletions auth-system/macrostrat/auth_system/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from contextvars import ContextVar
from typing import Optional

from sqlalchemy import Engine
from sqlalchemy import select, update
from sqlalchemy.orm import sessionmaker, declarative_base, Session
from sqlalchemy import Engine, select, update
from sqlalchemy.orm import Session, declarative_base, sessionmaker

from macrostrat.database import Database

from .schema import Token


Expand Down
56 changes: 32 additions & 24 deletions auth-system/macrostrat/auth_system/core/schema.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import datetime
import enum
from typing import List
import datetime
from sqlalchemy import ForeignKey, func, DateTime, Enum, PrimaryKeyConstraint, UniqueConstraint
from sqlalchemy.dialects.postgresql import VARCHAR, TEXT, INTEGER, ARRAY, BOOLEAN, JSON, JSONB

from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
PrimaryKeyConstraint,
UniqueConstraint,
func,
)
from sqlalchemy.dialects.postgresql import (
ARRAY,
BOOLEAN,
INTEGER,
JSON,
JSONB,
TEXT,
VARCHAR,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship


Expand All @@ -12,36 +28,34 @@ class Base(DeclarativeBase):

class GroupMembers(Base):
__tablename__ = "group_members"
__table_args__ = {
'schema': 'macrostrat_auth'
}
__table_args__ = {"schema": "macrostrat_auth"}
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
group_id: Mapped[int] = mapped_column(ForeignKey("macrostrat_auth.group.id"))
user_id: Mapped[int] = mapped_column(ForeignKey("macrostrat_auth.user.id"))


class Group(Base):
__tablename__ = "group"
__table_args__ = {
'schema': 'macrostrat_auth'
}
__table_args__ = {"schema": "macrostrat_auth"}
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(VARCHAR(255))
users: Mapped[List["User"]] = relationship(secondary="macrostrat_auth.group_members", lazy="joined",
back_populates="groups")
users: Mapped[List["User"]] = relationship(
secondary="macrostrat_auth.group_members",
lazy="joined",
back_populates="groups",
)


class User(Base):
__tablename__ = "user"
__table_args__ = {
'schema': 'macrostrat_auth'
}
__table_args__ = {"schema": "macrostrat_auth"}
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
sub: Mapped[str] = mapped_column(VARCHAR(255))
name: Mapped[str] = mapped_column(VARCHAR(255))
email: Mapped[str] = mapped_column(VARCHAR(255))
groups: Mapped[List[Group]] = relationship(secondary="macrostrat_auth.group_members", lazy="joined",
back_populates="users")
groups: Mapped[List[Group]] = relationship(
secondary="macrostrat_auth.group_members", lazy="joined", back_populates="users"
)
created_on: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)
Expand All @@ -52,20 +66,14 @@ class User(Base):

class Token(Base):
__tablename__ = "token"
__table_args__ = {
'schema': 'macrostrat_auth'
}
__table_args__ = {"schema": "macrostrat_auth"}
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
token: Mapped[str] = mapped_column(VARCHAR(255), unique=True)
group: Mapped[Group] = mapped_column(ForeignKey("macrostrat_auth.group.id"))
used_on: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=True
)
expires_on: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True)
)
expires_on: Mapped[datetime.datetime] = mapped_column(DateTime(timezone=True))
created_on: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now()
)


7 changes: 4 additions & 3 deletions auth-system/macrostrat/auth_system/legacy/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from starlette.routing import Route, Router
from starlette.authentication import AuthenticationError, requires
from starlette.responses import JSONResponse
from starlette.authentication import requires, AuthenticationError
from starlette.routing import Route, Router
from webargs_starlette import use_annotations

from macrostrat.utils import get_logger

from webargs_starlette import use_annotations
from .context import get_backend, get_identity_provider

log = get_logger(__name__)
Expand Down
8 changes: 4 additions & 4 deletions auth-system/macrostrat/auth_system/legacy/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"""
import time
import warnings
from typing import Tuple, Any
from typing import Any, Tuple

import jwt
from starlette.authentication import (
BaseUser,
SimpleUser,
AuthenticationBackend,
AuthCredentials,
AuthenticationBackend,
AuthenticationError,
BaseUser,
SimpleUser,
UnauthenticatedUser,
)
from starlette.requests import Request
Expand Down
2 changes: 1 addition & 1 deletion auth-system/macrostrat/auth_system/legacy/identity.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.security import check_password_hash, generate_password_hash


class IdentityProvider:
Expand Down
2 changes: 1 addition & 1 deletion auth-system/macrostrat/auth_system/legacy/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from . import get_backend
from .api import AuthAPI
from .context import create_backend, set_identity_provider, get_identity_provider
from .context import create_backend, get_identity_provider, set_identity_provider
from .identity import BaseUser, IdentityProvider


Expand Down
26 changes: 25 additions & 1 deletion database/macrostrat/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

from psycopg2.errors import InvalidSavepointSpecification
from psycopg2.sql import Identifier
from sqlalchemy import URL, MetaData, create_engine, inspect, Engine
from sqlalchemy import URL, Engine, MetaData, create_engine, inspect
from sqlalchemy.exc import IntegrityError, InternalError
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.orm import Session, scoped_session, sessionmaker
from sqlalchemy.sql.expression import Insert

from macrostrat.utils import get_logger

from .mapper import DatabaseMapper
from .postgresql import on_conflict, prefix_inserts # noqa
from .utils import ( # noqa
Expand Down Expand Up @@ -184,6 +185,29 @@ def inspector(self):
self.__inspector__ = inspect(self.engine)
return self.__inspector__

def refresh_schema(self, *, automap=None):
"""
Refresh the current database connection

- closes the session and flushes
- removes the inspector

If automap is True, will automap the database after refreshing.
If automap is False, will not automap the database after refreshing.
If automap is None, it will re-map the database if it was previously mapped.
"""
# Close the session
self.session.flush()
self.session.close()
# Remove the inspector
self.__inspector__ = None

if automap is None:
automap = self.mapper is not None

if automap:
self.automap()

def entity_names(self, **kwargs):
"""
Returns an iterator of names of *schema objects*
Expand Down
1 change: 1 addition & 0 deletions database/macrostrat/database/transfer/dump_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlalchemy.engine import Engine

from macrostrat.utils import get_logger

from .stream_utils import print_stdout, print_stream_progress
from .utils import _create_command

Expand Down
1 change: 1 addition & 0 deletions database/macrostrat/database/transfer/move_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy.engine import Engine

from macrostrat.utils import get_logger

from .dump_database import pg_dump
from .restore_database import pg_restore
from .stream_utils import print_stdout, print_stream_progress
Expand Down
1 change: 1 addition & 0 deletions database/macrostrat/database/transfer/restore_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sqlalchemy.engine import Engine

from macrostrat.utils import get_logger

from .stream_utils import print_stdout, print_stream_progress
from .utils import _create_command, _create_database_if_not_exists

Expand Down
1 change: 1 addition & 0 deletions database/macrostrat/database/transfer/stream_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aiofiles.threadpool import AsyncBufferedIOBase

from macrostrat.utils import get_logger

from .utils import console

log = get_logger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion database/macrostrat/database/transfer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.engine.url import URL
from sqlalchemy_utils import create_database, database_exists, drop_database

from macrostrat.utils import get_logger, ApplicationError
from macrostrat.utils import ApplicationError, get_logger

console = Console()

Expand Down
Loading
Loading