From 6e5b192fb6745bda2d1d97caf2eb1ef364bc9098 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Thu, 18 Jan 2024 15:54:25 +0100 Subject: [PATCH] graphql: create history table before running --- Jobs/GraphQL/GraphQLJob.vb | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/Jobs/GraphQL/GraphQLJob.vb b/Jobs/GraphQL/GraphQLJob.vb index cad98f37..60fd33c4 100644 --- a/Jobs/GraphQL/GraphQLJob.vb +++ b/Jobs/GraphQL/GraphQLJob.vb @@ -120,7 +120,11 @@ Public Class GraphQLJob ' Clear Table before inserting If pQuery.ClearBeforeFill = True Then If DeleteWithQueryName(pQuery) = False Then - Throw New ApplicationException($"Error while clearing table before fill for Query [{pQuery.Name}]") + Throw New ApplicationException($"Error while dropping history table before fill for Query [{pQuery.Name}]") + End If + + If CreateHistoryTable(pQuery) = False Then + Throw New ApplicationException($"Error while creating history table before fill for Query [{pQuery.Name}]") End If End If @@ -196,12 +200,28 @@ Public Class GraphQLJob End Try End Function - Private Function DeleteWithQueryName(pQuery) - Dim oDeleteSQL = $"TRUNCATE TABLE {pQuery.DestinationTable}" - Return _MSSQL.ExecuteNonQuery(oDeleteSQL) + Private Function DeleteWithQueryName(pQuery As GraphQL.Query) As Boolean + Dim oHistoryTableName = $"{pQuery.DestinationTable}_HISTORY" + Dim oDeleteHistorySQL = $" + IF (EXISTS (SELECT * + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = 'dbo' + AND TABLE_NAME = '{oHistoryTableName}')) + BEGIN + DROP TABLE {oHistoryTableName}; + END" + + Return _MSSQL.ExecuteNonQuery(oDeleteHistorySQL) + End Function + + Private Function CreateHistoryTable(pQuery As Query) As Boolean + Dim oHistoryTableName = $"{pQuery.DestinationTable}_HISTORY" + Dim oFillHistorySQL = $"SELECT * INTO {oHistoryTableName} FROM {pQuery.DestinationTable}" + + Return _MSSQL.ExecuteNonQuery(oFillHistorySQL) End Function - Private Function DeleteWithStatus(pQuery As Query, pStatus As Integer) + Private Function DeleteWithStatus(pQuery As Query, pStatus As Integer) As Boolean Dim oDeleteSQL = $"DELETE FROM {pQuery.DestinationTable} WHERE STATUS = {pStatus} AND ADDED_QUERY_ID = '{pQuery.Id}'" Return _MSSQL.ExecuteNonQuery(oDeleteSQL) End Function