Migra a koigrid

Mueve tu stack desde Heroku/AWS/Vercel/Railway/Render pieza a pieza — cada paso es reversible. Postgres migra casi sin downtime con replicación lógica (CDC): haces el cutover cuando el lag es 0.

Desde Heroku

La mudanza entera en cuatro órdenes. Sube TAL CUAL el latest.dump que te da heroku pg:backups:download (es pg_dump en formato custom; koigrid lo convierte). Heroku Postgres no puede replicar a una base de fuera, así que la mudanza es un volcado y una restauración: pon la app en modo mantenimiento mientras haces el volcado final. La línea web: de tu Procfile es la orden de arranque y PORT se inyecta; una línea worker: es otra app de tipo worker del mismo repo; una orden release: va en koigrid.json (releaseCommand). Una base nueva nace con el tamaño más pequeño (su disco crece solo); pide más memoria o una réplica cuando la necesites.

# 1) data: Heroku backups are pg_dump CUSTOM format —
#    upload latest.dump as it is (koigrid converts it).
#    No replication out of Heroku: freeze writes for the final dump
heroku maintenance:on -a <app>
heroku pg:backups:capture -a <app>
heroku pg:backups:download -a <app>          # → latest.dump
# 2) a database (the smallest by default: 512 MB, 10 GB that grows,
#    no replica), then your data → row counts per table
curl -X POST https://koigrid.com/api/v1/databases \
  -H "Authorization: Bearer $KOIGRID_TOKEN" -H 'content-type: application/json' \
  -d '{"name":"<name>","memoryMb":512,"diskGb":10,"replicas":0}'
koigrid db restore-dump <db-id> --file latest.dump --wait
# 3) the app: Procfile "web:" is the start command and PORT
#    is injected. Copy each var from "heroku config -s" except
#    DATABASE_URL/REDIS_URL (the reference points at the new db)
koigrid apps deploy <name> --dir . \
  --env 'DATABASE_URL=${{db.<name>.DATABASE_URL}}' --env KEY=value
#    a Procfile "worker:" line is its own app of kind worker
#    (same repo, no port or URL), with the same vars
koigrid apps deploy <name>-worker --dir . --worker \
  --start-cmd '<your worker: command>' \
  --env 'DATABASE_URL=${{db.<name>.DATABASE_URL}}' --env KEY=value
# 4) your domain, once https://<slug>.koigrid.app works: the
#    exact DNS record, plus an optional _acme-challenge CNAME
#    so the certificate is ready BEFORE you switch
koigrid apps domains <app-id> add www.example.com

Desde Railway

Railway corre Postgres con el superusuario postgres, así que puedes mudarte casi sin corte por replicación lógica: activa wal_level=logical (ALTER SYSTEM y reinicia el servicio; compruébalo con SHOW wal_level y, si se revierte, arranca Postgres con -c wal_level=logical como hace la propia plantilla de ElectricSQL de Railway), publica y suscríbete desde koigrid. ¿Base pequeña o te vale una ventana de mantenimiento? Entonces volcado y restauración. Activa Public Networking en el Postgres de Railway para tener DATABASE_PUBLIC_URL.

# near-zero downtime: logical replication (Railway Postgres runs as superuser)
psql "$DATABASE_PUBLIC_URL" -c "ALTER SYSTEM SET wal_level = logical"
# restart the Postgres service in Railway, then check it stuck:
psql "$DATABASE_PUBLIC_URL" -c "SHOW wal_level"     # → logical
psql "$DATABASE_PUBLIC_URL" -c "CREATE PUBLICATION mig FOR TABLES IN SCHEMA public"
# schema first, then preflight, subscribe and wait for cutover.ready (see Postgres below)
pg_dump --schema-only --no-owner --no-privileges "$DATABASE_PUBLIC_URL" | psql "$DIRECT_URL"
koigrid db subs <db-id> preflight --source "$DATABASE_PUBLIC_URL" --publication mig
koigrid db subs <db-id> create mig --source "$DATABASE_PUBLIC_URL" --publication mig --origin none
# or, with a short maintenance window: dump and restore
pg_dump --no-owner --no-acl -Fc "$DATABASE_PUBLIC_URL" > dump.dump
koigrid db restore-dump <db-id> --file dump.dump --wait

