-
Notifications
You must be signed in to change notification settings - Fork 652
Apply discounts to existing subscriptions #8780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+201
−42
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7b659b5
Fix discount expiration when applied to existing subscriptions
claude c194ad9
Add test for repeating discounts with trial periods
claude 07b1a56
Update server/polar/models/subscription.py
pieterbeulque 77b3c29
Add data backfill for discount_applied_at
claude 90a2c1d
Rebase migration
pieterbeulque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
server/migrations/versions/2026-01-06-1200_add_discount_applied_at_to_subscriptions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """Add discount_applied_at to subscriptions | ||
|
|
||
| Revision ID: ea11a3dc85a2 | ||
| Revises: 3d212567b9a6 | ||
| Create Date: 2026-01-06 12:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # Polar Custom Imports | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "ea11a3dc85a2" | ||
| down_revision = "3c48bf325744" | ||
| branch_labels: tuple[str] | None = None | ||
| depends_on: tuple[str] | None = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.add_column( | ||
| "subscriptions", | ||
| sa.Column("discount_applied_at", sa.TIMESTAMP(timezone=True), nullable=True), | ||
| ) | ||
|
|
||
| # Backfill discount_applied_at for existing subscriptions with discounts | ||
| # by finding the first order that used the discount | ||
| op.execute( | ||
| """ | ||
| UPDATE subscriptions s | ||
| SET discount_applied_at = o.created_at | ||
| FROM ( | ||
| SELECT DISTINCT ON (o.subscription_id, o.discount_id) | ||
| o.subscription_id, | ||
| o.discount_id, | ||
| o.created_at | ||
| FROM orders o | ||
| WHERE o.subscription_id IS NOT NULL | ||
| AND o.discount_id IS NOT NULL | ||
| AND o.deleted_at IS NULL | ||
| ORDER BY o.subscription_id, o.discount_id, o.created_at ASC | ||
| ) o | ||
| WHERE s.id = o.subscription_id | ||
| AND s.discount_id = o.discount_id | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_column("subscriptions", "discount_applied_at") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.