diff --git a/packages/postgres-database/src/simcore_postgres_database/migration/versions/52b5c2466605_remove_services_limitations.py b/packages/postgres-database/src/simcore_postgres_database/migration/versions/52b5c2466605_remove_services_limitations.py new file mode 100644 index 000000000000..f1392536e7cd --- /dev/null +++ b/packages/postgres-database/src/simcore_postgres_database/migration/versions/52b5c2466605_remove_services_limitations.py @@ -0,0 +1,97 @@ +"""remove services_limitations + +Revision ID: 52b5c2466605 +Revises: 38fe651b4196 +Create Date: 2023-06-27 15:24:13.207340+00:00 + +""" +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "52b5c2466605" +down_revision = "38fe651b4196" +branch_labels = None +depends_on = None + + +modified_timestamp_trigger = sa.DDL( + """ +DROP TRIGGER IF EXISTS trigger_auto_update on services_limitations; +CREATE TRIGGER trigger_auto_update +BEFORE INSERT OR UPDATE ON services_limitations +FOR EACH ROW EXECUTE PROCEDURE services_limitations_auto_update_modified(); + """ +) + +update_modified_timestamp_procedure = sa.DDL( + """ +CREATE OR REPLACE FUNCTION services_limitations_auto_update_modified() +RETURNS TRIGGER AS $$ +BEGIN + NEW.modified := current_timestamp; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; + """ +) + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index("idx_unique_gid_cluster_id_null", table_name="services_limitations") + op.drop_table("services_limitations") + # ### end Alembic commands ### + # custom + op.execute(f"DROP FUNCTION services_limitations_auto_update_modified();") + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "services_limitations", + sa.Column("gid", sa.BIGINT(), autoincrement=False, nullable=False), + sa.Column("cluster_id", sa.BIGINT(), autoincrement=False, nullable=True), + sa.Column("ram", sa.BIGINT(), autoincrement=False, nullable=True), + sa.Column("cpu", sa.NUMERIC(), autoincrement=False, nullable=True), + sa.Column("vram", sa.BIGINT(), autoincrement=False, nullable=True), + sa.Column("gpu", sa.INTEGER(), autoincrement=False, nullable=True), + sa.Column( + "created", + postgresql.TIMESTAMP(timezone=True), + server_default=sa.text("now()"), + autoincrement=False, + nullable=False, + ), + sa.Column( + "modified", + postgresql.TIMESTAMP(timezone=True), + server_default=sa.text("now()"), + autoincrement=False, + nullable=False, + ), + sa.ForeignKeyConstraint( + ["cluster_id"], + ["clusters.id"], + name="fk_services_limitations_to_clusters_id", + onupdate="CASCADE", + ondelete="CASCADE", + ), + sa.ForeignKeyConstraint( + ["gid"], + ["groups.gid"], + name="fk_services_limitations_to_groups_gid", + onupdate="CASCADE", + ondelete="CASCADE", + ), + sa.UniqueConstraint("gid", "cluster_id", name="gid_cluster_id_uniqueness"), + ) + op.create_index( + "idx_unique_gid_cluster_id_null", "services_limitations", ["gid"], unique=False + ) + # ### end Alembic commands ### + + # custom + op.execute(update_modified_timestamp_procedure) + op.execute(modified_timestamp_trigger) diff --git a/packages/postgres-database/src/simcore_postgres_database/models/services_limitations.py b/packages/postgres-database/src/simcore_postgres_database/models/services_limitations.py deleted file mode 100644 index b06d9a858ace..000000000000 --- a/packages/postgres-database/src/simcore_postgres_database/models/services_limitations.py +++ /dev/null @@ -1,98 +0,0 @@ -""" Groups table - - - List of groups in the framework - - Groups have a ID, name and a list of users that belong to the group -""" - - -from typing import Final - -import sqlalchemy as sa - -from ._common import ( - column_created_datetime, - column_modified_datetime, - register_modified_datetime_auto_update_trigger, -) -from .base import metadata -from .clusters import clusters -from .groups import groups - -_TABLE_NAME = "services_limitations" -USE_DEFAULTS: Final = None -UNLIMITED: Final[int] = -999 - -services_limitations = sa.Table( - _TABLE_NAME, - metadata, - sa.Column( - "gid", - sa.BigInteger, - sa.ForeignKey( - groups.c.gid, - onupdate="CASCADE", - ondelete="CASCADE", - name=f"fk_{_TABLE_NAME}_to_groups_gid", - ), - nullable=False, - doc="Group unique ID", - ), - sa.Column( - "cluster_id", - sa.BigInteger(), - sa.ForeignKey( - clusters.c.id, - name=f"fk_{_TABLE_NAME}_to_clusters_id", - onupdate="CASCADE", - ondelete="CASCADE", - ), - nullable=True, - doc="The cluster id with which these limitations are associated, if NULL or 0 uses the default", - ), - sa.Column( - "ram", - sa.BigInteger, - nullable=True, - doc="defines this group maximum allowable RAM used per service " - "(None means use defaults, <0 means no limits)", - ), - sa.Column( - "cpu", - sa.Numeric, - nullable=True, - doc="defines this group maximum allowable CPUs used per service " - "(None means use defaults, <0 means no limits)", - ), - sa.Column( - "vram", - sa.BigInteger, - nullable=True, - doc="defines this group maximum allowable VRAM used per service " - "(None means use defaults, <0 means no limits)", - ), - sa.Column( - "gpu", - sa.Integer, - nullable=True, - doc="defines this group maximum allowable CPUs used per service " - "(None means use defaults, <0 means no limits)", - ), - # TIME STAMPS ---- - column_created_datetime(timezone=True), - column_modified_datetime(timezone=True), - sa.UniqueConstraint( - "gid", - "cluster_id", - name="gid_cluster_id_uniqueness", - ), - # prevents having multiple entries with NULL cluster (postgres < 15 treats NULL as always different) - sa.Index( - "idx_unique_gid_cluster_id_null", - "gid", - unique=True, - postgresql_where=sa.text("cluster_id IS NULL"), - ), -) - - -register_modified_datetime_auto_update_trigger(services_limitations)