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 operation_class_config #14

Merged
merged 1 commit into from
Jul 25, 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
3 changes: 3 additions & 0 deletions src/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ class Config:
PK_SET_MAX_SIZE = 1000000
EVENT_BATCH_DURATION_IN_SECONDS = 3600

# Operation Class specific config
OPERATION_CLASS_CONFIG = {}

@property
def operation_class(self):
if self._operation_class is not None:
Expand Down
10 changes: 10 additions & 0 deletions src/sbosc/operations/operation.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
from abc import abstractmethod
from contextlib import contextmanager
from dataclasses import dataclass
from typing import List

from MySQLdb.cursors import Cursor

from config import config
from modules.db import Database
from modules.redis import RedisData


@dataclass
class MigrationOperationConfig:
pass


class MigrationOperation:
"""Abstract class for migration operations."""
operation_config_class = MigrationOperationConfig

def __init__(self, migration_id):
self.migration_id = migration_id
Expand All @@ -24,6 +32,8 @@ def __init__(self, migration_id):
self.source_column_list: list = metadata.source_columns.split(',')
self.start_datetime = metadata.start_datetime

self.operation_config = self.operation_config_class(**config.OPERATION_CLASS_CONFIG)

@abstractmethod
def insert_batch(self, db: Database, start_pk: int, end_pk: int, upsert=False, limit=None) -> Cursor:
"""
Expand Down
26 changes: 16 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
get_logger()


@pytest.fixture(scope='session')
@pytest.fixture(scope='package')
def secret():
from config import secret
return secret


@pytest.fixture(scope='session')
@pytest.fixture(scope='package')
def config():
from config import config
return config
Expand All @@ -40,7 +40,7 @@ def request_id(request):
return request.node.callspec.id


@pytest.fixture(scope='session')
@pytest.fixture(scope='package')
def cursor(config, secret):
connection = MySQLdb.connect(
host=config.SOURCE_WRITER_ENDPOINT,
Expand All @@ -55,7 +55,8 @@ def cursor(config, secret):

@pytest.fixture
def sqlalchemy_engine(config, secret):
return create_engine(f'mysql+mysqldb://{secret.USERNAME}:@{config.SOURCE_WRITER_ENDPOINT}:{secret.PORT}/{config.SOURCE_DB}')
return create_engine(
f'mysql+mysqldb://{secret.USERNAME}:@{config.SOURCE_WRITER_ENDPOINT}:{secret.PORT}/{config.SOURCE_DB}')


@pytest.fixture(autouse=True)
Expand All @@ -69,13 +70,18 @@ def time_sleep_mock():


@pytest.fixture(scope='session')
def redis_data():
def migration_id():
return 1


@pytest.fixture(scope='session')
def redis_data(migration_id):
from modules.redis import RedisData
return RedisData(1, False)
return RedisData(migration_id, False)


@pytest.fixture(autouse=True, scope='session')
def init_migration(config, cursor, redis_data):
@pytest.fixture(autouse=True, scope='package')
def init_migration(config, cursor, redis_data, migration_id):
from sbosc.const import Stage
from sbosc.controller.initializer import Initializer

Expand All @@ -86,10 +92,10 @@ def init_migration(config, cursor, redis_data):
cursor.execute(f'CREATE TABLE {config.SOURCE_DB}.{config.SOURCE_TABLE} (id int)')
cursor.execute(f'CREATE TABLE {config.DESTINATION_DB}.{config.DESTINATION_TABLE} (id int)')

migration_id = Initializer().init_migration()
retrieved_migration_id = Initializer().init_migration()

# Validate Initializer.init_migration
assert migration_id == 1
assert retrieved_migration_id == migration_id
assert redis_data.current_stage == Stage.START_EVENT_HANDLER
assert redis_data.metadata.source_db == config.SOURCE_DB
assert redis_data.metadata.source_table == config.SOURCE_TABLE
Expand Down
Loading