@@ -216,16 +216,8 @@ impl MutTxId {
216
216
217
217
// Generate the full definition of the table, with the generated indexes, constraints, sequences...
218
218
219
- // Insert the columns into `st_columns`
220
- for col in table_schema. columns ( ) {
221
- let row = StColumnRow {
222
- table_id : col. table_id ,
223
- col_pos : col. col_pos ,
224
- col_name : col. col_name . clone ( ) ,
225
- col_type : col. col_type . clone ( ) . into ( ) ,
226
- } ;
227
- self . insert_via_serialize_bsatn ( ST_COLUMN_ID , & row) ?;
228
- }
219
+ // Insert the columns into `st_column`.
220
+ self . insert_st_column ( table_schema. columns ( ) ) ?;
229
221
230
222
let schedule = table_schema. schedule . clone ( ) ;
231
223
let mut schema_internal = table_schema;
@@ -279,6 +271,15 @@ impl MutTxId {
279
271
Ok ( table_id)
280
272
}
281
273
274
+ /// Insert `columns` into `st_column`.
275
+ fn insert_st_column ( & mut self , columns : & [ ColumnSchema ] ) -> Result < ( ) > {
276
+ columns. iter ( ) . try_for_each ( |col| {
277
+ let row: StColumnRow = col. clone ( ) . into ( ) ;
278
+ self . insert_via_serialize_bsatn ( ST_COLUMN_ID , & row) ?;
279
+ Ok ( ( ) )
280
+ } )
281
+ }
282
+
282
283
fn create_table_internal ( & mut self , schema : Arc < TableSchema > ) {
283
284
// Construct the in memory tables.
284
285
let table_id = schema. table_id ;
@@ -319,6 +320,11 @@ impl MutTxId {
319
320
Ok ( RowTypeForTable :: Arc ( self . schema_for_table ( table_id) ?) )
320
321
}
321
322
323
+ /// Drops all the columns of `table_id` in `st_column`.
324
+ fn drop_st_column ( & mut self , table_id : TableId ) -> Result < ( ) > {
325
+ self . delete_col_eq ( ST_COLUMN_ID , StColumnFields :: TableId . col_id ( ) , & table_id. into ( ) )
326
+ }
327
+
322
328
pub fn drop_table ( & mut self , table_id : TableId ) -> Result < ( ) > {
323
329
let schema = & * self . schema_for_table ( table_id) ?;
324
330
@@ -336,7 +342,7 @@ impl MutTxId {
336
342
337
343
// Drop the table and their columns
338
344
self . delete_col_eq ( ST_TABLE_ID , StTableFields :: TableId . col_id ( ) , & table_id. into ( ) ) ?;
339
- self . delete_col_eq ( ST_COLUMN_ID , StColumnFields :: TableId . col_id ( ) , & table_id. into ( ) ) ?;
345
+ self . drop_st_column ( table_id) ?;
340
346
341
347
if let Some ( schedule) = & schema. schedule {
342
348
self . delete_col_eq (
@@ -464,11 +470,19 @@ impl MutTxId {
464
470
column_schemas. iter_mut ( ) . for_each ( |c| c. table_id = table_id) ;
465
471
466
472
// Try to change the tables into what we want.
467
- let old_column_schemas = tx_table. change_columns_to ( column_schemas) . map_err ( TableError :: from) ?;
473
+ let old_column_schemas = tx_table
474
+ . change_columns_to ( column_schemas. clone ( ) )
475
+ . map_err ( TableError :: from) ?;
468
476
// SAFETY: `commit_table` should have a schema identical to that of `tx_table`
469
477
// prior to changing it just now.
470
478
unsafe { commit_table. set_layout_and_schema_to ( tx_table) } ;
471
479
480
+ // Update system tables.
481
+ // We'll simply remove all rows in `st_columns` and then add the new ones.
482
+ // The datastore takes care of not persisting any no-op delete/inserts to the commitlog.
483
+ self . drop_st_column ( table_id) ?;
484
+ self . insert_st_column ( & column_schemas) ?;
485
+
472
486
// Remember the pending change so we can undo if necessary.
473
487
self . push_schema_change ( PendingSchemaChange :: TableAlterRowType ( table_id, old_column_schemas) ) ;
474
488
0 commit comments