Files
Investidor-V2/outputs/investmoz-parques-QA-20261016/backup-restore-QA.py
T
nala cef6e201c7
tests / ci (push) Canceled after 0s
versao nrgocios 2
2026-10-16 20:11:05 +02:00

20 lines
2.1 KiB
Python

from pathlib import Path
import subprocess,json,hashlib,shutil
out=Path(__file__).resolve().parent;private=Path('C:/Users/macbook/.config/investmoz/parques-QA-20261016');qa=private/'source';restore=private/'restored-source';pg=Path('C:/Program Files/PostgreSQL/17/bin');args=['-h','127.0.0.1','-p','55441','-U','postgres'];db='parks_operational_restore_20261016_test'
assert not restore.exists() and db.endswith('_test');dump=private/'after-use-QA.dump'
subprocess.run([str(pg/'pg_dump.exe'),*args,'-d','parks_operational_20261016_test','-Fc','-f',str(dump)],check=True,capture_output=True)
subprocess.run([str(pg/'createdb.exe'),*args,db],check=True,capture_output=True)
subprocess.run([str(pg/'pg_restore.exe'),*args,'-d',db,'--exit-on-error','--no-owner','--no-privileges',str(dump)],check=True,capture_output=True)
for folder in ['app','config','routes','database','resources','bootstrap','public/build','storage']:shutil.copytree(qa/folder,restore/folder)
for name in ['artisan','composer.json','composer.lock','package.json','package-lock.json'] :shutil.copy2(qa/name,restore/name)
(restore/'.env').write_text((qa/'.env').read_text('utf-8').replace('parks_operational_20261016_test',db),'utf-8')
subprocess.run(['powershell','-NoProfile','-Command',f"New-Item -ItemType Junction -Path '{restore/'vendor'}' -Target '{qa/'vendor'}' | Out-Null"],check=True,capture_output=True)
boot=(out/'boot-QA.php').read_text('utf-8').replace("parques-QA-20261016/source'","parques-QA-20261016/restored-source'").replace('parks_operational_20261016_test',db)
(out/'boot-restored-QA.php').write_text(boot,'utf-8')
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest();files={}
for p in(qa/'storage/app').rglob('*'):
if p.is_file():
rel=p.relative_to(qa).as_posix();assert sha(p)==sha(restore/rel);files[rel]=sha(p)
record={'source_database':'parks_operational_20261016_test','restored_database':db,'port':55441,'dump_sha256':sha(dump),'private_dump':True,'private_files_restored':len(files),'files_equal':True,'configuration_and_key_private':True,'active_changes':0}
(out/'BACKUP-RESTAURO-QA.json').write_text(json.dumps(record,indent=2)+'\n','utf-8');print(json.dumps(record))