Add subcription feature

This commit is contained in:
Urtzi Alfaro
2026-01-13 22:22:38 +01:00
parent b931a5c45e
commit 6ddf608d37
61 changed files with 7915 additions and 1238 deletions

View File

@@ -232,6 +232,11 @@ def upgrade() -> None:
sa.Column('report_retention_days', sa.Integer(), nullable=True),
# Enterprise-specific limits
sa.Column('max_child_tenants', sa.Integer(), nullable=True),
# Tenant linking support
sa.Column('user_id', sa.String(length=36), nullable=True),
sa.Column('is_tenant_linked', sa.Boolean(), nullable=False, server_default='FALSE'),
sa.Column('tenant_linking_status', sa.String(length=50), nullable=True),
sa.Column('linked_at', sa.DateTime(), nullable=True),
# Features and metadata
sa.Column('features', sa.JSON(), nullable=True),
sa.Column('created_at', sa.DateTime(timezone=True), nullable=False, server_default=sa.text('CURRENT_TIMESTAMP')),
@@ -299,6 +304,24 @@ def upgrade() -> None:
postgresql_where=sa.text("stripe_customer_id IS NOT NULL")
)
# Index 7: User ID for tenant linking
if not _index_exists(connection, 'idx_subscriptions_user_id'):
op.create_index(
'idx_subscriptions_user_id',
'subscriptions',
['user_id'],
unique=False
)
# Index 8: Tenant linking status
if not _index_exists(connection, 'idx_subscriptions_linking_status'):
op.create_index(
'idx_subscriptions_linking_status',
'subscriptions',
['tenant_linking_status'],
unique=False
)
# Create coupons table with tenant_id nullable to support system-wide coupons
op.create_table('coupons',
sa.Column('id', sa.UUID(), nullable=False),
@@ -417,6 +440,13 @@ def upgrade() -> None:
op.create_index('ix_tenant_locations_location_type', 'tenant_locations', ['location_type'])
op.create_index('ix_tenant_locations_coordinates', 'tenant_locations', ['latitude', 'longitude'])
# Add constraint to ensure data consistency for tenant linking
op.create_check_constraint(
'chk_tenant_linking',
'subscriptions',
"((is_tenant_linked = FALSE AND tenant_id IS NULL) OR (is_tenant_linked = TRUE AND tenant_id IS NOT NULL))"
)
def downgrade() -> None:
# Drop tenant_locations table
@@ -445,7 +475,12 @@ def downgrade() -> None:
op.drop_index('idx_coupon_code_active', table_name='coupons')
op.drop_table('coupons')
# Drop check constraint for tenant linking
op.drop_constraint('chk_tenant_linking', 'subscriptions', type_='check')
# Drop subscriptions table indexes first
op.drop_index('idx_subscriptions_linking_status', table_name='subscriptions')
op.drop_index('idx_subscriptions_user_id', table_name='subscriptions')
op.drop_index('idx_subscriptions_stripe_customer_id', table_name='subscriptions')
op.drop_index('idx_subscriptions_stripe_sub_id', table_name='subscriptions')
op.drop_index('idx_subscriptions_active_tenant', table_name='subscriptions')