Desde Render

Vuelca desde la External Database URL de la base y súbelo: el formato custom ocupa poco y restore-dump lo acepta tal cual. El pg_dump -n public que documenta Render deja fuera los CREATE EXTENSION; koigrid crea solo las extensiones que tu volcado usa (uuid-ossp, citext, pg_trgm…). La exportación del panel (.dir.tar.gz, formato directorio) también se acepta: súbela tal cual; se desempaqueta junto a tu base sólo si cabe con margen. La replicación lógica desde Render pide un workspace Pro, una base de al menos 10 GB y pedírselo a su soporte.

# External Database URL from the Render dashboard (Connect → External)
pg_dump --no-owner --no-acl -Fc "$RENDER_EXTERNAL_DATABASE_URL" > dump.dump
koigrid db restore-dump <db-id> --file dump.dump --wait   # extensions created for you
# env vars: copy them from the service's Environment tab, pointing
# DATABASE_URL at the reference instead
koigrid apps deploy <name> --dir . \
  --env 'DATABASE_URL=${{db.<name>.DATABASE_URL}}' --env KEY=value

1. Postgres

RESTORE GESTIONADO (lo más fácil): sube tu dump y koigrid lo importa — POST /databases/:id/restore-dump/upload-url {gzip:true} → PUT del .sql.gz → POST /databases/:id/restore-dump {dumpKey}, o koigrid db restore-dump <id> --file dump.sql --wait (el CLI comprime y sube en streaming). Pre-siembra las extensiones (arregla el desajuste de extensions.vector de Supabase), falla rápido con la CAUSA RAÍZ, es atómico (un fallo deja la BD intacta, así que el reintento da la misma causa), conserva tu dump para que reintentar no cueste re-subirlo, y da la propiedad a tu rol de app para que los datos se lean desde el primer momento. Alternativa manual: dump + restore. Casi sin downtime: la replicación lógica vuelca tu origen en koigrid en streaming; cutover con lag 0. Para un restore grande, conéctate al puerto DIRECTO de escritura (no al pool de PgBouncer) — el pooling en modo transacción rompe COPY y los restores largos.

# 1) Create the managed database — match your SOURCE major version (15, 16, 17) to avoid dump mismatches
koigrid db create prod --version 17
DATABASE_URL="$(koigrid db connection <id> --json | jq -r .connection.uri)"

# --- Simple (dump + restore) ---
# For a BIG restore, connect to the DIRECT write port (connection.directUri), NOT the pool: transaction
# pooling breaks COPY and long restores (missing tables/indexes).
pg_dump "$SOURCE_URL" | psql "$DATABASE_URL"        # or: drizzle-kit migrate / prisma migrate deploy
# ex-Supabase? install extensions into their schema:
#   POST /databases/:id/extensions {"name":"uuid-ossp","schema":"extensions"}  (koigrid does it as superuser)

