-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Set tool configuration with YAML
- Loading branch information
1 parent
d9eb652
commit 545e9f6
Showing
39 changed files
with
973 additions
and
915 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
backend/capellacollab/alembic/versions/c973be2e2ac7_migrate_tools_to_json_configuration.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Migrate tools to JSON configuration | ||
Revision ID: c973be2e2ac7 | ||
Revises: 86ab7d4d1684 | ||
Create Date: 2024-01-31 17:40:31.743565 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "c973be2e2ac7" | ||
down_revision = "86ab7d4d1684" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("tool_integrations") | ||
op.add_column( | ||
"tools", | ||
sa.Column( | ||
"integrations", | ||
postgresql.JSONB(astext_type=sa.Text()), | ||
nullable=False, | ||
server_default=sa.text("'{}'::jsonb"), | ||
), | ||
) | ||
op.drop_column("tools", "docker_image_backup_template") | ||
op.drop_column("tools", "docker_image_template") | ||
op.drop_column("tools", "readonly_docker_image_template") | ||
op.add_column( | ||
"versions", | ||
sa.Column( | ||
"config", | ||
postgresql.JSONB(astext_type=sa.Text()), | ||
nullable=False, | ||
server_default=sa.text("'{}'::jsonb"), | ||
), | ||
) | ||
op.drop_column("versions", "is_deprecated") | ||
op.drop_column("versions", "is_recommended") | ||
# ### end Alembic commands ### |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import typing as t | ||
|
||
import pydantic | ||
from sqlalchemy import types | ||
from sqlalchemy.dialects import postgresql | ||
|
||
|
||
class PydanticDecorator(types.TypeDecorator): | ||
"""Maps a pydantic object to a JSONB column and vice versa. | ||
Use in Database models like this: | ||
```py | ||
json_column: orm.Mapped[pydantic.BaseModel] = orm.mapped_column(PydanticDecorator(pydantic.BaseModel)) | ||
``` | ||
Replace: | ||
- `json_column` with the name of the column in the database | ||
- `pydantic.BaseModel` with the pydantic model you want to use | ||
""" | ||
|
||
impl = postgresql.JSONB | ||
python_type = pydantic.BaseModel | ||
|
||
cache_ok = True | ||
|
||
def __init__(self, pydantic_model: t.Type[pydantic.BaseModel]): | ||
super().__init__() | ||
self.pydantic_model = pydantic_model | ||
|
||
def process_bind_param(self, value, dialect): | ||
"""Convert a pydantic object to JSONB.""" | ||
if value is None: | ||
return None | ||
return value.model_dump() | ||
|
||
def process_literal_param(self, value, dialect): | ||
"""Convert a literal pydantic object to JSONB.""" | ||
if value is None: | ||
return None | ||
return value.model_dump() | ||
|
||
def process_result_value(self, value, dialect): | ||
"""Convert JSONB to a pydantic object.""" | ||
if value is None: | ||
return None | ||
return self.pydantic_model.model_validate(value) | ||
|
||
|
||
class PydanticDatabaseModel(pydantic.BaseModel): | ||
"""Base class for database models with an ID. | ||
Use it to extend pydantic models with the database ID field: | ||
```py | ||
class PydanticModel(PydanticSuperModel, decorator.PydanticDatabaseModel): | ||
pass | ||
``` | ||
""" | ||
|
||
id: int = pydantic.Field( | ||
description="Unique identifier of the resource.", ge=1 | ||
) |
This file contains 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 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.