@@ -13,6 +13,7 @@ use ts_rs::TS;
13
13
14
14
use crate :: admin:: AdminError as Error ;
15
15
use crate :: app_state:: AppState ;
16
+ use crate :: config:: proto:: hash_config;
16
17
use crate :: transaction:: { TransactionLog , TransactionRecorder } ;
17
18
18
19
#[ derive( Clone , Debug , Deserialize , TS ) ]
@@ -51,14 +52,14 @@ pub async fn alter_table_handler(
51
52
52
53
debug ! ( "Alter table:\n source: {source_schema:?}\n target: {target_schema:?}" , ) ;
53
54
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 ( ) ,
62
63
}
63
64
} ;
64
65
@@ -141,7 +142,7 @@ pub async fn alter_table_handler(
141
142
)
142
143
. await ?;
143
144
144
- // Write to migration file.
145
+ // Write migration file and apply it right away .
145
146
if let Some ( log) = log {
146
147
let migration_path = state. data_dir ( ) . migrations_path ( ) ;
147
148
let report = log
@@ -152,6 +153,32 @@ pub async fn alter_table_handler(
152
153
153
154
state. schema_metadata ( ) . invalidate_all ( ) . await ?;
154
155
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
+
155
182
return Ok ( ( StatusCode :: OK , "altered table" ) . into_response ( ) ) ;
156
183
}
157
184
0 commit comments