style: 统一 Ruff 代码格式
This commit is contained in:
@@ -91,7 +91,7 @@ def encrypt_value(value: str | None) -> str | None:
|
||||
|
||||
|
||||
def _decrypt_with_candidate(value: str, candidate: _KeyCandidate) -> str:
|
||||
raw = _b64d(value[len(_PREFIX):])
|
||||
raw = _b64d(value[len(_PREFIX) :])
|
||||
if len(raw) < 28:
|
||||
raise ValueError("密文字段长度无效")
|
||||
nonce = raw[:12]
|
||||
@@ -112,7 +112,9 @@ def decrypt_value(value: str | None) -> str | None:
|
||||
return _decrypt_with_candidate(value, candidate)
|
||||
except Exception as exc:
|
||||
last_error = exc
|
||||
raise ValueError("敏感字段解密失败,请确认 APP_ENCRYPTION_KEY 是否正确") from last_error
|
||||
raise ValueError(
|
||||
"敏感字段解密失败,请确认 APP_ENCRYPTION_KEY 是否正确"
|
||||
) from last_error
|
||||
|
||||
|
||||
def decrypt_value_with_key_name(value: str) -> tuple[str, str]:
|
||||
@@ -125,7 +127,9 @@ def decrypt_value_with_key_name(value: str) -> tuple[str, str]:
|
||||
return _decrypt_with_candidate(value, candidate), candidate.name
|
||||
except Exception as exc:
|
||||
last_error = exc
|
||||
raise ValueError("敏感字段解密失败,请确认 APP_ENCRYPTION_KEY 是否正确") from last_error
|
||||
raise ValueError(
|
||||
"敏感字段解密失败,请确认 APP_ENCRYPTION_KEY 是否正确"
|
||||
) from last_error
|
||||
|
||||
|
||||
class EncryptedText(TypeDecorator):
|
||||
@@ -184,9 +188,15 @@ def encrypt_existing_sensitive_data(engine: Engine) -> int:
|
||||
for table_name, column_name in _SENSITIVE_COLUMNS:
|
||||
if not _table_exists(engine, table_name):
|
||||
continue
|
||||
rows = conn.execute(
|
||||
text(f"SELECT id, {column_name} FROM {table_name} WHERE {column_name} IS NOT NULL")
|
||||
).mappings().all()
|
||||
rows = (
|
||||
conn.execute(
|
||||
text(
|
||||
f"SELECT id, {column_name} FROM {table_name} WHERE {column_name} IS NOT NULL"
|
||||
)
|
||||
)
|
||||
.mappings()
|
||||
.all()
|
||||
)
|
||||
for row in rows:
|
||||
raw_value = row[column_name]
|
||||
if raw_value is None or raw_value == "":
|
||||
@@ -204,7 +214,9 @@ def encrypt_existing_sensitive_data(engine: Engine) -> int:
|
||||
continue
|
||||
encrypted = encrypt_value(plain_value)
|
||||
conn.execute(
|
||||
text(f"UPDATE {table_name} SET {column_name} = :value WHERE id = :id"),
|
||||
text(
|
||||
f"UPDATE {table_name} SET {column_name} = :value WHERE id = :id"
|
||||
),
|
||||
{"value": encrypted, "id": row["id"]},
|
||||
)
|
||||
changed += 1
|
||||
|
||||
Reference in New Issue
Block a user