Skip to content

Commit

Permalink
🎨 improve DB foreign key dependencies (🗃️) (#6428)
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 authored Sep 24, 2024
1 parent 65ffdd9 commit 1a4dcea
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""improve foreign key dependencies
Revision ID: 10729e07000d
Revises: 47ca7335e146
Create Date: 2024-09-24 07:52:20.253076+00:00
"""
from alembic import op

# revision identifiers, used by Alembic.
revision = "10729e07000d"
down_revision = "47ca7335e146"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_foreign_key(
"fk_rut_pricing_plan_to_service_key_and_version",
"resource_tracker_pricing_plan_to_service",
"services_meta_data",
["service_key", "service_version"],
["key", "version"],
onupdate="CASCADE",
ondelete="CASCADE",
)
op.drop_index(
"ix_resource_tracker_pricing_plans_product_name",
table_name="resource_tracker_pricing_plans",
)
op.create_foreign_key(
"fk_rut_pricing_plans_product_name",
"resource_tracker_pricing_plans",
"products",
["product_name"],
["name"],
onupdate="CASCADE",
ondelete="CASCADE",
)
op.create_foreign_key(
"fk_resource_tracker_pricing_units_costs_pricing_plan_id",
"resource_tracker_pricing_unit_costs",
"resource_tracker_pricing_plans",
["pricing_plan_id"],
["pricing_plan_id"],
onupdate="CASCADE",
ondelete="CASCADE",
)
op.create_foreign_key(
"fk_resource_tracker_pricing_units_costs_pricing_unit_id",
"resource_tracker_pricing_unit_costs",
"resource_tracker_pricing_units",
["pricing_unit_id"],
["pricing_unit_id"],
onupdate="CASCADE",
ondelete="CASCADE",
)
op.create_foreign_key(
"fk_wallets_product_name",
"wallets",
"products",
["product_name"],
["name"],
onupdate="CASCADE",
ondelete="CASCADE",
)
op.create_foreign_key(
"fk_workspaces_product_name",
"workspaces",
"products",
["product_name"],
["name"],
onupdate="CASCADE",
ondelete="CASCADE",
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint("fk_workspaces_product_name", "workspaces", type_="foreignkey")
op.drop_constraint("fk_wallets_product_name", "wallets", type_="foreignkey")
op.drop_constraint(
"fk_resource_tracker_pricing_units_costs_pricing_unit_id",
"resource_tracker_pricing_unit_costs",
type_="foreignkey",
)
op.drop_constraint(
"fk_resource_tracker_pricing_units_costs_pricing_plan_id",
"resource_tracker_pricing_unit_costs",
type_="foreignkey",
)
op.drop_constraint(
"fk_rut_pricing_plans_product_name",
"resource_tracker_pricing_plans",
type_="foreignkey",
)
op.create_index(
"ix_resource_tracker_pricing_plans_product_name",
"resource_tracker_pricing_plans",
["product_name"],
unique=False,
)
op.drop_constraint(
"fk_rut_pricing_plan_to_service_key_and_version",
"resource_tracker_pricing_plan_to_service",
type_="foreignkey",
)
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,11 @@
doc="Option to mark default pricing plan for the service (ex. when there are more pricing plans for the same service)",
),
# ---------------------------
sa.ForeignKeyConstraint(
["service_key", "service_version"],
["services_meta_data.key", "services_meta_data.version"],
name="fk_rut_pricing_plan_to_service_key_and_version",
onupdate="CASCADE",
ondelete="CASCADE",
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ class PricingPlanClassification(str, enum.Enum):
sa.Column(
"product_name",
sa.String,
sa.ForeignKey(
"products.name",
onupdate="CASCADE",
ondelete="CASCADE",
name="fk_rut_pricing_plans_product_name",
),
nullable=False,
doc="Product name",
index=True,
doc="Products unique name",
),
sa.Column(
"display_name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
sa.Column(
"pricing_plan_id",
sa.BigInteger,
sa.ForeignKey(
"resource_tracker_pricing_plans.pricing_plan_id",
name="fk_resource_tracker_pricing_units_costs_pricing_plan_id",
onupdate="CASCADE",
ondelete="CASCADE",
),
nullable=False,
doc="Parent pricing plan",
doc="Foreign key to pricing plan",
index=True,
),
sa.Column(
Expand All @@ -35,8 +41,14 @@
sa.Column(
"pricing_unit_id",
sa.BigInteger,
sa.ForeignKey(
"resource_tracker_pricing_units.pricing_unit_id",
name="fk_resource_tracker_pricing_units_costs_pricing_unit_id",
onupdate="CASCADE",
ondelete="CASCADE",
),
nullable=False,
doc="Parent pricing unit",
doc="Foreign key to pricing unit",
index=True,
),
sa.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ class WalletStatus(str, enum.Enum):
),
column_created_datetime(timezone=True),
column_modified_datetime(timezone=True),
sa.Column("product_name", sa.String, nullable=False, doc="Product name"),
sa.Column(
"product_name",
sa.String,
sa.ForeignKey(
"products.name",
onupdate="CASCADE",
ondelete="CASCADE",
name="fk_wallets_product_name",
),
nullable=False,
doc="Products unique name",
),
)

# ------------------------ TRIGGERS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@
nullable=False,
doc="Identifier of the group that owns this workspace (Should be just PRIMARY GROUP)",
),
sa.Column("product_name", sa.String, nullable=False, doc="Product name"),
sa.Column(
"product_name",
sa.String,
sa.ForeignKey(
"products.name",
onupdate="CASCADE",
ondelete="CASCADE",
name="fk_workspaces_product_name",
),
nullable=False,
doc="Products unique name",
),
column_created_datetime(timezone=True),
column_modified_datetime(timezone=True),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from simcore_postgres_database.models.resource_tracker_pricing_units import (
resource_tracker_pricing_units,
)
from simcore_postgres_database.models.services import services_meta_data
from starlette import status
from yarl import URL

Expand Down Expand Up @@ -184,6 +185,15 @@ def resource_tracker_pricing_tables_db(postgres_db: sa.engine.Engine) -> Iterato
modified=datetime.now(tz=timezone.utc),
)
)

con.execute(
services_meta_data.insert().values(
key=_SERVICE_KEY,
version=_SERVICE_VERSION,
name="name",
description="description",
)
)
con.execute(
resource_tracker_pricing_plan_to_service.insert().values(
pricing_plan_id=_PRICING_PLAN_ID,
Expand All @@ -192,6 +202,15 @@ def resource_tracker_pricing_tables_db(postgres_db: sa.engine.Engine) -> Iterato
service_default_plan=True,
)
)

con.execute(
services_meta_data.insert().values(
key=_SERVICE_KEY_2,
version=_SERVICE_VERSION_2,
name="name",
description="description",
)
)
con.execute(
resource_tracker_pricing_plan_to_service.insert().values(
pricing_plan_id=_PRICING_PLAN_ID_2,
Expand All @@ -207,6 +226,7 @@ def resource_tracker_pricing_tables_db(postgres_db: sa.engine.Engine) -> Iterato
con.execute(resource_tracker_pricing_units.delete())
con.execute(resource_tracker_pricing_plans.delete())
con.execute(resource_tracker_pricing_unit_costs.delete())
con.execute(services_meta_data.delete())


async def test_get_default_pricing_plan_for_service(
Expand Down
Loading

0 comments on commit 1a4dcea

Please sign in to comment.