From 589311a28b5390da215ebf7db57af92f80dc2323 Mon Sep 17 00:00:00 2001 From: Jinghan Ying Date: Thu, 18 Nov 2021 14:50:19 +0800 Subject: [PATCH] refactor(database/offline): drop tables after complete row read --- internal/database/offline/postgres/join.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/database/offline/postgres/join.go b/internal/database/offline/postgres/join.go index c9dcfcd7b..7d02f214c 100644 --- a/internal/database/offline/postgres/join.go +++ b/internal/database/offline/postgres/join.go @@ -134,10 +134,15 @@ func (db *DB) readJoinedTable(ctx context.Context, entityRowsTableName string, t } data := make(chan []interface{}) - var scanErr error + var scanErr, dropErr error go func() { - defer rows.Close() - defer close(data) + defer func() { + if err := db.dropTemporaryTables(ctx, tableNames); err != nil { + dropErr = err + } + defer rows.Close() + defer close(data) + }() for rows.Next() { record, err := rows.SliceScan() if err != nil { @@ -146,10 +151,15 @@ func (db *DB) readJoinedTable(ctx context.Context, entityRowsTableName string, t data <- record } }() + + // TODO: return errors through channel + if scanErr != nil { + return nil, scanErr + } return &types.JoinResult{ Header: header, Data: data, - }, scanErr + }, dropErr } func (db *DB) dropTemporaryTables(ctx context.Context, tableNames []string) error {