Skip to content

Commit 04f64be

Browse files
authored
Add LeasesTableName to SqlTriggerAttribute (#893)
* addLeasesTableNameSetting to SqlTriggerAttribute * add provider test * add samples * remove setting * add new constructor * fix metrics provider * add integration test * fix oop * fix test * cleanup + pr comments * quote escape leasestablename
1 parent f5b5b9c commit 04f64be

File tree

23 files changed

+251
-22
lines changed

23 files changed

+251
-22
lines changed

Worker.Extensions.Sql/src/SqlTriggerAttribute.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,21 @@ public sealed class SqlTriggerAttribute : TriggerBindingAttribute
1313
/// </summary>
1414
/// <param name="tableName">Name of the table to watch for changes.</param>
1515
/// <param name="connectionStringSetting">The name of the app setting where the SQL connection string is stored</param>
16-
public SqlTriggerAttribute(string tableName, string connectionStringSetting)
16+
/// <param name="leasesTableName">Optional - The name of the table used to store leases. If not specified, the leases table name will be Leases_{FunctionId}_{TableId}</param>
17+
public SqlTriggerAttribute(string tableName, string connectionStringSetting, string leasesTableName = null)
1718
{
1819
this.TableName = tableName ?? throw new ArgumentNullException(nameof(tableName));
1920
this.ConnectionStringSetting = connectionStringSetting ?? throw new ArgumentNullException(nameof(connectionStringSetting));
21+
this.LeasesTableName = leasesTableName;
2022
}
2123

24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="SqlTriggerAttribute"/> class with null value for LeasesTableName.
26+
/// </summary>
27+
/// <param name="tableName">Name of the table to watch for changes.</param>
28+
/// <param name="connectionStringSetting">The name of the app setting where the SQL connection string is stored</param>
29+
public SqlTriggerAttribute(string tableName, string connectionStringSetting) : this(tableName, connectionStringSetting, null) { }
30+
2231
/// <summary>
2332
/// Name of the app setting containing the SQL connection string.
2433
/// </summary>
@@ -28,5 +37,13 @@ public SqlTriggerAttribute(string tableName, string connectionStringSetting)
2837
/// Name of the table to watch for changes.
2938
/// </summary>
3039
public string TableName { get; }
40+
41+
/// <summary>
42+
/// Name of the table used to store leases.
43+
/// If not specified, the leases table name will be Leases_{FunctionId}_{TableId}
44+
/// More information on how this is generated can be found here
45+
/// https://github.com/Azure/azure-functions-sql-extension/blob/release/trigger/docs/TriggerBinding.md#az_funcleases_
46+
/// </summary>
47+
public string LeasesTableName { get; }
3148
}
3249
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using Microsoft.Azure.WebJobs.Extensions.Sql.Samples.Common;
6+
using Microsoft.Extensions.Logging;
7+
using Newtonsoft.Json;
8+
9+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.Samples.TriggerBindingSamples
10+
{
11+
public static class ProductsTriggerLeasesTableName
12+
{
13+
[FunctionName(nameof(ProductsTriggerLeasesTableName))]
14+
public static void Run(
15+
[SqlTrigger("[dbo].[Products]", "SqlConnectionString", "Leases")]
16+
IReadOnlyList<SqlChange<Product>> changes,
17+
ILogger logger)
18+
{
19+
logger.LogInformation("SQL Changes: " + JsonConvert.SerializeObject(changes));
20+
}
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"bindings": [
3+
{
4+
"name": "changes",
5+
"type": "sqlTrigger",
6+
"direction": "in",
7+
"tableName": "dbo.Products",
8+
"connectionStringSetting": "SqlConnectionString",
9+
"leasesTableName": "Leases"
10+
}
11+
],
12+
"disabled": false
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#load "../Common/product.csx"
2+
#r "Newtonsoft.Json"
3+
#r "Microsoft.Azure.WebJobs.Extensions.Sql"
4+
5+
using System.Net;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Extensions.Primitives;
8+
using Newtonsoft.Json;
9+
using Microsoft.Azure.WebJobs.Extensions.Sql;
10+
11+
public static void Run(IReadOnlyList<SqlChange<Product>> changes, ILogger log)
12+
{
13+
log.LogInformation("SQL Changes: " + JsonConvert.SerializeObject(changes));
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"bindings": [
3+
{
4+
"name": "changes",
5+
"type": "sqlTrigger",
6+
"direction": "in",
7+
"tableName": "dbo.Products",
8+
"connectionStringSetting": "SqlConnectionString",
9+
"leasesTableName": "Leases"
10+
}
11+
],
12+
"disabled": false
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
module.exports = async function (context, changes) {
5+
context.log(`SQL Changes: ${JSON.stringify(changes)}`)
6+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using Microsoft.Azure.Functions.Worker;
6+
using Microsoft.Azure.Functions.Worker.Extensions.Sql;
7+
using Microsoft.Azure.WebJobs.Extensions.Sql.SamplesOutOfProc.Common;
8+
using Microsoft.Extensions.Logging;
9+
using Newtonsoft.Json;
10+
using System;
11+
12+
namespace Microsoft.Azure.WebJobs.Extensions.Sql.SamplesOutOfProc.TriggerBindingSamples
13+
{
14+
public class ProductsTriggerLeasesTableName
15+
{
16+
private static readonly Action<ILogger, string, Exception> _loggerMessage = LoggerMessage.Define<string>(LogLevel.Information, eventId: new EventId(0, "INFO"), formatString: "{Message}");
17+
18+
[Function("ProductsTriggerLeasesTableName")]
19+
public static void Run(
20+
[SqlTrigger("[dbo].[Products]", "SqlConnectionString", "Leases")]
21+
IReadOnlyList<SqlChange<Product>> changes, FunctionContext context)
22+
{
23+
if (changes != null && changes.Count > 0)
24+
{
25+
_loggerMessage(context.GetLogger("ProductsTriggerLeasesTableName"), "SQL Changes: " + JsonConvert.SerializeObject(changes), null);
26+
}
27+
}
28+
}
29+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"bindings": [
3+
{
4+
"name": "changes",
5+
"type": "sqlTrigger",
6+
"direction": "in",
7+
"tableName": "dbo.Products",
8+
"connectionStringSetting": "SqlConnectionString",
9+
"leasesTableName": "Leases"
10+
}
11+
],
12+
"disabled": false
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using namespace System.Net
5+
6+
param($changes)
7+
8+
$changesJson = $changes | ConvertTo-Json -Compress
9+
Write-Host "SQL Changes: $changesJson"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import json
5+
import logging
6+
7+
def main(changes):
8+
logging.info("SQL Changes: %s", json.loads(changes))

0 commit comments

Comments
 (0)