<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Where the router was last told to send the traffic.
 *
 * The hostname list was already compared before the router file is rewritten, so
 * an address that had changed would be re-applied — but the same file names a
 * BACKEND, and nothing compared that. cloud-init gives every guest `ip=dhcp`, so
 * a lease expiry or a cold boot can move the machine; the router went on pointing
 * at the address it had, the customer's cloud answered 502, and no run anywhere
 * would rewrite it, because the hostnames still matched and `route_written` still
 * said a file existed.
 *
 * Left null for every existing row on purpose: null does not match the backend
 * ConfigureDnsAndTls computes, so the first run to touch each instance rewrites
 * its router once and records what it wrote. Backfilling it from `guest_ip` would
 * be asserting that the file on the host says what we hope it says, which is the
 * assumption this column exists to stop making.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('instances', function (Blueprint $table) {
            $table->string('routed_backend')->nullable()->after('routed_hostnames');
        });
    }

    public function down(): void
    {
        Schema::table('instances', function (Blueprint $table) {
            $table->dropColumn('routed_backend');
        });
    }
};
