fix(db): Добавлены новые таблицы для хранения информации о новых и старых ценах при использовании промокодов на скидку

This commit is contained in:
VAQYBIN
2026-01-20 00:16:24 +05:00
parent d619afff29
commit 62bf5c35a8
2 changed files with 439 additions and 0 deletions
+61
View File
@@ -112,6 +112,62 @@ def _migration_0003_normalize_referral_codes(connection: Connection) -> None:
)
)
def _migration_0004_add_discount_promo_codes(connection: Connection) -> None:
inspector = inspect(connection)
# 1. Добавить поля в payments
payment_columns: Set[str] = {col["name"] for col in inspector.get_columns("payments")}
if "original_amount" not in payment_columns:
connection.execute(text("ALTER TABLE payments ADD COLUMN original_amount FLOAT"))
if "discount_applied" not in payment_columns:
connection.execute(text("ALTER TABLE payments ADD COLUMN discount_applied FLOAT"))
# 2. Модифицировать promo_codes
promo_columns: Set[str] = {col["name"] for col in inspector.get_columns("promo_codes")}
if "promo_type" not in promo_columns:
connection.execute(
text(
"ALTER TABLE promo_codes ADD COLUMN promo_type VARCHAR NOT NULL DEFAULT 'bonus_days'"
)
)
if "discount_percentage" not in promo_columns:
connection.execute(
text("ALTER TABLE promo_codes ADD COLUMN discount_percentage INTEGER")
)
# Изменить bonus_days на nullable (если еще не nullable)
connection.execute(
text("ALTER TABLE promo_codes ALTER COLUMN bonus_days DROP NOT NULL")
)
# Создать индекс на promo_type
connection.execute(
text(
"CREATE INDEX IF NOT EXISTS idx_promo_codes_promo_type ON promo_codes (promo_type)"
)
)
# 3. Создать таблицу active_discounts
connection.execute(
text(
"""
CREATE TABLE IF NOT EXISTS active_discounts (
user_id BIGINT PRIMARY KEY,
promo_code_id INTEGER NOT NULL,
discount_percentage INTEGER NOT NULL,
activated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT fk_active_discounts_user
FOREIGN KEY (user_id) REFERENCES users (user_id) ON DELETE CASCADE,
CONSTRAINT fk_active_discounts_promo_code
FOREIGN KEY (promo_code_id) REFERENCES promo_codes (promo_code_id) ON DELETE CASCADE
)
"""
)
)
MIGRATIONS: List[Migration] = [
Migration(
id="0001_add_channel_subscription_fields",
@@ -128,6 +184,11 @@ MIGRATIONS: List[Migration] = [
description="Normalize referral codes to uppercase for consistent lookups",
upgrade=_migration_0003_normalize_referral_codes,
),
Migration(
id="0004_add_discount_promo_codes",
description="Add support for percentage discount promo codes",
upgrade=_migration_0004_add_discount_promo_codes,
),
]