-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ catalog: adds services compatibility policy 🗃️ (#6071)
- Loading branch information
Showing
14 changed files
with
738 additions
and
56 deletions.
There are no files selected for viewing
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
97 changes: 97 additions & 0 deletions
97
.../src/simcore_postgres_database/migration/versions/d0e56c2d0a0d_new_services_comp_table.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,97 @@ | ||
"""new services_comp table | ||
Revision ID: d0e56c2d0a0d | ||
Revises: 19f3d9085636 | ||
Create Date: 2024-07-17 16:15:49.970615+00:00 | ||
""" | ||
from typing import Final | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "d0e56c2d0a0d" | ||
down_revision = "19f3d9085636" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
# auto-update modified | ||
# TRIGGERS ------------------------ | ||
_TABLE_NAME: Final[str] = "services_compatibility" | ||
_TRIGGER_NAME: Final[str] = "trigger_auto_update" # NOTE: scoped on table | ||
_PROCEDURE_NAME: Final[ | ||
str | ||
] = f"{_TABLE_NAME}_auto_update_modified()" # NOTE: scoped on database | ||
modified_timestamp_trigger = sa.DDL( | ||
f""" | ||
DROP TRIGGER IF EXISTS {_TRIGGER_NAME} on {_TABLE_NAME}; | ||
CREATE TRIGGER {_TRIGGER_NAME} | ||
BEFORE INSERT OR UPDATE ON {_TABLE_NAME} | ||
FOR EACH ROW EXECUTE PROCEDURE {_PROCEDURE_NAME}; | ||
""" | ||
) | ||
|
||
# PROCEDURES ------------------------ | ||
update_modified_timestamp_procedure = sa.DDL( | ||
f""" | ||
CREATE OR REPLACE FUNCTION {_PROCEDURE_NAME} | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
NEW.modified := current_timestamp; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
""" | ||
) | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"services_compatibility", | ||
sa.Column("key", sa.String(), nullable=False), | ||
sa.Column("version", sa.String(), nullable=False), | ||
sa.Column( | ||
"custom_policy", postgresql.JSONB(astext_type=sa.Text()), nullable=False | ||
), | ||
sa.Column( | ||
"created", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("now()"), | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"modified", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.text("now()"), | ||
nullable=False, | ||
), | ||
sa.Column("modified_by", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["key", "version"], | ||
["services_meta_data.key", "services_meta_data.version"], | ||
onupdate="CASCADE", | ||
ondelete="CASCADE", | ||
), | ||
sa.ForeignKeyConstraint( | ||
["modified_by"], ["users.id"], onupdate="CASCADE", ondelete="SET NULL" | ||
), | ||
sa.PrimaryKeyConstraint("key", "version", name="services_compatibility_pk"), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
# custom | ||
op.execute(update_modified_timestamp_procedure) | ||
op.execute(modified_timestamp_trigger) | ||
|
||
|
||
def downgrade(): | ||
# custom | ||
op.execute(f"DROP TRIGGER IF EXISTS {_TRIGGER_NAME} on {_TABLE_NAME};") | ||
op.execute(f"DROP FUNCTION {_PROCEDURE_NAME};") | ||
|
||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table("services_compatibility") | ||
# ### 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
68 changes: 68 additions & 0 deletions
68
packages/postgres-database/src/simcore_postgres_database/models/services_compatibility.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,68 @@ | ||
""" Services table | ||
- List of 3rd party services in the framework | ||
- Services have a key, version, and access rights defined by group ids | ||
""" | ||
|
||
|
||
import sqlalchemy as sa | ||
import typing_extensions | ||
from sqlalchemy.dialects.postgresql import JSONB | ||
from typing_extensions import NotRequired, Required | ||
|
||
from ._common import ( | ||
column_created_datetime, | ||
column_modified_by_user, | ||
column_modified_datetime, | ||
) | ||
from .base import metadata | ||
from .users import users | ||
|
||
|
||
class CompatiblePolicyDict(typing_extensions.TypedDict, total=False): | ||
# SpecifierSet e.g. ~=0.9 | ||
# SEE https://packaging.python.org/en/latest/specifications/version-specifiers/#id5 | ||
versions_specifier: Required[str] | ||
# Only necessary if key!=PolicySpecifierDict.key | ||
other_service_key: NotRequired[str | None] | ||
|
||
|
||
services_compatibility = sa.Table( | ||
# | ||
# CUSTOM COMPATIBILITY POLICIES | ||
# Otherwise default compatibility policy is employed. | ||
# | ||
"services_compatibility", | ||
metadata, | ||
sa.Column( | ||
"key", | ||
sa.String, | ||
nullable=False, | ||
doc="Service Key Identifier", | ||
), | ||
sa.Column( | ||
"version", | ||
sa.String, | ||
nullable=False, | ||
doc="Service version", | ||
), | ||
sa.Column( | ||
"custom_policy", | ||
JSONB, | ||
nullable=False, | ||
doc="PolicySpecifierDict with custom policy", | ||
), | ||
# Traceability, i.e. when | ||
column_created_datetime(timezone=True), | ||
column_modified_datetime(timezone=True), | ||
# Traceability, i.e. who | ||
column_modified_by_user(users_table=users, required=True), | ||
# Constraints | ||
sa.ForeignKeyConstraint( | ||
["key", "version"], | ||
["services_meta_data.key", "services_meta_data.version"], | ||
onupdate="CASCADE", | ||
ondelete="CASCADE", | ||
), | ||
sa.PrimaryKeyConstraint("key", "version", name="services_compatibility_pk"), | ||
) |
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
Oops, something went wrong.