Skip to content

Commit

Permalink
server/product: upgrade Stripe's products metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed May 15, 2024
1 parent bd5132c commit 8c4d5ee
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Update Stripe's product metadata
Revision ID: 40f397ad512a
Revises: 8ca7adb6786e
Create Date: 2024-05-15 12:06:39.431048
"""

import sqlalchemy as sa
from alembic import op

from polar.integrations.stripe.service import stripe as stripe_service

# Polar Custom Imports
from polar.kit.extensions.sqlalchemy import PostgresUUID

# revision identifiers, used by Alembic.
revision = "40f397ad512a"
down_revision = "8ca7adb6786e"
branch_labels: tuple[str] | None = None
depends_on: tuple[str] | None = None


def upgrade() -> None:
connection = op.get_bind()
result = connection.execute(
sa.text(
"""
SELECT products.id, products.stripe_product_id
FROM products
WHERE stripe_product_id IS NOT NULL;
"""
)
)

for product_id, stripe_product_id in result:
metadata = {
"product_id": str(product_id),
"subscription_tier_id": "",
}
stripe_service.update_product(stripe_product_id, metadata=metadata)


def downgrade() -> None:
connection = op.get_bind()
result = connection.execute(
sa.text(
"""
SELECT products.id, products.stripe_product_id
FROM products
WHERE stripe_product_id IS NOT NULL;
"""
)
)

for product_id, stripe_product_id in result:
metadata = {
"subscription_tier_id": str(product_id),
"product_id": "",
}
stripe_service.update_product(stripe_product_id, metadata=metadata)
10 changes: 2 additions & 8 deletions server/polar/integrations/stripe/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid
from collections.abc import Iterator
from typing import Literal, TypedDict, Unpack, cast
from typing import Literal, Unpack, cast

import stripe as stripe_lib

Expand All @@ -24,12 +24,6 @@
stripe_lib.default_http_client = stripe_http_client


class ProductUpdateKwargs(TypedDict, total=False):
name: str
description: str
default_price: str


class MissingOrganizationBillingEmail(PolarError):
def __init__(self, organization_id: uuid.UUID) -> None:
self.organization_id = organization_id
Expand Down Expand Up @@ -371,7 +365,7 @@ def create_price_for_product(
return price

def update_product(
self, product: str, **kwargs: Unpack[ProductUpdateKwargs]
self, product: str, **kwargs: Unpack[stripe_lib.Product.ModifyParams]
) -> stripe_lib.Product:
return stripe_lib.Product.modify(product, **kwargs)

Expand Down
4 changes: 2 additions & 2 deletions server/polar/product/service/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections.abc import Sequence
from typing import Any, List, Literal, TypeVar # noqa: UP035

import stripe
from sqlalchemy import Select, and_, case, or_, select, update
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.orm import contains_eager, joinedload
Expand All @@ -11,7 +12,6 @@
from polar.authz.service import AccessType, Authz
from polar.benefit.service.benefit import benefit as benefit_service
from polar.exceptions import NotPermitted, PolarError, PolarRequestValidationError
from polar.integrations.stripe.service import ProductUpdateKwargs
from polar.integrations.stripe.service import stripe as stripe_service
from polar.kit.db.postgres import AsyncSession
from polar.kit.pagination import PaginationParams, paginate
Expand Down Expand Up @@ -236,7 +236,7 @@ async def user_update(
if product.is_archived and update_schema.is_archived is False:
product = await self._unarchive(product)

product_update: ProductUpdateKwargs = {}
product_update: stripe.Product.ModifyParams = {}
if update_schema.name is not None and update_schema.name != product.name:
product.name = update_schema.name
product_update["name"] = product.get_stripe_name()
Expand Down

0 comments on commit 8c4d5ee

Please sign in to comment.