Skip to content
Merged
81 changes: 81 additions & 0 deletions .bernd-restore-296475.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Restore soft-deleted original user 296475 (szrnr1453@gmail.com) and re-link
* her Google identity from the two throwaway duplicates (1041778, 1041838).
*
* RUN: php .bernd-restore-296475.php -> dry run (no writes)
* php .bernd-restore-296475.php --apply -> apply (writes)
*
* Backs up all three rows to .bernd-backup-296475-<ts>.json before writing.
*/

require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use App\User;
use Illuminate\Support\Facades\DB;

$apply = in_array('--apply', $argv, true);
$email = 'szrnr1453@gmail.com';
$primary = 296475;
$dupes = [1041778, 1041838];

$rows = DB::table('users')->whereIn('id', [$primary, ...$dupes])->get();
echo "rows found: ".$rows->count().PHP_EOL;
foreach ($rows as $r) {
echo sprintf(" %d | deleted_at=%s | provider=%s | provider_id=%s | created=%s\n",
$r->id, $r->deleted_at ?? 'NULL', $r->provider ?? '-', $r->provider_id ?? '-', $r->created_at);
}

$primaryRow = $rows->firstWhere('id', $primary);
if (! $primaryRow) { exit("ABORT: primary $primary not found\n"); }
if ($primaryRow->deleted_at === null) { echo "NOTE: $primary already active (deleted_at NULL)\n"; }

// Google identity to adopt: prefer the newest duplicate that actually has one.
$source = null;
foreach ($dupes as $d) {
$r = $rows->firstWhere('id', $d);
if ($r && $r->deleted_at !== null && ! empty($r->provider_id)) {
$source = $r;
}
}

echo "google identity source: ".($source ? $source->id.' ('.$source->provider.'/'.$source->provider_id.')' : 'none').PHP_EOL;

$certTable = 'CertificatesOfExcellence';
try {
$certCount = DB::table($certTable)->where('user_id', $primary)->count();
echo "$certTable for $primary: $certCount".PHP_EOL;
foreach (DB::table($certTable)->whereIn('user_id', [$primary, ...$dupes])->get() as $c) {
echo " cert id=".($c->id ?? '?').' user_id='.$c->user_id.PHP_EOL;
}
} catch (\Throwable $e) {
echo 'cert lookup skipped: '.$e->getMessage().PHP_EOL;
}

if (! $apply) {
echo PHP_EOL."DRY RUN — nothing written. Re-run with --apply.\n";
exit(0);
}

$backup = __DIR__.'/.bernd-backup-'.$primary.'-'.date('YmdHis').'.json';
file_put_contents($backup, json_encode($rows, JSON_PRETTY_PRINT));
echo "backup: $backup".PHP_EOL;

DB::transaction(function () use ($primary, $source) {
$update = ['deleted_at' => null];
if ($source) {
$update['provider'] = $source->provider;
$update['provider_id'] = $source->provider_id;
}
DB::table('users')->where('id', $primary)->update($update);
});

$after = DB::table('users')->where('id', $primary)->first();
echo sprintf("after: %d | deleted_at=%s | provider=%s | provider_id=%s\n",
$after->id, $after->deleted_at ?? 'NULL', $after->provider ?? '-', $after->provider_id ?? '-');

$active = DB::table('users')->where('id', $primary)->whereNull('deleted_at')->exists();
echo "restore_ok=".($active ? 'YES' : 'NO').PHP_EOL;
echo "duplicates left soft-deleted: ".(DB::table('users')->whereIn('id', [1041778, 1041838])->whereNotNull('deleted_at')->count())."/2".PHP_EOL;
64 changes: 64 additions & 0 deletions .tmp_cert_backfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
// One-off backfill: generate missing event certificates ONLY for activities
// that the organiser actually reported (reported_at set) but whose
// certificate_url is still empty. Replicates ReportController:54 exactly.
require '/home/forge/codeweek.eu/vendor/autoload.php';
$app = require '/home/forge/codeweek.eu/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

use App\Certificate;
use App\Event;

$limit = (int) ($argv[1] ?? 25);
$pauseMs = (int) ($argv[2] ?? 0);
$failFile = '/tmp/cert_backfill_failed.txt';
$failed = [];
if (file_exists($failFile)) {
foreach (file($failFile) as $line) {
$line = trim($line);
if ($line !== '') {
$failed[(int) $line] = true;
}
}
}

$done = 0;
$errors = 0;
while (true) {
$q = Event::where('status', 'approved')
->whereNotNull('reported_at')
->where(function ($q) {
$q->whereNull('certificate_url')->orWhere('certificate_url', '');
});
if (! empty($failed)) {
$q->whereNotIn('id', array_keys($failed));
}
$ids = $q->orderBy('id')->limit($limit)->pluck('id')->all();
if (empty($ids)) {
break;
}
foreach ($ids as $id) {
$event = Event::find($id);
if (! $event) {
continue;
}
try {
$t0 = microtime(true);
$url = (new Certificate($event))->generate();
if (empty($event->fresh()->certificate_url)) {
$event->update(['certificate_url' => $url]);
}
$done++;
echo sprintf("[%s] ok id=%d %.1fs %s\n", date('H:i:s'), $id, microtime(true) - $t0, $url);
} catch (\Throwable $e) {
$errors++;
$failed[$id] = true;
file_put_contents($failFile, $id . PHP_EOL, FILE_APPEND);
echo sprintf("[%s] FAIL id=%d %s\n", date('H:i:s'), $id, substr($e->getMessage(), 0, 200));
}
if ($pauseMs) {
usleep($pauseMs * 1000);
}
}
}
echo "DONE ok={$done} errors={$errors}\n";
Loading
Loading