Files
triliumnext-korean-patch/tools/test-database.py
T

21 lines
2.0 KiB
Python

"""격리된 배포 검사 폴더의 DB만 읽습니다. 운영 DB 경로는 허용하지 않습니다."""
import sqlite3,pathlib,json,hashlib,re,sys
root=pathlib.Path('/tmp/trilium-release-test-20260913-r1').resolve();dbpath=(root/'data-r2/document.db').resolve();assert dbpath.is_relative_to(root)
db=sqlite3.connect('file:'+str(dbpath)+'?mode=ro',uri=True);db.row_factory=sqlite3.Row
notes=[dict(n) for n in db.execute('SELECT noteId,title,type,blobId FROM notes WHERE isDeleted=0')];ids={n['noteId'] for n in notes}
pdfs=[dict(n) for n in db.execute("SELECT n.title,b.content FROM notes n JOIN blobs b ON n.blobId=b.blobId WHERE n.type='file' AND n.mime='application/pdf' AND n.isDeleted=0")]
expected='1141ea4fb9d619fd55d75d0bdcd6bbd5678b161633586764d0eadfc4b9235400';assert any(hashlib.sha256(n['content']).hexdigest()==expected for n in pdfs)
missing=[]
for n in db.execute("SELECT n.title,b.content FROM notes n JOIN blobs b ON n.blobId=b.blobId WHERE n.type='text' AND n.isDeleted=0"):
text=n['content'].decode() if isinstance(n['content'],bytes) else n['content'] or ''
for link in re.findall(r'href="#root/([^"#?]+)"',text):
if link.split('/')[-1] not in ids:missing.append([n['title'],link])
assert not missing,missing
assert sum(1 for n in notes if n['noteId'].startswith('_help_'))>=181
assert any(n['title']=='일지' for n in notes)
digest=hashlib.sha256(json.dumps(sorted((n['noteId'],n['title'],n['blobId']) for n in notes),ensure_ascii=False).encode()).hexdigest()
result={'activeNotes':len(notes),'koreanTitles':sum(bool(re.search('[가-힣]',n['title'])) for n in notes),'helpNotes':sum(n['noteId'].startswith('_help_') for n in notes),'koreanPdf':True,'danglingInternalLinks':len(missing),'noteSnapshotSha256':digest}
if len(sys.argv)>1 and sys.argv[1]=='save':(root/'before-update.json').write_text(json.dumps(result))
elif len(sys.argv)>1 and sys.argv[1]=='compare':assert json.loads((root/'before-update.json').read_text())['noteSnapshotSha256']==digest
print(json.dumps(result));db.close()