fix(users): update referral code handling to ensure uniqueness and proper formatting
Implement logic to clean and standardize referral codes in the users table. This includes setting referral codes to NULL if they are empty or consist only of whitespace, and generating unique referral codes for users with duplicates. The changes enhance data integrity and ensure consistent formatting of referral codes across the database.
This commit is contained in:
+40
-11
@@ -67,6 +67,45 @@ def _run_legacy_migrator_compatibility(connection: Connection) -> None:
|
||||
for column in inspect(connection).get_columns("users")
|
||||
}
|
||||
if "referral_code" in users_columns:
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE users
|
||||
SET referral_code = NULLIF(UPPER(BTRIM(referral_code)), '')
|
||||
WHERE referral_code IS NOT NULL
|
||||
"""
|
||||
)
|
||||
)
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
WITH duplicate_codes AS (
|
||||
SELECT
|
||||
user_id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY referral_code
|
||||
ORDER BY user_id
|
||||
) AS rn
|
||||
FROM users
|
||||
WHERE referral_code IS NOT NULL
|
||||
)
|
||||
UPDATE users AS u
|
||||
SET referral_code = UPPER(
|
||||
SUBSTRING(
|
||||
md5(
|
||||
u.user_id::text
|
||||
|| clock_timestamp()::text
|
||||
|| random()::text
|
||||
)
|
||||
FROM 1 FOR 9
|
||||
)
|
||||
)
|
||||
FROM duplicate_codes AS d
|
||||
WHERE u.user_id = d.user_id
|
||||
AND d.rn > 1
|
||||
"""
|
||||
)
|
||||
)
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
@@ -84,7 +123,7 @@ def _run_legacy_migrator_compatibility(connection: Connection) -> None:
|
||||
)
|
||||
) AS referral_code
|
||||
FROM users
|
||||
WHERE referral_code IS NULL OR referral_code = ''
|
||||
WHERE referral_code IS NULL
|
||||
)
|
||||
UPDATE users AS u
|
||||
SET referral_code = g.referral_code
|
||||
@@ -102,16 +141,6 @@ def _run_legacy_migrator_compatibility(connection: Connection) -> None:
|
||||
"""
|
||||
)
|
||||
)
|
||||
connection.execute(
|
||||
text(
|
||||
"""
|
||||
UPDATE users
|
||||
SET referral_code = UPPER(referral_code)
|
||||
WHERE referral_code IS NOT NULL
|
||||
AND referral_code <> UPPER(referral_code)
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
db_inspector = inspect(connection)
|
||||
if db_inspector.has_table("payments"):
|
||||
|
||||
@@ -225,7 +225,14 @@ async def mark_provider_payment_processing_once(
|
||||
Payment.status != "processing",
|
||||
]
|
||||
if expected_status_prefix:
|
||||
conditions.append(Payment.status.like(f"{expected_status_prefix}%"))
|
||||
if expected_status_prefix == "pending":
|
||||
# YooKassa may keep authorized payments in waiting_for_capture
|
||||
# before reporting a final successful capture webhook.
|
||||
conditions.append(
|
||||
Payment.status.in_(("waiting_for_capture", "pending", "pending_yookassa"))
|
||||
)
|
||||
else:
|
||||
conditions.append(Payment.status.like(f"{expected_status_prefix}%"))
|
||||
|
||||
stmt = (
|
||||
update(Payment)
|
||||
|
||||
Reference in New Issue
Block a user