refactor(tkg): extract shared t() function to schema module

- Create src/core/db/schema.rs with t() and table_name() functions
- Remove duplicate t() from tkg.rs and trace_agent_api.rs
- postgres_db.rs uses schema::table_name() from shared module
This commit is contained in:
Accusys
2026-07-10 00:08:12 +08:00
parent 5a941f857c
commit 13abb15595
3 changed files with 103 additions and 70 deletions
+10 -25
View File
@@ -1,30 +1,15 @@
use crate::core::config::DATABASE_SCHEMA;
use once_cell::sync::Lazy;
/// Schema-qualified table name helper
/// Returns `name` for public schema, `schema.name` for others
pub fn t(name: &str) -> String {
table_name(name)
}
pub static SCHEMA_PREFIX: Lazy<String> = Lazy::new(|| {
let schema = DATABASE_SCHEMA.as_str();
/// Alias for t() - used by postgres_db.rs
pub fn table_name(name: &str) -> String {
let schema = std::env::var("DATABASE_SCHEMA").unwrap_or_else(|_| "dev".to_string());
if schema == "public" {
String::new()
name.to_string()
} else {
format!("{}.", schema)
}
});
pub fn table_name(table: &str) -> String {
let prefix = SCHEMA_PREFIX.as_str();
if prefix.is_empty() {
table.to_string()
} else {
format!("{}{}", prefix, table)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_table_name_public() {
assert_eq!(table_name("videos"), "videos");
format!("{}.{}", schema, name)
}
}
+1 -8
View File
@@ -6,6 +6,7 @@ use std::path::Path;
use crate::core::db::postgres_db::PostgresDb;
use crate::core::db::redis_client::RedisClient;
use crate::core::db::schema::t;
use crate::core::progress::{publish_tkg_progress, TkgPhase, TkgProgress, TkgStats};
use std::sync::Arc;
@@ -121,14 +122,6 @@ async fn build_node_id_map(pool: &PgPool, file_uuid: &str) -> HashMap<(String, S
.collect()
}
fn t(name: &str) -> String {
let schema = std::env::var("DATABASE_SCHEMA").unwrap_or_else(|_| "dev".to_string());
if schema == "public" {
name.to_string()
} else {
format!("{}.{}", schema, name)
}
}
// ── Phase 0: Populate trace_id from face.json ───────────────────────────────────────────────────────