feat: add tier_id FK to VendorSubscription for proper tier relationship

- Add tier_id column with FK to subscription_tiers table
- Add tier_obj relationship to VendorSubscription model
- Update tier_limits property to use database tier when available
- Create migration with SQLite batch mode support
- Backfill tier_id from existing tier code values

This enables proper database relationship between vendors and their
subscription tier, instead of just storing the tier code string.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-26 07:33:49 +01:00
parent 44de82eb47
commit 0ad54a52e0
3 changed files with 491 additions and 2 deletions

View File

@@ -0,0 +1,71 @@
"""Add tier_id FK to vendor_subscriptions
Revision ID: k9f0a1b2c3d4
Revises: 2953ed10d22c
Create Date: 2025-12-26
Adds tier_id column to vendor_subscriptions table with FK to subscription_tiers.
Backfills tier_id based on existing tier (code) values.
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "k9f0a1b2c3d4"
down_revision = "2953ed10d22c"
branch_labels = None
depends_on = None
def upgrade() -> None:
# Use batch mode for SQLite compatibility
with op.batch_alter_table("vendor_subscriptions", schema=None) as batch_op:
# Add tier_id column (nullable for backfill)
batch_op.add_column(
sa.Column("tier_id", sa.Integer(), nullable=True)
)
# Create index for tier_id
batch_op.create_index(
"ix_vendor_subscriptions_tier_id",
["tier_id"],
unique=False,
)
# Add FK constraint
batch_op.create_foreign_key(
"fk_vendor_subscriptions_tier_id",
"subscription_tiers",
["tier_id"],
["id"],
ondelete="SET NULL",
)
# Backfill tier_id from tier code
# This updates existing subscriptions to link to their tier
op.execute(
"""
UPDATE vendor_subscriptions
SET tier_id = (
SELECT id FROM subscription_tiers
WHERE subscription_tiers.code = vendor_subscriptions.tier
)
WHERE EXISTS (
SELECT 1 FROM subscription_tiers
WHERE subscription_tiers.code = vendor_subscriptions.tier
)
"""
)
def downgrade() -> None:
with op.batch_alter_table("vendor_subscriptions", schema=None) as batch_op:
# Drop FK constraint
batch_op.drop_constraint(
"fk_vendor_subscriptions_tier_id",
type_="foreignkey",
)
# Drop index
batch_op.drop_index("ix_vendor_subscriptions_tier_id")
# Drop column
batch_op.drop_column("tier_id")