Spaces:
Running

axeronis commited on
Commit
8a8c88a
·
1 Parent(s): 1ddd07d

Fix: Use VARCHAR instead of Postgres enum for subscription_plan

Browse files
backend/alembic/versions/2025_12_24_fix_subscription_case.py CHANGED
@@ -4,12 +4,13 @@ Revision ID: fix_subscriptionplan_case
4
  Revises:
5
  Create Date: 2025-12-24
6
 
7
- This migration fixes the case mismatch between stored values (uppercase like 'START')
8
- and the Postgres enum (lowercase like 'start').
 
 
9
  """
10
  from alembic import op
11
  import sqlalchemy as sa
12
- from sqlalchemy.dialects import postgresql
13
 
14
 
15
  # revision identifiers, used by Alembic.
@@ -20,33 +21,27 @@ depends_on = None
20
 
21
 
22
  def upgrade():
23
- """Convert uppercase subscription_plan values to lowercase."""
24
- # First, temporarily change column to VARCHAR to allow data manipulation
25
  op.execute("""
26
  ALTER TABLE tenants
27
- ALTER COLUMN subscription_plan TYPE VARCHAR
28
  USING subscription_plan::VARCHAR
29
  """)
30
 
31
- # Update all uppercase values to lowercase
32
  op.execute("""
33
  UPDATE tenants
34
  SET subscription_plan = LOWER(subscription_plan)
35
  WHERE subscription_plan IS NOT NULL
36
  """)
37
 
38
- # Map any non-standard values to valid ones
39
  op.execute("""
40
  UPDATE tenants
41
  SET subscription_plan = 'trial'
42
- WHERE subscription_plan NOT IN ('trial', 'start', 'growth', 'enterprise')
43
- """)
44
-
45
- # Convert back to enum type
46
- op.execute("""
47
- ALTER TABLE tenants
48
- ALTER COLUMN subscription_plan TYPE subscriptionplan
49
- USING subscription_plan::subscriptionplan
50
  """)
51
 
52
 
 
4
  Revises:
5
  Create Date: 2025-12-24
6
 
7
+ This migration:
8
+ 1. Changes subscription_plan from Postgres enum to VARCHAR
9
+ 2. Normalizes all values to lowercase
10
+ 3. Keeps as VARCHAR (Python Enum handles validation)
11
  """
12
  from alembic import op
13
  import sqlalchemy as sa
 
14
 
15
 
16
  # revision identifiers, used by Alembic.
 
21
 
22
 
23
  def upgrade():
24
+ """Convert subscription_plan to VARCHAR and normalize to lowercase."""
25
+ # Change column to VARCHAR (permanently - avoid Postgres enum issues)
26
  op.execute("""
27
  ALTER TABLE tenants
28
+ ALTER COLUMN subscription_plan TYPE VARCHAR(50)
29
  USING subscription_plan::VARCHAR
30
  """)
31
 
32
+ # Normalize all values to lowercase
33
  op.execute("""
34
  UPDATE tenants
35
  SET subscription_plan = LOWER(subscription_plan)
36
  WHERE subscription_plan IS NOT NULL
37
  """)
38
 
39
+ # Map any non-standard values to 'trial'
40
  op.execute("""
41
  UPDATE tenants
42
  SET subscription_plan = 'trial'
43
+ WHERE subscription_plan IS NULL
44
+ OR subscription_plan NOT IN ('trial', 'start', 'growth', 'enterprise')
 
 
 
 
 
 
45
  """)
46
 
47
 
backend/app/models/tenant.py CHANGED
@@ -3,7 +3,7 @@ from typing import Optional, List, Dict, Any
3
  from datetime import datetime
4
  from uuid import UUID, uuid4
5
  from enum import Enum
6
- from sqlalchemy import JSON, Enum as SAEnum
7
 
8
 
9
  class SubscriptionPlan(str, Enum):
@@ -35,11 +35,8 @@ class Tenant(SQLModel, table=True):
35
  id: UUID = Field(default_factory=uuid4, primary_key=True)
36
  name: str = Field(index=True)
37
 
38
- # Subscription - use values_callable to persist lowercase values (not uppercase enum names)
39
- subscription_plan: SubscriptionPlan = Field(
40
- default=SubscriptionPlan.TRIAL,
41
- sa_column=Column(SAEnum(SubscriptionPlan, values_callable=lambda obj: [e.value for e in obj]))
42
- )
43
  subscription_status: str = Field(default="trial") # trial, active, expired, cancelled
44
  subscription_start: datetime = Field(default_factory=datetime.utcnow)
45
  subscription_end: Optional[datetime] = None
 
3
  from datetime import datetime
4
  from uuid import UUID, uuid4
5
  from enum import Enum
6
+ from sqlalchemy import JSON
7
 
8
 
9
  class SubscriptionPlan(str, Enum):
 
35
  id: UUID = Field(default_factory=uuid4, primary_key=True)
36
  name: str = Field(index=True)
37
 
38
+ # Subscription - stored as VARCHAR, validated via Python Enum
39
+ subscription_plan: str = Field(default="trial", index=True)
 
 
 
40
  subscription_status: str = Field(default="trial") # trial, active, expired, cancelled
41
  subscription_start: datetime = Field(default_factory=datetime.utcnow)
42
  subscription_end: Optional[datetime] = None