Mise un place d'un système DIY de migrations DB
This commit is contained in:
@@ -9,6 +9,7 @@ CREATE TABLE IF NOT EXISTS subscriptions (
|
||||
frisbii_customer_handle TEXT,
|
||||
frisbii_subscription_handle TEXT,
|
||||
plan TEXT,
|
||||
prix_ht REAL,
|
||||
status TEXT,
|
||||
current_period_end TEXT,
|
||||
trial_used INTEGER NOT NULL DEFAULT 0,
|
||||
@@ -31,16 +32,19 @@ def init_schema() -> None:
|
||||
get_conn().executescript(SUBSCRIPTIONS_SCHEMA)
|
||||
|
||||
|
||||
def create_pending(user_id: int, customer_handle: str, plan: str) -> None:
|
||||
def create_pending(
|
||||
user_id: int, customer_handle: str, plan: str, prix_ht: float | None = None
|
||||
) -> None:
|
||||
now = _now()
|
||||
get_conn().execute(
|
||||
"INSERT INTO subscriptions "
|
||||
"(user_id, frisbii_customer_handle, plan, status, created_at, updated_at) "
|
||||
"VALUES (?, ?, ?, 'pending', ?, ?) "
|
||||
"(user_id, frisbii_customer_handle, plan, prix_ht, status, created_at, updated_at) "
|
||||
"VALUES (?, ?, ?, ?, 'pending', ?, ?) "
|
||||
"ON CONFLICT(user_id) DO UPDATE SET "
|
||||
"frisbii_customer_handle=excluded.frisbii_customer_handle, "
|
||||
"plan=excluded.plan, status='pending', updated_at=excluded.updated_at",
|
||||
(user_id, customer_handle, plan, now, now),
|
||||
"plan=excluded.plan, prix_ht=excluded.prix_ht, "
|
||||
"status='pending', updated_at=excluded.updated_at",
|
||||
(user_id, customer_handle, plan, prix_ht, now, now),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -27,8 +27,11 @@ def subscribe():
|
||||
|
||||
cust = _customer_handle(current_user.id)
|
||||
try:
|
||||
meta = plans.plan_meta(plan_key)
|
||||
client.get_or_create_customer(cust, current_user.email)
|
||||
db.create_pending(current_user.id, cust, plan_key)
|
||||
db.create_pending(
|
||||
current_user.id, cust, plan_key, meta["prix_ht"] if meta else None
|
||||
)
|
||||
# Anti-abus : pas de nouvel essai si l'utilisateur en a déjà consommé un.
|
||||
no_trial = db.has_used_trial(current_user.id)
|
||||
url = client.create_subscription_session(
|
||||
|
||||
@@ -2,6 +2,7 @@ import os
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from src import migrations
|
||||
from src.subscriptions import db
|
||||
from src.utils import logger
|
||||
|
||||
@@ -15,6 +16,7 @@ _REQUIRED_ENV = (
|
||||
|
||||
def init_subscriptions(app: Flask) -> None:
|
||||
db.init_schema()
|
||||
migrations.apply_pending()
|
||||
|
||||
from src.subscriptions.routes import subscriptions_bp, webhook
|
||||
|
||||
|
||||
Reference in New Issue
Block a user