Skip to content

Commit

Permalink
spi-stats:fix alter column.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljl committed Dec 10, 2024
1 parent df36be0 commit 7f24967
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 197 deletions.
46 changes: 29 additions & 17 deletions backend/basic/src/spi/spi_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,51 +436,63 @@ EXECUTE PROCEDURE TARDIS_AUTO_UPDATE_TIME_{}();"###,
Ok(())
}

/// Modify table column
/// 修改表字段
pub async fn modify_table_column(
/// alter table column
/// 更改表字段
pub async fn alter_table_column(
conn: &TardisRelDBlConnection,
tag: Option<&str>,
table_flag: &str,
modify_column_kind: &ModifyColumnKind,
alter_column_kind: &AlterColumnKind,
field_name: &str,
field_type: &str,
field_content: &str,
add_index: Vec<(&str, &str)>,
ctx: &TardisContext,
) -> TardisResult<()> {
let tag = tag.map(|t| format!("_{t}")).unwrap_or_default();
let schema_name = get_schema_name_from_context(ctx);
do_modify_table_column(&schema_name, conn, &tag, table_flag, modify_column_kind, field_name, field_type, add_index).await
do_alter_table_column(&schema_name, conn, &tag, table_flag, alter_column_kind, field_name, field_content, add_index).await
}

async fn do_modify_table_column(
async fn do_alter_table_column(
schema_name: &str,
conn: &TardisRelDBlConnection,
tag: &str,
table_flag: &str,
modify_column_kind: &ModifyColumnKind,
alter_column_kind: &AlterColumnKind,
field_name: &str,
field_type: &str,
field_content: &str,
add_index: Vec<(&str, &str)>,
) -> TardisResult<()> {
match modify_column_kind {
ModifyColumnKind::Add => {
match alter_column_kind {
AlterColumnKind::Add => {
conn.execute_one(
&format!("ALTER TABLE {schema_name}.{GLOBAL_STORAGE_FLAG}_{table_flag}{tag} ADD COLUMN {field_name} {field_type}"),
&format!("ALTER TABLE {schema_name}.{GLOBAL_STORAGE_FLAG}_{table_flag}{tag} ADD COLUMN {field_content}"),
vec![],
)
.await?;
// Add index
for (field_name_or_fun, index_type) in add_index.into_iter() {
let index_name = format!("idx_{schema_name}{tag}_{table_flag}_{field_name_or_fun}",);
for (idx, (field_name_or_fun, index_type)) in add_index.into_iter().enumerate() {
// index name shouldn't be longer than 63 characters
// [4 ][ 18 ][ 12 ][ 26 ][ 3 ]
// idx_{schema_name}{tag}_{table_flag}_{idx}
#[inline]
fn truncate_str(s: &str, max_size: usize) -> &str {
&s[..max_size.min(s.len())]
}
let index_name = format!(
"idx_{schema_name}{tag}_{table_flag}_{field_name_or_fun}_{idx}",
schema_name = truncate_str(schema_name, 18),
tag = truncate_str(tag, 11),
table_flag = truncate_str(table_flag, 25),
idx = truncate_str(idx.to_string().as_str(), 3),
);
conn.execute_one(
&format!("CREATE INDEX {index_name} ON {schema_name}.{GLOBAL_STORAGE_FLAG}_{table_flag}{tag} USING {index_type}({field_name_or_fun})"),
vec![],
)
.await?;
}
}
ModifyColumnKind::Delete => {
AlterColumnKind::Delete => {
conn.execute_one(
&format!("ALTER TABLE {schema_name}.{GLOBAL_STORAGE_FLAG}_{table_flag}{tag} DROP COLUMN {field_name}"),
vec![],
Expand All @@ -491,7 +503,7 @@ EXECUTE PROCEDURE TARDIS_AUTO_UPDATE_TIME_{}();"###,
Ok(())
}

pub enum ModifyColumnKind {
pub enum AlterColumnKind {
Add,
Delete,
}
Expand Down
2 changes: 1 addition & 1 deletion backend/spi/spi-stats/src/dto/stats_conf_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ pub struct StatsConfFactColInfoResp {
/// 关联外部系统传入的主键或编码
/// 用于扩展ext字段的事实列
pub rel_external_id: Option<String>,
pub dim_exclusive_rec: Option<String>,
pub dim_exclusive_rec: Option<bool>,
pub remark: Option<String>,
pub create_time: DateTime<Utc>,
pub update_time: DateTime<Utc>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) async fn add(add_req: &StatsConfDimAddReq, funs: &TardisFunsInst, ctx
Value::from(add_req.data_type.to_string()),
Value::from(add_req.hierarchy.as_ref().unwrap_or(&vec![]).clone()),
Value::from(add_req.remark.as_ref().unwrap_or(&"".to_string()).as_str()),
Value::from(add_req.dim_group_key.as_deref()),
Value::from(add_req.dim_group_key.as_ref().unwrap_or(&"".to_string()).as_str()),
Value::from(add_req.dynamic_url.as_deref()),
Value::from(add_req.is_tree.unwrap_or(false)),
Value::from(add_req.tree_dynamic_url.as_deref()),
Expand Down
Loading

0 comments on commit 7f24967

Please sign in to comment.