@@ -597,6 +597,13 @@ def convert_alter_query(self, mysql_query, db_name):
597
597
if op_name == 'change' :
598
598
self .__convert_alter_table_change_column (db_name , table_name , tokens )
599
599
continue
600
+
601
+ if op_name == 'rename' :
602
+ # Handle RENAME COLUMN operation
603
+ if tokens [0 ].lower () == 'column' :
604
+ tokens = tokens [1 :] # Skip the COLUMN keyword
605
+ self .__convert_alter_table_rename_column (db_name , table_name , tokens )
606
+ continue
600
607
601
608
raise Exception (f'operation { op_name } not implement, query: { subquery } , full query: { mysql_query } ' )
602
609
@@ -808,6 +815,53 @@ def __convert_alter_table_change_column(self, db_name, table_name, tokens):
808
815
query = f'ALTER TABLE `{ db_name } `.`{ table_name } ` RENAME COLUMN { column_name } TO { new_column_name } '
809
816
self .db_replicator .clickhouse_api .execute_command (query )
810
817
818
+ def __convert_alter_table_rename_column (self , db_name , table_name , tokens ):
819
+ """
820
+ Handle the RENAME COLUMN syntax of ALTER TABLE statements.
821
+ Example: RENAME COLUMN old_name TO new_name
822
+ """
823
+ if len (tokens ) < 3 :
824
+ raise Exception ('wrong tokens count for RENAME COLUMN' , tokens )
825
+
826
+ # Extract old and new column names
827
+ old_column_name = strip_sql_name (tokens [0 ])
828
+
829
+ # Check if the second token is "TO" (standard syntax)
830
+ if tokens [1 ].lower () != 'to' :
831
+ raise Exception ('expected TO keyword in RENAME COLUMN syntax' , tokens )
832
+
833
+ new_column_name = strip_sql_name (tokens [2 ])
834
+
835
+ # Update table structure
836
+ if self .db_replicator :
837
+ if table_name in self .db_replicator .state .tables_structure :
838
+ table_structure = self .db_replicator .state .tables_structure [table_name ]
839
+ mysql_table_structure : TableStructure = table_structure [0 ]
840
+ ch_table_structure : TableStructure = table_structure [1 ]
841
+
842
+ # Update field name in MySQL structure
843
+ mysql_field = mysql_table_structure .get_field (old_column_name )
844
+ if mysql_field :
845
+ mysql_field .name = new_column_name
846
+ else :
847
+ raise Exception (f'Column { old_column_name } not found in MySQL structure' )
848
+
849
+ # Update field name in ClickHouse structure
850
+ ch_field = ch_table_structure .get_field (old_column_name )
851
+ if ch_field :
852
+ ch_field .name = new_column_name
853
+ else :
854
+ raise Exception (f'Column { old_column_name } not found in ClickHouse structure' )
855
+
856
+ # Preprocess to update primary key IDs if the renamed column is part of the primary key
857
+ mysql_table_structure .preprocess ()
858
+ ch_table_structure .preprocess ()
859
+
860
+ # Execute the RENAME COLUMN command in ClickHouse
861
+ query = f'ALTER TABLE `{ db_name } `.`{ table_name } ` RENAME COLUMN `{ old_column_name } ` TO `{ new_column_name } `'
862
+ if self .db_replicator :
863
+ self .db_replicator .clickhouse_api .execute_command (query )
864
+
811
865
def _handle_create_table_like (self , create_statement , source_table_name , target_table_name , is_query_api = True ):
812
866
"""
813
867
Helper method to handle CREATE TABLE LIKE statements.
0 commit comments