# --- Near-zero-downtime (logical replication / CDC) — the managed flow, measured on a real 66 GB RDS ---
# a) SOURCE: logical replication on (RDS: parameter group with rds.logical_replication=1 → one reboot, ~1 min
#    with Multi-AZ failover), a replication role WITH BYPASSRLS and SELECT on every table, and a publication:
#      CREATE ROLE koi_repl LOGIN PASSWORD '…' BYPASSRLS;  GRANT rds_replication TO koi_repl;   -- RDS
#      GRANT SELECT ON ALL TABLES IN SCHEMA public TO koi_repl;
#      ALTER DEFAULT PRIVILEGES FOR ROLE <owner> IN SCHEMA public GRANT SELECT ON TABLES TO koi_repl;
#      CREATE PUBLICATION mig FOR TABLES IN SCHEMA public;
#    and let koigrid's database host reach your source on 5432 (security group / firewall).
# b) copy the SCHEMA to koigrid first (tables must exist), then FREEZE schema changes on the source until cutover:
pg_dump --schema-only --no-owner --no-privileges "$SOURCE_URL" | psql "$DIRECT_URL"
# c) PREFLIGHT — it catches the 4 traps that otherwise give a wrong or stuck copy with no visible error:
#    RLS without BYPASSRLS · NOT VALID CHECKs · schema drift · a role that cannot SELECT a table
koigrid db subs <id> preflight --source "$SOURCE_REPL_URL" --publication mig
# d) subscribe (refused if the preflight blocks). origin none = ready for a two-way rollback later:
koigrid db subs <id> create mig --source "$SOURCE_REPL_URL" --publication mig --origin none
# e) wait for the verdict — read it, do not infer it from the lag (tables copy in parallel, lag reads ~0 meanwhile):
koigrid db subs <id> ls          # → tablas 268/268 · cutover.ready=true
#    Budget DISK: the initial copy writes ~2x your data in WAL; koigrid grows the disk on its own, but plan for it.
#    Could not freeze the schema? Replication copies ROWS, not DDL: apply each change on koigrid too (a missing
#    column stops the whole apply). A NEW table is not replicated until you create it here and refresh:
koigrid db subs <id> update mig --refresh
# f) VERIFY row by row (the only check that catches a table copied to zero by RLS). It is a job: it counts in
#    batches, each with its own time limit enforced by Postgres, and the CLI waits until it finishes.
#    --count-source = a READ REPLICA of your source, to keep count(*) off your primary:
koigrid db subs <id> verify --source "$SOURCE_REPL_URL" --publication mig --count-source "$SOURCE_REPLICA_URL"
# --- After migrating: automatic password rotation (AWS Secrets Manager rotation substitute) ---
# ZERO-DOWNTIME dual-role rotation. One-time: switch the pooler to reloadable mode, then schedule:
koigrid db rotation <id> migrate                 # brief reconnect, once
koigrid db rotation <id> set --interval 30       # rotate every 30 days (or: koigrid db rotation <id> rotate)
# apps wiring DATABASE_URL='${{db.<name>.DATABASE_URL}}' auto-redeploy with the fresh password.

2. Almacenamiento (S3)

Tu bucket ya habla S3 — copia los objetos con rclone o aws-cli y reapunta el endpoint + claves de tu cliente.

koigrid storage buckets create assets
koigrid storage keys                     # → accessKey / secretKey / endpoint (shown once)

# copy objects from your old S3 (rclone remote 'koigrid' → https://s3.koigrid.com, path-style)
rclone copy s3old:my-bucket koigrid:assets

CERO cambios de código: koigrid soporta virtual-hosted-style (bucket.s3.koigrid.com), así que el AWS SDK estándar funciona tal cual — pon AWS_ENDPOINT_URL_S3 + tus claves como variables de entorno y deja tu código igual:

# Your app's S3 code stays EXACTLY as-is. Just set env vars:
AWS_ENDPOINT_URL_S3=https://s3.koigrid.com
AWS_ACCESS_KEY_ID=<from koigrid storage keys>
AWS_SECRET_ACCESS_KEY=<from koigrid storage keys>
AWS_REGION=us-east-1                # any value; koigrid is region-agnostic

# koigrid supports virtual-hosted-style (bucket.s3.koigrid.com), so the standard
# new S3Client() works with no forcePathStyle and no code change. Use the bucketName
# koigrid returns (koi-<org>-<name>-xxxx) as the Bucket.

3. App

