Skip to content

- Fix the trigger "auto-drop" function (the trigger drop needs to be … #215

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected override IList<string> CreateSqlServerDatabaseObjects(IEnumerable<Tabl
return string.Format("IF EXISTS (SELECT * FROM sys.service_message_types WITH (NOLOCK) WHERE name = N'{0}') DROP MESSAGE TYPE [{0}];", pm);
}));

var dropAllScript = this.PrepareScriptDropAll(dropMessages);
var dropAllScript = this.PrepareScriptDropAll(dropMessages, false);
sqlCommand.CommandText = this.PrepareScriptProcedureQueueActivation(dropAllScript);
sqlCommand.ExecuteNonQuery();
this.WriteTraceMessage(TraceLevel.Verbose, $"Procedure {_dataBaseObjectsNamingConvention} created.");
Expand Down
16 changes: 14 additions & 2 deletions TableDependency.SqlClient/Resources/SqlScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ ELSE BEGIN
SELECT @schema_id = schema_id FROM sys.schemas WITH (NOLOCK) WHERE name = N'{2}';

PRINT N'SqlTableDependency: Dropping trigger [{2}].[tr_{0}_Sender].';
IF EXISTS (SELECT * FROM sys.triggers WITH (NOLOCK) WHERE object_id = OBJECT_ID(N'[{2}].[tr_{0}_Sender]')) DROP TRIGGER [{2}].[tr_{0}_Sender];
IF EXISTS (SELECT * FROM sys.triggers WITH (NOLOCK) WHERE object_id = OBJECT_ID(N'[{2}].[tr_{0}_Sender]'))
BEGIN
SELECT 1;
{3}
END

PRINT N'SqlTableDependency: Deactivating queue [{2}].[{0}_Sender].';
IF EXISTS (SELECT * FROM sys.service_queues WITH (NOLOCK) WHERE schema_id = @schema_id AND name = N'{0}_Sender') EXEC (N'ALTER QUEUE [{2}].[{0}_Sender] WITH ACTIVATION (STATUS = OFF)');
Expand Down Expand Up @@ -194,6 +198,14 @@ ELSE BEGIN
{1}

PRINT N'SqlTableDependency: Dropping activation procedure {0}_QueueActivationSender.';
IF EXISTS (SELECT * FROM sys.objects WITH (NOLOCK) WHERE schema_id = @schema_id AND name = N'{0}_QueueActivationSender') DROP PROCEDURE [{2}].[{0}_QueueActivationSender];";
IF EXISTS (SELECT * FROM sys.objects WITH (NOLOCK) WHERE schema_id = @schema_id AND name = N'{0}_QueueActivationSender') DROP PROCEDURE [{2}].[{0}_QueueActivationSender];

IF EXISTS (SELECT * FROM sys.triggers WITH (NOLOCK) WHERE object_id = OBJECT_ID(N'[{2}].[tr_{0}_Sender]'))
BEGIN
SELECT 1;
{4}
END";


}
}
33 changes: 18 additions & 15 deletions TableDependency.SqlClient/SqlTableDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,8 @@ public override void Start(int timeOut = 120, int watchDogTimeOut = 180)
_processableMessages = this.CreateDatabaseObjects(timeOut, watchDogTimeOut);
_cancellationTokenSource = new CancellationTokenSource();

_task = Task.Factory.StartNew(() =>
WaitForNotifications(
_cancellationTokenSource.Token,
onChangedSubscribedList,
onErrorSubscribedList,
onStatusChangedSubscribedList,
timeOut,
watchDogTimeOut),
_cancellationTokenSource.Token);
_task = WaitForNotifications(_cancellationTokenSource.Token, onChangedSubscribedList, onErrorSubscribedList,
onStatusChangedSubscribedList, timeOut, watchDogTimeOut);

this.WriteTraceMessage(TraceLevel.Info, $"Waiting for receiving {_tableName}'s records change notifications.");
}
Expand All @@ -192,7 +185,7 @@ public override void Stop()
if (_task != null)
{
_cancellationTokenSource.Cancel(true);
_task?.Wait();
_task.Wait();
}

_task = null;
Expand Down Expand Up @@ -382,7 +375,7 @@ protected override void DropDatabaseObjects()
return string.Format("IF EXISTS (SELECT * FROM sys.service_message_types WITH (NOLOCK) WHERE name = N'{0}') DROP MESSAGE TYPE [{0}];", pm);
}));

var dropAllScript = this.PrepareScriptDropAll(dropMessages);
var dropAllScript = this.PrepareScriptDropAll(dropMessages, false);

sqlCommand.Transaction = sqlTransaction;
sqlCommand.CommandType = CommandType.Text;
Expand Down Expand Up @@ -525,7 +518,8 @@ protected virtual IList<string> CreateSqlServerDatabaseObjects(IEnumerable<Table
return string.Format("IF EXISTS (SELECT * FROM sys.service_message_types WITH (NOLOCK) WHERE name = N'{0}') DROP MESSAGE TYPE [{0}];", pm);
}));

var dropAllScript = this.PrepareScriptDropAll(dropMessages);
var dropAllScript = this.PrepareScriptDropAll(dropMessages, false);
var dropAllScriptTrigger = this.PrepareScriptDropAll(dropMessages, true);
sqlCommand.CommandText = this.PrepareScriptProcedureQueueActivation(dropAllScript);
sqlCommand.ExecuteNonQuery();
this.WriteTraceMessage(TraceLevel.Verbose, $"Procedure {_dataBaseObjectsNamingConvention} created.");
Expand Down Expand Up @@ -563,7 +557,7 @@ protected virtual IList<string> CreateSqlServerDatabaseObjects(IEnumerable<Table
columnsForExceptTable,
columnsForDeletedTable,
this.ConversationHandle,
dropAllScript);
dropAllScriptTrigger);

sqlCommand.ExecuteNonQuery();
this.WriteTraceMessage(TraceLevel.Verbose, $"Trigger {_dataBaseObjectsNamingConvention} created.");
Expand Down Expand Up @@ -614,9 +608,18 @@ protected virtual string PrepareScriptProcedureQueueActivation(string dropAllScr
return this.ActivateDatabaseLogging ? script : this.RemoveLogOperations(script);
}

protected virtual string PrepareScriptDropAll(string dropMessages)
protected virtual string PrepareScriptDropAll(string dropMessages, bool dropFromTrigger)
{
var script = string.Format(SqlScripts.ScriptDropAll, _dataBaseObjectsNamingConvention, dropMessages, _schemaName);
string sDropProc="", sDropTrig="";
if (dropFromTrigger)
{
sDropTrig = string.Format("DISABLE TRIGGER [{2}].[tr_{0}_Sender] ON [{2}].[{3}];DROP TRIGGER [{2}].[tr_{0}_Sender];", _dataBaseObjectsNamingConvention, dropMessages, _schemaName, _tableName);
}
else
{
sDropProc = string.Format("DISABLE TRIGGER [{2}].[tr_{0}_Sender] ON [{2}].[{3}];DROP TRIGGER [{2}].[tr_{0}_Sender];", _dataBaseObjectsNamingConvention, dropMessages, _schemaName, _tableName);
}
var script = string.Format(SqlScripts.ScriptDropAll, _dataBaseObjectsNamingConvention, dropMessages, _schemaName, sDropProc, sDropTrig);
return this.ActivateDatabaseLogging ? script : this.RemoveLogOperations(script);
}

Expand Down