Skip to content

Commit b29286a

Browse files
committed
Simplify
1 parent 6dc6e9c commit b29286a

File tree

1 file changed

+33
-58
lines changed

1 file changed

+33
-58
lines changed

mysql_ch_replicator/converter.py

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -748,18 +748,6 @@ def parse_create_table_query(self, mysql_query) -> tuple[TableStructure, TableSt
748748
new_table_name = match.group(1).strip('`"')
749749
source_table_name = match.group(2).strip('`"')
750750

751-
# Create a basic fallback structure in case we can't get the source
752-
basic_structure = TableStructure()
753-
basic_structure.table_name = new_table_name
754-
basic_structure.primary_keys = ['id']
755-
basic_structure.fields.append(TableField(
756-
name='id',
757-
field_type='int',
758-
parameters='NOT NULL AUTO_INCREMENT',
759-
additional_data=None,
760-
))
761-
basic_structure.preprocess()
762-
763751
# Try to get the actual structure from the existing table structures first
764752
if (hasattr(self, 'db_replicator') and
765753
self.db_replicator is not None and
@@ -782,13 +770,12 @@ def parse_create_table_query(self, mysql_query) -> tuple[TableStructure, TableSt
782770
return new_mysql_structure, new_ch_structure
783771

784772
# If we couldn't get it from state, try with MySQL API
785-
try:
786-
# If we have a db_replicator with a valid mysql_api
787-
if (hasattr(self, 'db_replicator') and
788-
self.db_replicator is not None and
789-
hasattr(self.db_replicator, 'mysql_api') and
790-
self.db_replicator.mysql_api is not None):
791-
773+
if (hasattr(self, 'db_replicator') and
774+
self.db_replicator is not None and
775+
hasattr(self.db_replicator, 'mysql_api') and
776+
self.db_replicator.mysql_api is not None):
777+
778+
try:
792779
# Get the CREATE statement for the source table
793780
source_create_statement = self.db_replicator.mysql_api.get_table_create_statement(source_table_name)
794781

@@ -803,14 +790,15 @@ def parse_create_table_query(self, mysql_query) -> tuple[TableStructure, TableSt
803790
ch_table_structure = self.convert_table_structure(mysql_table_structure)
804791

805792
return mysql_table_structure, ch_table_structure
806-
except Exception as e:
807-
print(f"Warning: Could not get source table structure for LIKE statement: {str(e)}")
793+
except Exception as e:
794+
error_msg = f"Could not get source table structure for LIKE statement: {str(e)}"
795+
print(f"Error: {error_msg}")
796+
raise Exception(error_msg, mysql_query)
808797

809-
# If we got here, use the fallback structure
810-
ch_basic_structure = self.convert_table_structure(basic_structure)
811-
return basic_structure, ch_basic_structure
798+
# If we got here, we couldn't determine the structure
799+
raise Exception(f"Could not determine structure for source table '{source_table_name}' in LIKE statement", mysql_query)
812800

813-
# Regular parsing for non-LIKE statements or if LIKE handling failed
801+
# Regular parsing for non-LIKE statements
814802
mysql_table_structure = self.parse_mysql_table_structure(mysql_query)
815803
ch_table_structure = self.convert_table_structure(mysql_table_structure)
816804
return mysql_table_structure, ch_table_structure
@@ -861,44 +849,31 @@ def parse_mysql_table_structure(self, create_statement, required_table_name=None
861849
raise Exception('wrong create statement', create_statement)
862850

863851
source_table_name = strip_sql_name(tokens[4].get_real_name())
852+
target_table_name = strip_sql_name(tokens[2].get_real_name())
864853

865-
# Create a basic structure with the target table name
866-
structure.table_name = strip_sql_name(tokens[2].get_real_name())
867-
868-
# During initial parsing in the test, we'll create a skeleton structure
869-
# Later during replication, the actual structure will be used
870-
structure.primary_keys = ['id'] # Assuming 'id' is the primary key
871-
structure.fields.append(TableField(
872-
name='id',
873-
field_type='int',
874-
parameters='NOT NULL AUTO_INCREMENT',
875-
additional_data=None,
876-
))
877-
structure.preprocess()
878-
879-
# Try to get the actual structure if possible
880-
try:
881-
# If we have a db_replicator with a valid mysql_api
882-
if (hasattr(self, 'db_replicator') and
854+
# Try to get the actual structure - we need a valid MySQL API
855+
if not (hasattr(self, 'db_replicator') and
883856
self.db_replicator is not None and
884857
hasattr(self.db_replicator, 'mysql_api') and
885858
self.db_replicator.mysql_api is not None):
886-
887-
# Try to get the CREATE statement from the current database
888-
mysql_api = self.db_replicator.mysql_api
889-
source_create_statement = mysql_api.get_table_create_statement(source_table_name)
890-
891-
# Parse the source table structure
892-
source_structure = self.parse_mysql_table_structure(source_create_statement)
893-
894-
# Copy the structure but keep the new table name
895-
structure = copy.deepcopy(source_structure)
896-
structure.table_name = strip_sql_name(tokens[2].get_real_name())
859+
raise Exception(f"Cannot parse LIKE statement - no MySQL API available to get source table structure", create_statement)
860+
861+
try:
862+
# Try to get the CREATE statement from the current database
863+
mysql_api = self.db_replicator.mysql_api
864+
source_create_statement = mysql_api.get_table_create_statement(source_table_name)
865+
866+
# Parse the source table structure
867+
source_structure = self.parse_mysql_table_structure(source_create_statement)
868+
869+
# Copy the structure but keep the new table name
870+
structure = copy.deepcopy(source_structure)
871+
structure.table_name = target_table_name
872+
return structure
897873
except Exception as e:
898-
# Log the error but still return the basic structure
899-
print(f"Warning: Could not get source table structure: {str(e)}")
900-
901-
return structure
874+
error_msg = f"Could not get source table structure: {str(e)}"
875+
print(f"Error: {error_msg}")
876+
raise Exception(error_msg, create_statement)
902877

903878
if not isinstance(tokens[3], sqlparse.sql.Parenthesis):
904879
raise Exception('wrong create statement', create_statement)

0 commit comments

Comments
 (0)