T17: backend penyimpanan bersama db.py (sqlite | mysql via PyMySQL)
- db.py: abstraksi connect/query/queryone/replace_snapshot; %s → ? utk sqlite; replace snapshot mysql = CREATE IF NOT EXISTS + DELETE + INSERT (MVCC) - config/.env.example: VEX_DB_BACKEND (default sqlite) + VEX_DB_HOST/PORT/USER/PASS/NAME - get_voters.persist → db.replace_snapshot; dashboard baca via db.query - LIKE pakai ESCAPE '!' (backslash memutus literal string MySQL, B2); db.query selalu kembalikan list (PyMySQL tuple vs sqlite list, B3) - docker-compose.yml: kontainer uji mariadb:11 (databisnisid-mariadb, 127.0.0.1:3306) - test_mariadb.py: suite integrasi opt-in (skip bila bukan mysql) - SPEC.md §V17/T17/B2/B3; AGENTS.md runbook
This commit is contained in:
1 parent
063fb5e5eb
commit
fdb5d0de15
12 files changed
+307
-79
No files matched your search
+7
-26
@@ -5,14 +5,14 @@ dibuang. Berjalan imparsial (repeatable) dan siap dijadwalkan (cron).
|
||||
"""
|
||||
|
||||
import math
|
||||
import sqlite3
|
||||
import sys
|
||||
import time
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
import requests
|
||||
|
||||
from config import API_NODE, DB_PATH, MIN_STAKED_VEX, TARGET_BP, VEX_STALE_DAYS
|
||||
import db
|
||||
from config import API_NODE, MIN_STAKED_VEX, TARGET_BP, VEX_STALE_DAYS
|
||||
|
||||
# Kontrak & scope sistem Vexanium = vexcore (BUKAN vexio sesuai konvensi EOS)
|
||||
CODE = 'vexcore'
|
||||
@@ -145,33 +145,14 @@ def normalize(row, now=None):
|
||||
|
||||
|
||||
def persist(rows):
|
||||
"""V7: ganti snapshot — DROP+INSERT tiap skan, ⊥ history/append.
|
||||
"""V7: ganti snapshot — tiap skan ⊥ history/append.
|
||||
|
||||
WAL: journal_mode=WAL → pembaca (dashboard multi-worker) tak terblokir
|
||||
saat DROP+INSERT harian. Mode ini persisten setelah diset.
|
||||
Delegasi ke `db.replace_snapshot` (backend sqlite/mysql):
|
||||
sqlite DROP+CREATE dalam WAL → pembaca (dashboard multi-worker) tak
|
||||
terblokir; mysql DELETE+INSERT dalam satu transaksi (MVCC snapshot).
|
||||
"""
|
||||
scanned_at = datetime.now().isoformat(timespec='seconds')
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
try:
|
||||
conn.execute('PRAGMA journal_mode=WAL')
|
||||
conn.execute('DROP TABLE IF EXISTS voters') # snapshot bersih
|
||||
conn.execute(
|
||||
'CREATE TABLE voters (' # I.db
|
||||
'owner TEXT PRIMARY KEY, '
|
||||
'weight TEXT NOT NULL, '
|
||||
'staked REAL NOT NULL, '
|
||||
'scanned_at TEXT NOT NULL, '
|
||||
'last_vote TEXT)'
|
||||
)
|
||||
conn.executemany(
|
||||
'INSERT INTO voters (owner, weight, staked, scanned_at, last_vote) '
|
||||
'VALUES (?, ?, ?, ?, ?)',
|
||||
[(r['owner'], r['weight'], r['staked'], scanned_at, r.get('last_vote'))
|
||||
for r in rows],
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
db.replace_snapshot(rows, scanned_at)
|
||||
return scanned_at
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user