fix: exclude never-paid Stripe statuses from active subscriptions; keep URL options for test DB bootstrap (#327)

This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 21:57:04 -05:00
1 parent f6a3dd6b9c
commit 23e4f021bc
3 files changed
+27 -6

No files matched your search

@@ -1,6 +1,6 @@
{
"db_name": "MySQL",
"query": "\nSELECT\n user_token,\n subscription_namespace as `subscription_namespace: enums::common::payments_namespace::PaymentsNamespace`,\n subscription_product_slug,\n subscription_expires_at\n\nFROM user_subscriptions\n\nWHERE\n user_token = ?\n AND subscription_expires_at > CURRENT_TIMESTAMP\n ORDER BY id ASC\n ",
"query": "\nSELECT\n user_token,\n subscription_namespace as `subscription_namespace: enums::common::payments_namespace::PaymentsNamespace`,\n subscription_product_slug,\n subscription_expires_at\n\nFROM user_subscriptions\n\nWHERE\n user_token = ?\n AND subscription_expires_at > CURRENT_TIMESTAMP\n AND (maybe_stripe_subscription_status IS NULL\n OR maybe_stripe_subscription_status NOT IN ('incomplete', 'incomplete_expired', 'unpaid'))\n ORDER BY id ASC\n ",
"describe": {
"columns": [
{
@@ -54,5 +54,5 @@
false
]
},
"hash": "680d60fcda0f6fdf269f3525a3fe582db847e56ca7380c0ff5696cbd6efb7db3"
"hash": "3cda37ea79d02f8a7cf4a678c61d54c4fcd8274a98301da734b1e032284c7171"
}
@@ -28,6 +28,8 @@ pub async fn list_active_user_subscriptions<'e, 'c : 'e, E>(
// but the CRC is wrong. Eg. what happened with ftx on 2022.11.18.
//
// https://stripe.com/docs/billing/subscriptions/overview#subscription-statuses
//
// Those handlers still assign a future expiry, so exclude statuses that were never paid.
let records = sqlx::query_as!(
RawActiveUserSubscription,
r#"
@@ -42,6 +44,8 @@ FROM user_subscriptions
WHERE
user_token = ?
AND subscription_expires_at > CURRENT_TIMESTAMP
AND (maybe_stripe_subscription_status IS NULL
OR maybe_stripe_subscription_status NOT IN ('incomplete', 'incomplete_expired', 'unpaid'))
ORDER BY id ASC
"#,
user_token,
@@ -80,12 +80,21 @@ async fn create_database_if_missing(url: &str, database: &str) {
}
}
/// `mysql://user:pass@host:port/db?params` → `mysql://user:pass@host:port`
/// `mysql://user:pass@host:port/db?params` → `mysql://user:pass@host:port?params`
/// (query options such as TLS settings must survive; only the database segment goes).
fn strip_database_from_url(url: &str) -> String {
match url.rfind('/') {
let (base, query) = match url.split_once('?') {
Some((base, query)) => (base, Some(query)),
None => (url, None),
};
let base = match base.rfind('/') {
// Position 7 is the end of "mysql://" — a URL with no database path.
Some(idx) if idx > "mysql://".len() => url[..idx].to_string(),
_ => url.to_string(),
Some(idx) if idx > "mysql://".len() => &base[..idx],
_ => base,
};
match query {
Some(query) => format!("{base}?{query}"),
None => base.to_string(),
}
}
@@ -101,6 +110,14 @@ mod tests {
);
}
#[test]
fn keeps_query_options() {
assert_eq!(
strip_database_from_url("mysql://u:p@localhost:3306/artcraft_test?ssl-mode=required&ssl-ca=/etc/ca.pem"),
"mysql://u:p@localhost:3306?ssl-mode=required&ssl-ca=/etc/ca.pem",
);
}
#[test]
fn leaves_database_free_urls_alone() {
assert_eq!(