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:
+10
-25
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ───────────────────────────────────────────────────────
|
||||
|
||||
|
||||
Reference in New Issue
Block a user