feat(backup): nommage horodaté des clés S3 (#89)
Ajoute les fonctions make_key() et parse_timestamp() pour gérer le format des clés S3 avec horodatage UTC. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
|
||||
_TS_FMT = "%Y%m%dT%H%M%SZ"
|
||||
_KEY_RE = re.compile(r"users-(\d{8}T\d{6}Z)\.sqlite\.gz\.enc$")
|
||||
|
||||
|
||||
def make_key(prefix: str, ts: datetime) -> str:
|
||||
stamp = ts.astimezone(timezone.utc).strftime(_TS_FMT)
|
||||
return f"{prefix.rstrip('/')}/users-{stamp}.sqlite.gz.enc"
|
||||
|
||||
|
||||
def parse_timestamp(key: str) -> datetime:
|
||||
match = _KEY_RE.search(key)
|
||||
if not match:
|
||||
raise ValueError(f"clé non reconnue : {key}")
|
||||
return datetime.strptime(match.group(1), _TS_FMT).replace(tzinfo=timezone.utc)
|
||||
@@ -0,0 +1,25 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
|
||||
from src.backup.naming import make_key, parse_timestamp
|
||||
|
||||
|
||||
def test_make_key_format():
|
||||
ts = datetime(2026, 6, 24, 14, 5, 9, tzinfo=timezone.utc)
|
||||
assert make_key("backups", ts) == "backups/users-20260624T140509Z.sqlite.gz.enc"
|
||||
|
||||
|
||||
def test_make_key_strips_trailing_slash():
|
||||
ts = datetime(2026, 6, 24, 14, 5, 9, tzinfo=timezone.utc)
|
||||
assert make_key("backups/", ts).startswith("backups/users-")
|
||||
|
||||
|
||||
def test_roundtrip():
|
||||
ts = datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone.utc)
|
||||
assert parse_timestamp(make_key("p", ts)) == ts
|
||||
|
||||
|
||||
def test_parse_rejects_unknown_key():
|
||||
with pytest.raises(ValueError):
|
||||
parse_timestamp("backups/users.sqlite.bak")
|
||||
Reference in New Issue
Block a user