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

FIX: Load _history tables from temp table #29

Merged
merged 2 commits into from
Nov 8, 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
15 changes: 8 additions & 7 deletions src/cubic_loader/qlik/ods_qlik.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_cdc_gz_csvs(etl_status: TableStatus, table: str) -> List[str]:
cdc_csvs = s3_list_cdc_gz_objects(S3_ARCHIVE, snapshot_prefix, min_ts=etl_status.last_cdc_ts)

# filter error files from table folder
for csv_file in s3_list_cdc_gz_objects(S3_ERROR, table, min_ts=etl_status.last_cdc_ts):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the fix for the S3 Permission error we were seeing as well

for csv_file in s3_list_cdc_gz_objects(S3_ERROR, table_prefix, min_ts=etl_status.last_cdc_ts):
if re_get_first(csv_file, RE_SNAPSHOT_TS) > etl_status.current_snapshot_ts:
cdc_csvs.append(csv_file)

Expand Down Expand Up @@ -332,7 +332,7 @@ def cdc_insert(self, cdc_df: pl.DataFrame) -> None:
if insert_df.shape[0] == 0:
return

insert_q = bulk_insert_from_temp(self.db_fact_table, insert_df.columns)
insert_q = bulk_insert_from_temp(self.db_fact_table, tmp_table, insert_df.columns)
with tempfile.TemporaryDirectory() as tmp_dir:
insert_path = os.path.join(tmp_dir, "insert.csv")
insert_df.write_csv(insert_path, quote_style="necessary")
Expand All @@ -358,16 +358,17 @@ def cdc_load_folder(self, load_folder: str) -> None:
try:
dfm_object = os.listdir(load_folder)[0].replace(".csv.gz", ".dfm").replace("|", "/")
merge_csv = os.path.join(load_folder, MERGED_FNAME)
key_columns = [col["name"].lower() for col in self.etl_status.last_schema if col["primaryKeyPos"] > 0]
load_table = f"{self.db_fact_table}_load"

cdc_ts = merge_cdc_csv_gz_files(load_folder)
self.cdc_verify_schema(dfm_object)

# Load records into _history table
remote_csv_gz_copy(merge_csv, self.db_history_table)

cdc_df = dataframe_from_merged_csv(merge_csv, dfm_object)

key_columns = [col["name"].lower() for col in self.etl_status.last_schema if col["primaryKeyPos"] > 0]
# Load records into _history table
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update comment here.
q: is _load a new type of table at this point? we need to communicate to folks who are using the database what it is, so they can ignore if need be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is just a table to load stuff, maybe truncating at the end as well? I want it to be obvious that it shouldn't be used by data analysts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sort of a "temp" table. It gets created by the dmap_import application when it starts to load a QLIK table and then gets dropped at the end. So DB users would only ever see it while the ETL operations is actively running.

self.db.truncate_table(load_table)
remote_csv_gz_copy(merge_csv, load_table)
self.db.execute(bulk_insert_from_temp(self.db_history_table, load_table, cdc_df.columns))

self.cdc_insert(cdc_df)

Expand Down
7 changes: 3 additions & 4 deletions src/cubic_loader/qlik/rds_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,14 @@ def bulk_update_from_temp(schema_and_table: str, update_column: str, key_columns
return update_query


def bulk_insert_from_temp(schema_and_table: str, columns: List[str]) -> str:
def bulk_insert_from_temp(insert_table_and_schema: str, temp_table_and_schema: str, columns: List[str]) -> str:
"""
create query to INSERT records from temp table to fact table
"""
tmp_table = f"{schema_and_table}_load"
columns_str = ",".join(columns)
insert_query = (
f"INSERT INTO {schema_and_table} ({columns_str}) "
f"SELECT {columns_str} FROM {tmp_table} "
f"INSERT INTO {insert_table_and_schema} ({columns_str}) "
f"SELECT {columns_str} FROM {temp_table_and_schema} "
f"ON CONFLICT DO NOTHING;"
)
return insert_query
Loading