Skip to content

Commit

Permalink
Workaround for identity column usage in version
Browse files Browse the repository at this point in the history
  • Loading branch information
slb-halfspace committed Dec 3, 2024
1 parent af6509c commit 62d120e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TableColumnNames:
calculation_version = "calculation_version"
is_internal_calculation = "is_internal_calculation"
"""True if the calculation is an internal calculation, False otherwise."""
calculation_version_dh3 = "calculation_version_dh3"
calculation_version_dh2 = "calculation_version_dh2"
created_by_user_id = "created_by_user_id"
energy_supplier_id = "energy_supplier_id"
from_date = "from_date"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ def write_calculation(
try:
spark.sql(
f"INSERT INTO {infrastructure_settings.catalog_name}.{WholesaleInternalDatabase.DATABASE_NAME}.{WholesaleInternalDatabase.CALCULATIONS_TABLE_NAME}"
f" ({TableColumnNames.calculation_id}, {TableColumnNames.calculation_type}, {TableColumnNames.calculation_period_start}, {TableColumnNames.calculation_period_end}, {TableColumnNames.calculation_execution_time_start}, {TableColumnNames.calculation_succeeded_time}, {TableColumnNames.is_internal_calculation})"
f" VALUES ('{args.calculation_id}', '{args.calculation_type.value}', '{calculation_period_start_datetime}', '{calculation_period_end_datetime}', '{calculation_execution_time_start}', NULL, '{args.is_internal_calculation}');"
f" ({TableColumnNames.calculation_id}, {TableColumnNames.calculation_type}, {TableColumnNames.calculation_period_start}, {TableColumnNames.calculation_period_end}, {TableColumnNames.calculation_execution_time_start}, {TableColumnNames.calculation_succeeded_time}, {TableColumnNames.is_internal_calculation}, {TableColumnNames.calculation_version_dh2}, {TableColumnNames.calculation_version})"
f" VALUES ('{args.calculation_id}', '{args.calculation_type.value}', '{calculation_period_start_datetime}', '{calculation_period_end_datetime}', '{calculation_execution_time_start}', NULL, '{args.is_internal_calculation}', NULL, NULL);"
)
spark.sql(
f"UPDATE {infrastructure_settings.catalog_name}.{WholesaleInternalDatabase.DATABASE_NAME}.{WholesaleInternalDatabase.CALCULATIONS_TABLE_NAME}"
f"SET {TableColumnNames.calculation_version} = CASE WHEN {TableColumnNames.calculation_version_dh2} IS NOT NONE THEN 0 ELSE {TableColumnNames.calculation_version_dh3} END"
)
break
except MetadataChangedException as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ RENAME COLUMN calculation_version TO calculation_version_dh3
GO

ALTER TABLE {CATALOG_NAME}.{WHOLESALE_INTERNAL_DATABASE_NAME}.calculations
ADD COLUMNS (calculation_version_dh2 BIGINT)
GO

ALTER TABLE {CATALOG_NAME}.{WHOLESALE_INTERNAL_DATABASE_NAME}.calculations
ADD COLUMN calculation_version BIGINT GENERATED ALWAYS AS (CASE WHEN calculation_version_dh2 IS NOT NULL THEN 0 ELSE calculation_version_dh3 END)
ADD COLUMNS (calculation_version_dh2 BIGINT, calculation_version BIGINT)

0 comments on commit 62d120e

Please sign in to comment.