Despliega desde un repo git, una imagen de contenedor, o tu carpeta local (--dir, sin git ni Docker). Fija env vars y luego un dominio propio. El CDN está activo por defecto y cachea tu HTML en el edge honrando el Cache-Control (envía s-maxage=N; el ISR de Next.js ya lo hace) — latencia de borde estilo CloudFront, sin config extra; las apps protegidas y los previews nunca se cachean en el edge.

# from a git repo (add --repo-token for a private repo):
koigrid apps deploy web --repo https://github.com/you/app
# ...or straight from your local folder (no git, no Docker):
koigrid apps deploy web --dir ./
# a background worker (Render Background Worker, Heroku worker dyno): no port, no URL
koigrid apps deploy worker --dir ./ --worker --start-cmd 'node worker.js'

# env vars (invalid lines are reported, not fatal; saved even before the first deploy):
curl -X POST $API/apps/<id>/env -H "Authorization: Bearer koi_KEY" \
  -d '{"env":{"DATABASE_URL":"...","API_KEY":"..."}}'

# custom domain + automatic TLS:
koigrid apps domains web app.yourdomain.com

4. Cron jobs

Tus endpoints de EventBridge/cron pasan a jobs HTTP — koigrid pega a la URL según el cron con tu cabecera secreta.

# an EventBridge/cron endpoint → an HTTP job (koigrid calls the URL on schedule):
koigrid jobs create nightly --schedule "0 3 * * *" \
  --url https://app.yourdomain.com/api/cron/report --header "x-cron-secret: $SECRET"

# ...or a container job:
koigrid jobs create backup --schedule "0 2 * * *" --image alpine --command "sh -c '…'"

5. Cutover

La base de datos es la única puerta de un solo sentido, así que la vuelta atrás se arma ANTES del cambio: una suscripción inversa (koigrid → tu base antigua, origin none) hace que volver atrás no pierda ninguna escritura. Después: parar escrituras, esperar retraso 0, promover koigrid, sincronizar las secuencias y cambiar el tráfico. Conserva lo antiguo 48-72 h.

# 0) BEFORE the night: your app running on koigrid in parallel (pointing at the replica, read-only checks),
#    and the ROLLBACK armed: a publication on koigrid + a subscription on your OLD database, origin none,
#    copy_data false — so every write made on koigrid after the switch also lands on the old one.
koigrid db pub <id> add back --all-tables
#      (on the OLD database)  CREATE SUBSCRIPTION back CONNECTION '<koigrid DIRECT_URL>' PUBLICATION back
#                             WITH (copy_data = false, origin = none, enabled = false);
# 1) stop writes on the old database (maintenance or read-only) and wait for lag 0:
koigrid db subs <id> ls
# 2) PROMOTE koigrid: stop applying the old database's changes, and enable the way back:
koigrid db subs <id> update mig --disable
#      (on the OLD database)  ALTER SUBSCRIPTION back ENABLE;
# 3) sequences are NOT replicated — without this the first INSERT fails with 'duplicate key'. Rehearse, then apply:
koigrid db subs <id> sync-sequences --source "$SOURCE_REPL_URL" --dry-run
koigrid db subs <id> sync-sequences --source "$SOURCE_REPL_URL"
#    and REFRESH MATERIALIZED VIEW where you have them.
# 4) switch the traffic: the origin of your CDN / load balancer → the koigrid app (seconds to minutes, no DNS),
#    or DNS with a low TTL. Watch errors and latency for 30 min.
# ROLLBACK: point the traffic back. The old database already has every write made on koigrid.
# 5) once the switch looks good, DELETE the forward subscription. The rollback does not need it (it uses 'back'),
#    and while it exists — even disabled — its replication slot on the OLD database retains every WAL segment and
#    fills that disk (measured: ~1 GB/h on a busy app). Deleting it drops that slot; check slotRemoto in the answer:
koigrid db subs <id> rm mig
#    (if it says the slot is orphaned, run the SELECT pg_drop_replication_slot(…) it prints, on the OLD database)
# Keep the old stack for 48-72 h before deleting anything else. Outbound bandwidth is included (never billed).