Skip to content

Commit

Permalink
Bypass error for Azure Synapse triggers #127
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Willey committed Mar 26, 2022
1 parent 609feca commit 54c7b74
Showing 1 changed file with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using DatabaseSchemaReader.DataSchema;
using DatabaseSchemaReader.ProviderSchemaReaders.ConnectionContext;

Expand Down Expand Up @@ -35,6 +37,28 @@ INNER JOIN sys.tables AS parent

}

private void UseAlternativeSql()
{
Sql = @"SELECT
tr.name AS TRIGGER_NAME,
SCHEMA_NAME(parent.schema_id) AS TRIGGER_SCHEMA,
SCHEMA_NAME(parent.schema_id) AS TABLE_SCHEMA,
parent.name AS TABLE_NAME,
OBJECTPROPERTY(tr.object_id, 'ExecIsUpdateTrigger') AS IS_UPDATE,
OBJECTPROPERTY(tr.object_id, 'ExecIsDeleteTrigger') AS IS_DELETE,
OBJECTPROPERTY(tr.object_id, 'ExecIsInsertTrigger') AS IS_INSERT,
OBJECTPROPERTY(tr.object_id, 'ExecIsAfterTrigger') AS IS_AFTER,
tr.is_instead_of_trigger AS IS_INSTEADOF,
tr.is_disabled AS IS_DISABLED,
NULL as TRIGGER_BODY
FROM sys.triggers AS tr
INNER JOIN sys.tables AS parent
ON tr.parent_id = parent.object_id
WHERE (SCHEMA_NAME(parent.schema_id) = @Owner or (@Owner is null))
AND (parent.name = @TABLE_NAME or (@TABLE_NAME is null))
";
}

protected override void AddParameters(DbCommand command)
{
AddDbParameter(command, "Owner", Owner);
Expand Down Expand Up @@ -77,7 +101,17 @@ protected override void Mapper(IDataRecord record)

public IList<DatabaseTrigger> Execute(IConnectionAdapter connectionAdapter)
{
ExecuteDbReader(connectionAdapter);
try
{
ExecuteDbReader(connectionAdapter);
}
catch (DbException dbException)
{
Trace.WriteLine(dbException);
Trace.TraceWarning("Retrying triggers without OBJECT_DEFINITION (Azure Synapse)");
UseAlternativeSql();
ExecuteDbReader(connectionAdapter);
}
return Result;
}
}
Expand Down

0 comments on commit 54c7b74

Please sign in to comment.