Skip to content

Commit 9549214

Browse files
feat: add date range filtering to stripe-backfill script (#2177)
- Add --created-gte and --created-lte CLI arguments for unix timestamps - Add workflow_dispatch inputs for date range parameters - Allows running backfill for specific time ranges to avoid rate limits Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: yujonglee <yujonglee.dev@gmail.com>
1 parent 54f1157 commit 9549214

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed

.github/workflows/stripe_backfill.yaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
on:
22
workflow_dispatch:
3+
inputs:
4+
created_gte:
5+
description: "Only sync objects created at or after this unix timestamp (optional)"
6+
required: false
7+
type: string
8+
created_lte:
9+
description: "Only sync objects created at or before this unix timestamp (optional)"
10+
required: false
11+
type: string
312

413
jobs:
514
backfill:
@@ -11,7 +20,15 @@ jobs:
1120
- uses: ./.github/actions/pnpm_install
1221
- uses: ./.github/actions/infisical_install
1322
- run: pnpm --filter @hypr/api install
14-
- run: infisical run --token="$INFISICAL_TOKEN" --env=prod --projectId="$INFISICAL_PROJECT_ID" --path="/stripe-backfill" -- bun apps/api/src/scripts/stripe-backfill.ts
23+
- run: |
24+
ARGS=""
25+
if [ -n "${{ inputs.created_gte }}" ]; then
26+
ARGS="$ARGS --created-gte ${{ inputs.created_gte }}"
27+
fi
28+
if [ -n "${{ inputs.created_lte }}" ]; then
29+
ARGS="$ARGS --created-lte ${{ inputs.created_lte }}"
30+
fi
31+
infisical run --token="$INFISICAL_TOKEN" --env=prod --projectId="$INFISICAL_PROJECT_ID" --path="/stripe-backfill" -- bun apps/api/src/scripts/stripe-backfill.ts $ARGS
1532
env:
1633
INFISICAL_TOKEN: ${{ secrets.INFISICAL_TOKEN }}
1734
INFISICAL_PROJECT_ID: ${{ secrets.INFISICAL_PROJECT_ID }}

apps/api/src/scripts/stripe-backfill.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,74 @@
11
import { StripeSync } from "@supabase/stripe-sync-engine";
2+
import { parseArgs } from "util";
23

34
const { DATABASE_URL, STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET } = Bun.env;
45

56
if (!DATABASE_URL || !STRIPE_SECRET_KEY || !STRIPE_WEBHOOK_SECRET) {
67
throw new Error("Missing required environment variables");
78
}
89

10+
const { values } = parseArgs({
11+
args: Bun.argv.slice(2),
12+
options: {
13+
"created-gte": {
14+
type: "string",
15+
},
16+
"created-lte": {
17+
type: "string",
18+
},
19+
help: {
20+
type: "boolean",
21+
short: "h",
22+
},
23+
},
24+
strict: true,
25+
allowPositionals: false,
26+
});
27+
28+
if (values.help) {
29+
console.log(`
30+
Usage: bun stripe-backfill.ts [options]
31+
32+
Options:
33+
--created-gte <timestamp> Only sync objects created at or after this unix timestamp
34+
--created-lte <timestamp> Only sync objects created at or before this unix timestamp
35+
-h, --help Show this help message
36+
37+
Examples:
38+
bun stripe-backfill.ts
39+
bun stripe-backfill.ts --created-gte 1704067200
40+
bun stripe-backfill.ts --created-gte 1704067200 --created-lte 1706745600
41+
`);
42+
process.exit(0);
43+
}
44+
45+
const createdGte = values["created-gte"]
46+
? parseInt(values["created-gte"], 10)
47+
: undefined;
48+
const createdLte = values["created-lte"]
49+
? parseInt(values["created-lte"], 10)
50+
: undefined;
51+
52+
if (values["created-gte"] && isNaN(createdGte!)) {
53+
throw new Error(
54+
"Invalid --created-gte value: must be a valid unix timestamp",
55+
);
56+
}
57+
58+
if (values["created-lte"] && isNaN(createdLte!)) {
59+
throw new Error(
60+
"Invalid --created-lte value: must be a valid unix timestamp",
61+
);
62+
}
63+
64+
const created: { gte?: number; lte?: number } | undefined =
65+
createdGte !== undefined || createdLte !== undefined
66+
? {
67+
...(createdGte !== undefined && { gte: createdGte }),
68+
...(createdLte !== undefined && { lte: createdLte }),
69+
}
70+
: undefined;
71+
972
const sync = new StripeSync({
1073
poolConfig: {
1174
connectionString: DATABASE_URL,
@@ -18,6 +81,13 @@ const sync = new StripeSync({
1881
backfillRelatedEntities: true,
1982
});
2083

21-
console.log("Starting Stripe backfill...");
22-
await sync.syncBackfill({ object: "all" });
84+
if (created) {
85+
console.log(
86+
`Starting Stripe backfill with date filter: ${JSON.stringify(created)}`,
87+
);
88+
} else {
89+
console.log("Starting Stripe backfill (all objects)...");
90+
}
91+
92+
await sync.syncBackfill({ object: "all", created });
2393
console.log("Backfill complete.");

0 commit comments

Comments
 (0)