From aefa1c6fe8968bf6a401085a0b4b50481db5ed09 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 2 Mar 2026 11:08:45 +0800 Subject: [PATCH] feat: enhance system information display with database version - Added `db_version` field to the `SystemInfo` interface to expose the current database migration version. - Updated the system information response to include the database version, reflecting its state during application runtime. - Enhanced the UI in the SystemInfo component to display the database version with appropriate labels and descriptions in multiple languages. This update improves transparency regarding the database state within the system information settings. --- .air.toml | 2 +- frontend/src/api/system/index.ts | 1 + frontend/src/i18n/locales/en-US.ts | 2 ++ frontend/src/i18n/locales/ko-KR.ts | 2 ++ frontend/src/i18n/locales/ru-RU.ts | 2 ++ frontend/src/i18n/locales/zh-CN.ts | 2 ++ frontend/src/views/settings/SystemInfo.vue | 11 ++++++++++ internal/database/migration.go | 24 ++++++++++++++++++++++ internal/handler/system.go | 11 ++++++++++ scripts/dev.sh | 3 ++- 10 files changed, 58 insertions(+), 2 deletions(-) diff --git a/.air.toml b/.air.toml index 8a7afb91..4e2e9398 100644 --- a/.air.toml +++ b/.air.toml @@ -5,7 +5,7 @@ tmp_dir = "tmp" [build] args_bin = [] bin = "./tmp/main" - cmd = "CGO_CFLAGS='-Wno-deprecated-declarations -Wno-gnu-folding-constant' CGO_LDFLAGS='-Wl,-no_warn_duplicate_libraries' go build -ldflags=\"-X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'\" -o ./tmp/main ./cmd/server" + cmd = "CGO_CFLAGS='-Wno-deprecated-declarations -Wno-gnu-folding-constant' CGO_LDFLAGS='-Wl,-no_warn_duplicate_libraries' go build -ldflags=\"$(./scripts/get_version.sh ldflags) -X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'\" -o ./tmp/main ./cmd/server" delay = 1000 exclude_dir = ["assets", "tmp", "vendor", "testdata", "frontend", "migrations", "node_modules", "docs"] exclude_file = [] diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 4c18d8c2..3fe63fcd 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -10,6 +10,7 @@ export interface SystemInfo { vector_store_engine?: string graph_database_engine?: string minio_enabled?: boolean + db_version?: string } export interface ToolDefinition { diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index d13cd223..73120b2a 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -1698,6 +1698,8 @@ export default { buildTimeDescription: 'Time when the system was built', goVersionLabel: 'Go Version', goVersionDescription: 'Go language version used by the backend', + dbVersionLabel: 'Database Version', + dbVersionDescription: 'Current database migration version', keywordIndexEngineLabel: 'Keyword Index Engine', keywordIndexEngineDescription: 'Currently used keyword index engine', vectorStoreEngineLabel: 'Vector Store Engine', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index a57e4df8..101a468b 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -1194,6 +1194,8 @@ export default { buildTimeDescription: "시스템이 빌드된 시간", goVersionLabel: "Go 버전", goVersionDescription: "백엔드에서 사용하는 Go 언어 버전", + dbVersionLabel: "데이터베이스 버전", + dbVersionDescription: "현재 데이터베이스 마이그레이션 버전", keywordIndexEngineLabel: "키워드 인덱스 엔진", keywordIndexEngineDescription: "현재 사용 중인 키워드 인덱스 엔진", vectorStoreEngineLabel: "벡터 저장소 엔진", diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 6a4f0b9f..c8b37f6b 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -815,6 +815,8 @@ export default { buildTimeDescription: 'Время, когда система была собрана', goVersionLabel: 'Версия Go', goVersionDescription: 'Версия языка Go, используемая backend', + dbVersionLabel: 'Версия базы данных', + dbVersionDescription: 'Текущая версия миграции базы данных', keywordIndexEngineLabel: 'Движок индексации ключевых слов', keywordIndexEngineDescription: 'Используемый в настоящее время движок индексации ключевых слов', vectorStoreEngineLabel: 'Движок векторного хранилища', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 02bf576e..ed9521fc 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -1174,6 +1174,8 @@ export default { buildTimeDescription: "系统构建的时间", goVersionLabel: "Go 版本", goVersionDescription: "后端使用的 Go 语言版本", + dbVersionLabel: "数据库版本", + dbVersionDescription: "当前数据库迁移版本号", keywordIndexEngineLabel: "关键词索引引擎", keywordIndexEngineDescription: "当前使用的关键词索引引擎", vectorStoreEngineLabel: "向量存储引擎", diff --git a/frontend/src/views/settings/SystemInfo.vue b/frontend/src/views/settings/SystemInfo.vue index 07b72cbe..95fb6991 100644 --- a/frontend/src/views/settings/SystemInfo.vue +++ b/frontend/src/views/settings/SystemInfo.vue @@ -67,6 +67,17 @@ + +
+
+ +

{{ $t('system.dbVersionDescription') }}

+
+
+ {{ systemInfo.db_version }} +
+
+
diff --git a/internal/database/migration.go b/internal/database/migration.go index f8f4ec35..5c836b2e 100644 --- a/internal/database/migration.go +++ b/internal/database/migration.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strings" + "sync" "github.com/Tencent/WeKnora/internal/logger" "github.com/golang-migrate/migrate/v4" @@ -13,6 +14,27 @@ import ( _ "github.com/golang-migrate/migrate/v4/source/file" ) +var ( + currentMigrationVersion uint + currentMigrationDirty bool + migrationVersionOnce sync.Once + migrationVersionSet bool +) + +// CachedMigrationVersion returns the migration version captured at startup. +// Returns (version, dirty, ok). ok is false if the version was never captured. +func CachedMigrationVersion() (uint, bool, bool) { + return currentMigrationVersion, currentMigrationDirty, migrationVersionSet +} + +func setMigrationVersion(version uint, dirty bool) { + migrationVersionOnce.Do(func() { + currentMigrationVersion = version + currentMigrationDirty = dirty + migrationVersionSet = true + }) +} + // RunMigrations executes all pending database migrations // This should be called during application startup func RunMigrations(dsn string) error { @@ -142,6 +164,8 @@ func RunMigrationsWithOptions(dsn string, opts MigrationOptions) error { return fmt.Errorf("failed to get migration version: %w", err) } + setMigrationVersion(version, dirty) + if oldVersion != version { logger.Infof(ctx, "Database migrated from version %d to %d", oldVersion, version) } else { diff --git a/internal/handler/system.go b/internal/handler/system.go index 7725dde7..ab52d5ee 100644 --- a/internal/handler/system.go +++ b/internal/handler/system.go @@ -11,6 +11,7 @@ import ( "github.com/Tencent/WeKnora/internal/application/service/file" "github.com/Tencent/WeKnora/internal/config" + "github.com/Tencent/WeKnora/internal/database" "github.com/Tencent/WeKnora/internal/infrastructure/docparser" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/types" @@ -48,6 +49,7 @@ type GetSystemInfoResponse struct { VectorStoreEngine string `json:"vector_store_engine,omitempty"` GraphDatabaseEngine string `json:"graph_database_engine,omitempty"` MinioEnabled bool `json:"minio_enabled,omitempty"` + DBVersion string `json:"db_version,omitempty"` } // 编译时注入的版本信息 @@ -82,6 +84,14 @@ func (h *SystemHandler) GetSystemInfo(c *gin.Context) { // Get MinIO enabled status minioEnabled := h.isMinioConfigured(c) + var dbVersion string + if ver, dirty, ok := database.CachedMigrationVersion(); ok { + dbVersion = fmt.Sprintf("%d", ver) + if dirty { + dbVersion += " (dirty)" + } + } + response := GetSystemInfoResponse{ Version: Version, Edition: Edition, @@ -92,6 +102,7 @@ func (h *SystemHandler) GetSystemInfo(c *gin.Context) { VectorStoreEngine: vectorStoreEngine, GraphDatabaseEngine: graphDatabaseEngine, MinioEnabled: minioEnabled, + DBVersion: dbVersion, } logger.Info(ctx, "System info retrieved successfully") diff --git a/scripts/dev.sh b/scripts/dev.sh index ec21f62c..2b750cb1 100755 --- a/scripts/dev.sh +++ b/scripts/dev.sh @@ -282,7 +282,8 @@ start_app() { log_info "未检测到 Air,使用普通模式启动" log_warning "提示: 安装 Air 可以实现代码修改后自动重启" log_info "安装命令: go install github.com/air-verse/air@latest" - go run -ldflags="-X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'" cmd/server/main.go + LDFLAGS="$(./scripts/get_version.sh ldflags) -X 'google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'" + go run -ldflags="$LDFLAGS" cmd/server/main.go fi }