feat: add unique constraints for custom subdomain and domain per platform
Some checks failed
Some checks failed
Add UNIQUE(custom_subdomain, platform_id) on store_platforms to prevent two stores from claiming the same subdomain on the same platform. Add UNIQUE(store_id, platform_id) on store_domains to enforce one custom domain per store per platform. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
"""add unique constraints for custom_subdomain and store domain per platform
|
||||||
|
|
||||||
|
Revision ID: z_unique_subdomain_domain
|
||||||
|
Revises: a44f4956cfb1
|
||||||
|
Create Date: 2026-02-26
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
revision = "z_unique_subdomain_domain"
|
||||||
|
down_revision = ("a44f4956cfb1", "tenancy_003")
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
# StorePlatform: same custom_subdomain cannot be claimed twice on the same platform
|
||||||
|
op.create_unique_constraint(
|
||||||
|
"uq_custom_subdomain_platform",
|
||||||
|
"store_platforms",
|
||||||
|
["custom_subdomain", "platform_id"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# StoreDomain: a store can have at most one custom domain per platform
|
||||||
|
op.create_unique_constraint(
|
||||||
|
"uq_store_domain_platform",
|
||||||
|
"store_domains",
|
||||||
|
["store_id", "platform_id"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
op.drop_constraint("uq_store_domain_platform", "store_domains", type_="unique")
|
||||||
|
op.drop_constraint("uq_custom_subdomain_platform", "store_platforms", type_="unique")
|
||||||
@@ -68,6 +68,8 @@ class StoreDomain(Base, TimestampMixin):
|
|||||||
# Constraints
|
# Constraints
|
||||||
__table_args__ = (
|
__table_args__ = (
|
||||||
UniqueConstraint("store_id", "domain", name="uq_store_domain"),
|
UniqueConstraint("store_id", "domain", name="uq_store_domain"),
|
||||||
|
# A store can have at most one custom domain per platform
|
||||||
|
UniqueConstraint("store_id", "platform_id", name="uq_store_domain_platform"),
|
||||||
Index("idx_domain_active", "domain", "is_active"),
|
Index("idx_domain_active", "domain", "is_active"),
|
||||||
Index("idx_store_domain_primary", "store_id", "is_primary"),
|
Index("idx_store_domain_primary", "store_id", "is_primary"),
|
||||||
Index("idx_store_domain_platform", "platform_id"),
|
Index("idx_store_domain_platform", "platform_id"),
|
||||||
|
|||||||
@@ -152,6 +152,12 @@ class StorePlatform(Base, TimestampMixin):
|
|||||||
"platform_id",
|
"platform_id",
|
||||||
name="uq_store_platform",
|
name="uq_store_platform",
|
||||||
),
|
),
|
||||||
|
# Same custom_subdomain cannot be claimed twice on the same platform
|
||||||
|
UniqueConstraint(
|
||||||
|
"custom_subdomain",
|
||||||
|
"platform_id",
|
||||||
|
name="uq_custom_subdomain_platform",
|
||||||
|
),
|
||||||
# Performance indexes
|
# Performance indexes
|
||||||
Index(
|
Index(
|
||||||
"idx_store_platform_active",
|
"idx_store_platform_active",
|
||||||
|
|||||||
Reference in New Issue
Block a user