Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_max_pk to operation class #29

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/sbosc/operations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def apply_update(self, db, updated_pks):
cursor.execute(query)
return cursor

def get_max_pk(self, db, start_pk, end_pk):
metadata = self.redis_data.metadata
with db.cursor(host='dest') as cursor:
cursor: Cursor
cursor.execute(f'''
SELECT MAX({self.pk_column}) FROM {metadata.destination_db}.{metadata.destination_table}
WHERE {self.pk_column} BETWEEN {start_pk} AND {end_pk}
''')
return cursor.fetchone()[0]

def _get_not_imported_pks_query(self, start_pk, end_pk):
return f'''
SELECT source.{self.pk_column} FROM {self.source_db}.{self.source_table} AS source
Expand Down Expand Up @@ -181,6 +191,16 @@ def apply_update(self, db, updated_pks):
else:
return cursor

def get_max_pk(self, db, start_pk, end_pk):
metadata = self.redis_data.metadata
with db.cursor(host='dest') as cursor:
cursor: Cursor
cursor.execute(f'''
SELECT MAX({self.pk_column}) FROM {metadata.destination_db}.{metadata.destination_table}
WHERE {self.pk_column} BETWEEN {start_pk} AND {end_pk}
''')
return cursor.fetchone()[0]

def get_not_imported_pks(self, source_cursor, dest_cursor, start_pk, end_pk):
source_cursor.execute(f'''
SELECT {self.pk_column} FROM {self.source_db}.{self.source_table}
Expand Down
8 changes: 8 additions & 0 deletions src/sbosc/operations/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def apply_update(self, db: Database, updated_pks: list) -> Cursor:
"""
pass

@abstractmethod
def get_max_pk(self, db: Database, start_pk: int, end_pk: int) -> int:
"""
Returns the maximum primary key in the destination table.
Used when chunk status is DUPLICATE_KEY to determine starting batch range.
"""
pass

@abstractmethod
def get_not_imported_pks(self, source_cursor: Cursor, dest_cursor: Cursor, start_pk: int, end_pk: int) -> list:
"""
Expand Down
12 changes: 1 addition & 11 deletions src/sbosc/worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def get_start_pk(self, chunk_info: ChunkInfo):
elif chunk_info.status == ChunkStatus.IN_PROGRESS:
return chunk_info.last_pk_inserted + 1
elif chunk_info.status == ChunkStatus.DUPLICATE_KEY:
max_pk = self.get_max_pk(chunk_info.start_pk, chunk_info.end_pk)
max_pk = self.migration_operation.get_max_pk(self.db, chunk_info.start_pk, chunk_info.end_pk)
return max_pk + 1

def bulk_import(self):
Expand Down Expand Up @@ -206,16 +206,6 @@ def apply_dml_events(self):
except Exception as e:
self.logger.error(e)

def get_max_pk(self, start_pk, end_pk):
metadata = self.redis_data.metadata
with self.db.cursor(host='dest') as cursor:
cursor: Cursor
cursor.execute(f'''
SELECT MAX({metadata.pk_column}) FROM {metadata.destination_db}.{metadata.destination_table}
WHERE {metadata.pk_column} BETWEEN {start_pk} AND {end_pk}
''')
return cursor.fetchone()[0]

@staticmethod
def calculate_metrics(func: Callable[..., Cursor]):
def wrapper(self: Self, *args, **kwargs):
Expand Down