Skip to content

Commit c86f331

Browse files
committed
Fix up record API config after renaming table. #93
1 parent 2b66bbc commit c86f331

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

trailbase-core/src/admin/table/alter_table.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use ts_rs::TS;
1313

1414
use crate::admin::AdminError as Error;
1515
use crate::app_state::AppState;
16+
use crate::config::proto::hash_config;
1617
use crate::transaction::{TransactionLog, TransactionRecorder};
1718

1819
#[derive(Clone, Debug, Deserialize, TS)]
@@ -51,14 +52,14 @@ pub async fn alter_table_handler(
5152

5253
debug!("Alter table:\nsource: {source_schema:?}\ntarget: {target_schema:?}",);
5354

54-
let temp_table_name: QualifiedName = {
55-
if target_table_name != source_table_name {
56-
target_table_name.clone()
57-
} else {
58-
QualifiedName {
59-
name: format!("__alter_table_{}", target_table_name.name),
60-
database_schema: target_table_name.database_schema.clone(),
61-
}
55+
let is_table_rename = target_table_name != source_table_name;
56+
let temp_table_name: QualifiedName = if is_table_rename {
57+
// No ephemeral table needed.
58+
target_table_name.clone()
59+
} else {
60+
QualifiedName {
61+
name: format!("__alter_table_{}", target_table_name.name),
62+
database_schema: target_table_name.database_schema.clone(),
6263
}
6364
};
6465

@@ -141,7 +142,7 @@ pub async fn alter_table_handler(
141142
)
142143
.await?;
143144

144-
// Write to migration file.
145+
// Write migration file and apply it right away.
145146
if let Some(log) = log {
146147
let migration_path = state.data_dir().migrations_path();
147148
let report = log
@@ -152,6 +153,32 @@ pub async fn alter_table_handler(
152153

153154
state.schema_metadata().invalidate_all().await?;
154155

156+
// Fix configuration: update all table references by existing APIs.
157+
//
158+
// TODO: Update and or validate for column changes. Specifically: expand, excluded_columns and
159+
// all the rules.
160+
if is_table_rename
161+
&& matches!(
162+
source_schema.name.database_schema.as_deref(),
163+
Some("main") | None
164+
)
165+
{
166+
let mut config = state.get_config();
167+
let old_config_hash = hash_config(&config);
168+
169+
for api in &mut config.record_apis {
170+
if let Some(ref name) = api.table_name {
171+
if *name == source_schema.name.name {
172+
api.table_name = Some(target_schema.name.name.clone());
173+
}
174+
}
175+
}
176+
177+
state
178+
.validate_and_update_config(config, Some(old_config_hash))
179+
.await?;
180+
}
181+
155182
return Ok((StatusCode::OK, "altered table").into_response());
156183
}
157184

trailbase-core/src/admin/table/drop_table.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,17 @@ pub async fn drop_table_handler(
6161
})
6262
.await?;
6363

64-
// Update configuration: remove all APIs reference the no longer existing table.
64+
// Write migration file and apply it right away.
65+
if let Some(log) = log {
66+
let migration_path = state.data_dir().migrations_path();
67+
let _report = log
68+
.apply_as_migration(state.conn(), migration_path, &filename)
69+
.await?;
70+
}
71+
72+
state.schema_metadata().invalidate_all().await?;
73+
74+
// Fix configuration: remove all APIs reference the no longer existing table.
6575
let mut config = state.get_config();
6676
let old_config_hash = hash_config(&config);
6777

@@ -75,15 +85,5 @@ pub async fn drop_table_handler(
7585
.validate_and_update_config(config, Some(old_config_hash))
7686
.await?;
7787

78-
// Write migration file and apply it right away.
79-
if let Some(log) = log {
80-
let migration_path = state.data_dir().migrations_path();
81-
let _report = log
82-
.apply_as_migration(state.conn(), migration_path, &filename)
83-
.await?;
84-
}
85-
86-
state.schema_metadata().invalidate_all().await?;
87-
8888
return Ok((StatusCode::OK, "").into_response());
8989
}

0 commit comments

Comments
 (0)