From 21444d3826b3f3361c424e8cd031a2ba0ad2ead8 Mon Sep 17 00:00:00 2001 From: mirivlad Date: Wed, 3 Jun 2026 10:48:19 +0800 Subject: [PATCH] sshkeeper: fix migration - route_hops via ensureSchema() to avoid duplicate column error --- internal/db/db.go | 16 ++++++++++++++++ internal/db/migrations/002_routes.sql | 12 +++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/internal/db/db.go b/internal/db/db.go index 3593dce..bb2d857 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -44,6 +44,7 @@ func Open(dataDir string) (*DB, error) { } func (db *DB) ensureSchema() error { + // Add startup_command column hasStartupCommand, err := db.hasColumn("servers", "startup_command") if err != nil { return err @@ -54,6 +55,21 @@ func (db *DB) ensureSchema() error { } } + // Add route_hops column + hasRouteHops, err := db.hasColumn("servers", "route_hops") + if err != nil { + return err + } + if !hasRouteHops { + if _, err := db.conn.Exec("ALTER TABLE servers ADD COLUMN route_hops TEXT NOT NULL DEFAULT ''"); err != nil { + return fmt.Errorf("add route_hops: %w", err) + } + // Migrate existing ProxyJump values into route_hops + if _, err := db.conn.Exec("UPDATE servers SET route_hops = proxy_jump WHERE proxy_jump != ''"); err != nil { + return fmt.Errorf("migrate proxy_jump to route_hops: %w", err) + } + } + _, err = db.conn.Exec(` CREATE TABLE IF NOT EXISTS global_command_templates ( id INTEGER PRIMARY KEY AUTOINCREMENT, diff --git a/internal/db/migrations/002_routes.sql b/internal/db/migrations/002_routes.sql index 2ea8b91..1486b7c 100644 --- a/internal/db/migrations/002_routes.sql +++ b/internal/db/migrations/002_routes.sql @@ -1,10 +1,4 @@ -- v0.2.0: Add route support --- Adds route columns to servers table and migrates existing ProxyJump data. - -ALTER TABLE servers ADD COLUMN route_hops TEXT NOT NULL DEFAULT ''; - --- Migrate existing ProxyJump values into Route.Hops (all as raw addresses). --- ProxyJump format: "host1,host2,host3" → each becomes a RouteHop with IsProfile=false. --- We store as a simple comma-separated list of raw addresses in route_hops for now. --- The application layer will parse this into Route.Hops on read. -UPDATE servers SET route_hops = proxy_jump WHERE proxy_jump != '' AND route_hops = ''; +-- Note: The route_hops column is now added programmatically in ensureSchema() +-- in db.go to handle idempotent migrations. This file is kept for reference. +-- No SQL operations needed here. \ No newline at end of file