diff --git a/src/routing_layer/topo_node_sqlite.c b/src/routing_layer/topo_node_sqlite.c index 7a5f6c0d..d7252205 100644 --- a/src/routing_layer/topo_node_sqlite.c +++ b/src/routing_layer/topo_node_sqlite.c @@ -281,35 +281,45 @@ int topo_node_sqlite_init(sqlite3* db) { return -1; } + /* backfill: вывести deleted из adm_tags (idempotent, самовосстановление) */ { sqlite3_stmt* s = NULL; if (sqlite3_prepare_v2(db, "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'peers\\_%' ESCAPE '\\'", -1, &s, NULL) == SQLITE_OK) { - char isql[256]; while (sqlite3_step(s) == SQLITE_ROW) { const char* tbl = (const char*)sqlite3_column_text(s, 0); - snprintf(isql, sizeof(isql), - "CREATE INDEX IF NOT EXISTS \"idx_%s_type_rtt\" ON \"%s\" (node_type, node_RTT)", tbl, tbl); - sqlite3_exec(db, isql, NULL, NULL, NULL); - snprintf(isql, sizeof(isql), - "ALTER TABLE \"%s\" ADD COLUMN connected INTEGER NOT NULL DEFAULT 0", tbl); - sqlite3_exec(db, isql, NULL, NULL, NULL); - snprintf(isql, sizeof(isql), - "ALTER TABLE \"%s\" ADD COLUMN source INTEGER NOT NULL DEFAULT 0", tbl); - sqlite3_exec(db, isql, NULL, NULL, NULL); - snprintf(isql, sizeof(isql), - "ALTER TABLE \"%s\" ADD COLUMN deleted INTEGER NOT NULL DEFAULT 0", tbl); - sqlite3_exec(db, isql, NULL, NULL, NULL); + if (!tbl) continue; + sqlite3_stmt* sb = NULL; char bsql[256]; + snprintf(bsql, sizeof(bsql), "SELECT node_id, adm_tags, deleted FROM \"%s\"", tbl); + if (sqlite3_prepare_v2(db, bsql, -1, &sb, NULL) == SQLITE_OK) { + sqlite3_stmt* ub = NULL; char usql[256]; + snprintf(usql, sizeof(usql), "UPDATE \"%s\" SET deleted=? WHERE node_id=?", tbl); + int up_ok = sqlite3_prepare_v2(db, usql, -1, &ub, NULL) == SQLITE_OK; + int fixed = 0; + while (sqlite3_step(sb) == SQLITE_ROW) { + uint64_t nid = (uint64_t)sqlite3_column_int64(sb, 0); + const char* tags = (const char*)sqlite3_column_text(sb, 1); + int cur = sqlite3_column_int(sb, 2); + int d = adm_tags_deleted(tags); + if (d != cur) { + if (up_ok) { + sqlite3_bind_int(ub, 1, d); + sqlite3_bind_int64(ub, 2, (sqlite3_int64)nid); + sqlite3_step(ub); sqlite3_reset(ub); + } + fixed++; + } + } + if (ub) sqlite3_finalize(ub); + sqlite3_finalize(sb); + if (fixed) DEBUG_INFO(DEBUG_CATEGORY_BGP, "backfill deleted: tbl=%s fixed=%d", tbl, fixed); + } } sqlite3_finalize(s); } } - sqlite3_exec(db, "ALTER TABLE nodes ADD COLUMN client_type INTEGER NOT NULL DEFAULT 0", NULL, NULL, NULL); - sqlite3_exec(db, "ALTER TABLE node_addresses ADD COLUMN config_type INTEGER DEFAULT 0", NULL, NULL, NULL); - sqlite3_exec(db, "ALTER TABLE node_addresses ADD COLUMN nat_type INTEGER DEFAULT 0", NULL, NULL, NULL); - DEBUG_INFO(DEBUG_CATEGORY_BGP, "topo_node_sqlite tables initialized"); return 0; }