Cadangan otomatis DB berskema lama tepat sebelum migrasi go-live
init_db menyalin /data/app.db → /data/app.db.sebelum-golive-<waktu> (sqlite backup API, konsisten) hanya bila skema masih OBE-Mapping (peran dosen_rps_mk) — sekali, di volume persisten. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
2bab18fdf3
commit
99a1655f56
@@ -8,3 +8,4 @@ __pycache__/
|
|||||||
tests/*.db
|
tests/*.db
|
||||||
tests/*.xlsx
|
tests/*.xlsx
|
||||||
tests/*.log
|
tests/*.log
|
||||||
|
tests/*.db.*
|
||||||
|
|||||||
@@ -310,7 +310,29 @@ def get_conn() -> sqlite3.Connection:
|
|||||||
return conn
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def _cadangkan_sebelum_migrasi() -> None:
|
||||||
|
"""Basis data berskema lama (OBE-Mapping: peran dosen_mk/dosen_rps_mk) dicadangkan utuh
|
||||||
|
SEBELUM migrasi pertama — salinan ada di volume yang sama: {DB_PATH}.sebelum-golive-<waktu>.
|
||||||
|
Rollback = hentikan aplikasi, kembalikan salinan ini ke DB_PATH, deploy kode lama."""
|
||||||
|
if not os.path.exists(DB_PATH):
|
||||||
|
return
|
||||||
|
sumber = sqlite3.connect(DB_PATH)
|
||||||
|
try:
|
||||||
|
row = sumber.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='pengguna'").fetchone()
|
||||||
|
if not row or "dosen_rps_mk" not in row[0]:
|
||||||
|
return
|
||||||
|
tujuan = f"{DB_PATH}.sebelum-golive-{datetime.now(WIB).strftime('%Y%m%d-%H%M%S')}"
|
||||||
|
salinan = sqlite3.connect(tujuan)
|
||||||
|
with salinan:
|
||||||
|
sumber.backup(salinan) # konsisten walau ada penulisan berjalan
|
||||||
|
salinan.close()
|
||||||
|
print(f"[migrasi] cadangan skema lama disimpan: {tujuan}", flush=True)
|
||||||
|
finally:
|
||||||
|
sumber.close()
|
||||||
|
|
||||||
|
|
||||||
def init_db() -> None:
|
def init_db() -> None:
|
||||||
|
_cadangkan_sebelum_migrasi()
|
||||||
with get_conn() as conn:
|
with get_conn() as conn:
|
||||||
for tabel in ("rps", "portofolio", "nilai"):
|
for tabel in ("rps", "portofolio", "nilai"):
|
||||||
conn.execute(
|
conn.execute(
|
||||||
|
|||||||
@@ -22,9 +22,19 @@ krs0 = sorted(tuple(r) for r in src.execute("SELECT nim, nama, mk_kode, kelas, t
|
|||||||
tugas0 = [dict(r) for r in src.execute("SELECT p.user_id, p.mk_kode, p.kelas, u.role FROM penugasan p JOIN pengguna u ON u.id=p.user_id")]
|
tugas0 = [dict(r) for r in src.execute("SELECT p.user_id, p.mk_kode, p.kelas, u.role FROM penugasan p JOIN pengguna u ON u.id=p.user_id")]
|
||||||
token = src.execute("SELECT s.token, s.user_id FROM sesi s ORDER BY s.rowid DESC LIMIT 1").fetchone()
|
token = src.execute("SELECT s.token, s.user_id FROM sesi s ORDER BY s.rowid DESC LIMIT 1").fetchone()
|
||||||
|
|
||||||
|
import glob
|
||||||
|
for f in glob.glob(SALIN + ".sebelum-golive-*"):
|
||||||
|
os.remove(f)
|
||||||
from app import db
|
from app import db
|
||||||
db.init_db(); db.init_db() # dua kali: idempoten
|
db.init_db(); db.init_db() # dua kali: idempoten
|
||||||
cek(True, "init_db berjalan (2×) tanpa galat")
|
cek(True, "init_db berjalan (2×) tanpa galat")
|
||||||
|
cad = glob.glob(SALIN + ".sebelum-golive-*")
|
||||||
|
cek(len(cad) == 1, f"cadangan otomatis sebelum migrasi dibuat tepat sekali ({len(cad)})")
|
||||||
|
if cad:
|
||||||
|
cc = sqlite3.connect(cad[0])
|
||||||
|
cek("dosen_rps_mk" in cc.execute("SELECT sql FROM sqlite_master WHERE name='pengguna'").fetchone()[0]
|
||||||
|
and cc.execute("SELECT COUNT(*) FROM rps").fetchone()[0] == len(rps0), "cadangan berisi skema & data lama utuh")
|
||||||
|
cc.close()
|
||||||
c = db.get_conn()
|
c = db.get_conn()
|
||||||
akun1 = {r["id"]: dict(r) for r in c.execute("SELECT * FROM pengguna")}
|
akun1 = {r["id"]: dict(r) for r in c.execute("SELECT * FROM pengguna")}
|
||||||
cek(set(akun1) == set(akun0), f"{len(akun1)} akun, ID sama")
|
cek(set(akun1) == set(akun0), f"{len(akun1)} akun, ID sama")
|
||||||
|
|||||||
Reference in New Issue
Block a user