Files
Investidor-V2/outputs/investmoz-referencias-economicas-ativacao-20261016/revert-compatible.py
T
nala 2d23157e40
tests / ci (push) Canceled after 0s
versao nrgocios
2026-10-16 13:06:58 +02:00

40 lines
2.9 KiB
Python

"""Prepared rollback only. Never writes database data or automatically withdraws publication."""
from pathlib import Path
import json,hashlib,datetime,subprocess,shutil,os,sys
root=Path(__file__).resolve().parents[2];out=Path(__file__).resolve().parent
assert sys.argv[1:]==['--candidate=referencias-economicas-QA-966202f15577']
m=json.loads((out/'para-revisao/MANIFESTO-APLICADO.json').read_text('utf-8'))
b=json.loads((out/'FRESH-BACKUP.json').read_text('utf-8'));backup=Path(b['private_directory']).resolve()
sha=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
assert backup.parent==Path('C:/Users/macbook/.config/investmoz').resolve()
assert sha(backup/'destination.dump')==b['dump_sha256'] and sha(backup/'destination-files.zip')==b['files_sha256']
assert not (out/'REVERSAO-EXECUTADA.json').exists() and not (root/'storage/framework/down').exists()
for s in m['application']+m['build']:assert sha(root/s['path'])==s['sha256'],'Drift: reconcile before rollback'
routes=next(x for x in m['application'] if x['path']=='routes/web.php')
assert sha(backup/'files/routes/web.php')==routes['before_sha256']
retained=m['compatible_rollback']['retained_artifacts'];remove=m['compatible_rollback']['remove']
assert len(retained)==9 and len(remove)==3
php='C:/laragon/bin/php/php-8.5.11-Win32-vs17-x64/php.exe'
def artisan(*args):
p=subprocess.run([php,'artisan',*args],cwd=root,capture_output=True);assert p.returncode==0,'Artisan failed; inspect private output, preserve maintenance state'
def put(src,dest):
assert dest.resolve().is_relative_to(root.resolve());tmp=dest.with_name(dest.name+'.compatible-rollback.tmp');assert not tmp.exists()
shutil.copy2(src,tmp);assert sha(tmp)==sha(src);os.replace(tmp,dest)
hold=backup/'operational-code-before-compatible-rollback';assert not hold.exists();hold.mkdir()
for n in ['routes/web.php',*remove]:
p=hold/n;p.parent.mkdir(parents=True,exist_ok=True);shutil.copy2(root/n,p)
try:
artisan('down','--retry=60');put(backup/'files/routes/web.php',root/'routes/web.php')
for n in remove:
p=(root/n).resolve();assert p.is_relative_to(root.resolve());expected=next(x for x in m['application'] if x['path']==n)['sha256'];assert sha(p)==expected;p.unlink()
artisan('view:clear')
for s in m['application']:
if s['path'] in retained:assert sha(root/s['path'])==s['sha256']
for s in m['build']:assert sha(root/s['path'])==s['sha256']
artisan('up')
except BaseException:
for n in ['routes/web.php',*remove]:put(hold/n,root/n)
artisan('view:clear');artisan('up');raise
r={'at':datetime.datetime.now(datetime.timezone.utc).isoformat(),'candidate':m['candidate'],'mode':'partial compatible after use','restored':['routes/web.php'],'removed':remove,'retained':retained,'entire_candidate_build_retained':162,'database_writes':0,'publication_withdrawn':False,'data_cache_clear_executed':False}
(out/'REVERSAO-EXECUTADA.json').write_text(json.dumps(r,indent=2)+'\n','utf-8');print(json.dumps(r))