Skip to content

Commit

Permalink
added type transfor for jsonb (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ali-e authored Oct 4, 2024
1 parent a0d798a commit 6ba8140
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions backend/migrating/v2/management/commands/migrate_to_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,43 @@ def _prepare_row_and_migrate_relations(
row = [json.dumps(value) if isinstance(value, dict) else value for value in row]
return tuple(row)

def _convert_lists_to_json(
self,
row: tuple[Any, ...],
key: str,
column_names: list[str],
) -> tuple[Any, ...]:
"""Convert specified field in the row to JSON format if it is a
list."""
if key not in column_names:
return row

index = column_names.index(key) # Index of the key
row_as_list = list(row)
if isinstance(row[index], list):
row_as_list[index] = json.dumps(row_as_list[index])
return tuple(row_as_list)
row = [
json.dumps(value) if isinstance(value, dict) else value
for value in row_as_list
]
return row

def _process_type_transfer(
self,
row: tuple[Any, ...],
type_transformations: dict[str, dict[str, str]],
column_names: list[str],
) -> tuple[Any, ...]:
"""Process type transformations for specified fields in the row."""
for key, transaction in type_transformations.items():
data_type = transaction.get("type")

if data_type == "list":
row = self._convert_lists_to_json(row, key, column_names)

return row

def _migrate_rows(
self,
migration_name: str,
Expand All @@ -211,6 +248,7 @@ def _migrate_rows(
dest_query: str,
column_names: list[str],
column_transformations: dict[str, dict[str, Any]],
type_transformations: dict[str, Any],
) -> None:
"""Migrates rows in batches from the source to the destination
database.
Expand Down Expand Up @@ -254,6 +292,11 @@ def _migrate_rows(
if converted_row is None:
logger.info(f"[{migration_name}] Skipping deleted row {row}.")
continue
# Process type transformations for the converted row
converted_row = self._process_type_transfer(
converted_row, type_transformations, column_names
)

converted_rows.append(converted_row)

start_time = time.time()
Expand Down Expand Up @@ -370,6 +413,7 @@ def migrate(
dest_table = migration.get("dest_table")
clear_table = migration.get("clear_table", False)
column_transformations = migration.get("new_key_transaction", {})
type_transformations = migration.get("type_transformations", {})

# Clear the destination table if specified
if clear_table and dest_table:
Expand Down Expand Up @@ -398,6 +442,7 @@ def migrate(
dest_query,
column_names,
column_transformations,
type_transformations,
)

# Adjust the auto-increment value
Expand Down

0 comments on commit 6ba8140

Please sign in to comment.