-- Keep existing plans bound to the old revision. Only advance the original -- bootstrap template; a newer operator revision retains its position. CREATE FUNCTION seed_windows_restart_receipt(target_tenant uuid) RETURNS void LANGUAGE plpgsql SET search_path = pg_catalog, public AS $seed$ DECLARE previous_tenant text := coalesce(current_setting('app.tenant_id', true), ''); BEGIN PERFORM set_config('app.tenant_id', target_tenant::text, true); INSERT INTO public.script_revisions(tenant_id, name, version, interpreter, execution_identity, created_by, script_utf8) SELECT target_tenant, 'Windows neu starten', 2, 'powershell', 'system', 'system:bootstrap', $ps$$ErrorActionPreference = 'Stop' $exe = [IO.Path]::Combine([Environment]::SystemDirectory, 'shutdown.exe') $process = Start-Process -FilePath $exe -ArgumentList '/r /t 60 /d p:0:0 /c "Genehmigter Neustart durch die Geraeteverwaltung"' -NoNewWindow -Wait -PassThru if ($null -eq $process -or $null -eq $process.ExitCode) { throw 'Windows hat keinen Exitcode gemeldet.' } if ($process.ExitCode -ne 0) { throw ('Windows hat den Neustart abgelehnt. Exitcode ' + $process.ExitCode) } Write-Output 'Neustart in einer Minute eingeplant.' $ps$ WHERE EXISTS (SELECT 1 FROM public.script_revisions WHERE tenant_id=target_tenant AND name='Windows neu starten' AND version=1 AND created_by='system:bootstrap' AND execution_identity='system' AND interpreter='powershell') AND NOT EXISTS (SELECT 1 FROM public.script_revisions WHERE tenant_id=target_tenant AND name='Windows neu starten' AND version>1) ON CONFLICT (tenant_id, name, version) DO NOTHING; PERFORM set_config('app.tenant_id', previous_tenant, true); END $seed$; REVOKE ALL ON FUNCTION seed_windows_restart_receipt(uuid) FROM PUBLIC; CREATE OR REPLACE FUNCTION seed_tenant_script_library() RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public AS $$ DECLARE previous_tenant text := coalesce(current_setting('app.tenant_id', true), ''); BEGIN PERFORM set_config('app.tenant_id', NEW.tenant_id::text, true); PERFORM public.seed_windows_update_script(NEW.tenant_id); PERFORM public.seed_standard_diagnostic_scripts(NEW.tenant_id); PERFORM public.seed_restart_scripts(NEW.tenant_id); PERFORM public.seed_windows_restart_receipt(NEW.tenant_id); PERFORM set_config('app.tenant_id', previous_tenant, true); RETURN NEW; END $$; DO $backfill$ BEGIN LOCK TABLE tenants IN SHARE ROW EXCLUSIVE MODE; PERFORM public.seed_windows_restart_receipt(tenant_id) FROM tenants; END $backfill$;