-
Notifications
You must be signed in to change notification settings - Fork 67
Fix schema parse error when using reserved keys as table names #881
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a514b3c
bracketed name for reserved words as table name
MaddyDev 60154b4
add test
MaddyDev e7847d9
add tests
MaddyDev 73992db
fix csx test
MaddyDev 2fb4d95
Merge branch 'release/trigger' into maddy/fixSchemaParseError
MaddyDev c4aba61
enable test only for csharp
MaddyDev 5f26783
fix TableNotPresentTest
MaddyDev b76a232
revert GetUserTableIdAsync change
MaddyDev efc572e
refactor GetUserTableIdAsync
MaddyDev a28b542
comment out Java test
MaddyDev 2c745af
refactor code to use SqlObject
MaddyDev bf26c21
Merge branch 'release/trigger' into maddy/fixSchemaParseError
MaddyDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Azure.Functions.Worker; | ||
| using Microsoft.Azure.Functions.Worker.Extensions.Sql; | ||
|
|
||
| namespace DotnetIsolatedTests | ||
| { | ||
| public static class ReservedTableNameTrigger | ||
| { | ||
| /// <summary> | ||
| /// Used in verification of the trigger function execution on table with reserved keys as name. | ||
| /// </summary> | ||
| [Function(nameof(ReservedTableNameTrigger))] | ||
| public static void Run( | ||
| [SqlTrigger("[dbo].[User]", "SqlConnectionString")] | ||
| IReadOnlyList<SqlChange<User>> changes, | ||
| FunctionContext context) | ||
| { | ||
| ILogger logger = context.GetLogger("ReservedTableNameTrigger"); | ||
| logger.LogInformation("SQL Changes: " + Utils.JsonSerializeObject(changes)); | ||
| } | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public string UserName { get; set; } | ||
| public int UserId { get; set; } | ||
| public string FullName { get; set; } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| if (obj is User) | ||
| { | ||
| var that = obj as User; | ||
| return this.UserId == that.UserId && this.UserName == that.UserName && this.FullName == that.FullName; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| DROP TABLE IF EXISTS [dbo].[User]; | ||
|
|
||
| CREATE TABLE [dbo].[User] ( | ||
| [UserId] [int] NOT NULL PRIMARY KEY, | ||
| [UserName] [nvarchar](50) NOT NULL, | ||
| [FullName] [nvarchar](max) NULL | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -656,6 +656,71 @@ JOIN sys.columns c | |
| Assert.True(1 == (int)this.ExecuteScalar("SELECT 1 FROM sys.columns WHERE Name = N'LastAccessTime' AND Object_ID = Object_ID(N'[az_func].[GlobalState]')"), $"{GlobalStateTableName} should have {LastAccessTimeColumnName} column after restarting the listener."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests that trigger function executes on table whose name is a reserved word (User). | ||
| /// </summary> | ||
| [Theory] | ||
| [SqlInlineData()] | ||
| [UnsupportedLanguages(SupportedLanguages.Java)] // test timing out for Java | ||
| public async void ReservedTableNameTest(SupportedLanguages lang) | ||
| { | ||
| this.SetChangeTrackingForTable("User"); | ||
| this.StartFunctionHost(nameof(ReservedTableNameTrigger), lang, true); | ||
| User expectedResponse = Utils.JsonDeserializeObject<User>(/*lang=json,strict*/ "{\"UserId\":999,\"UserName\":\"test\",\"FullName\":\"Testy Test\"}"); | ||
| int index = 0; | ||
| string messagePrefix = "SQL Changes: "; | ||
|
|
||
| var taskCompletion = new TaskCompletionSource<bool>(); | ||
|
|
||
| void MonitorOutputData(object sender, DataReceivedEventArgs e) | ||
| { | ||
| if (e.Data != null && (index = e.Data.IndexOf(messagePrefix, StringComparison.Ordinal)) >= 0) | ||
| { | ||
| string json = e.Data[(index + messagePrefix.Length)..]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have this logic in a couple of places now, could you make a issue to follow up on for consolidating this into a single helper? |
||
| // Sometimes we'll get messages that have extra logging content on the same line - so to prevent that from breaking | ||
| // the deserialization we look for the end of the changes array and only use that. | ||
| // (This is fine since we control what content is in the array so know that none of the items have a ] in them) | ||
| json = json[..(json.IndexOf(']') + 1)]; | ||
| IReadOnlyList<SqlChange<User>> changes; | ||
| try | ||
| { | ||
| changes = Utils.JsonDeserializeObject<IReadOnlyList<SqlChange<User>>>(json); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| throw new InvalidOperationException($"Exception deserializing JSON content. Error={ex.Message} Json=\"{json}\"", ex); | ||
| } | ||
| Assert.Equal(SqlChangeOperation.Insert, changes[0].Operation); // Expected change operation | ||
| User user = changes[0].Item; | ||
| Assert.NotNull(user); // user deserialized correctly | ||
| Assert.Equal(expectedResponse, user); // user has the expected values | ||
| taskCompletion.SetResult(true); | ||
| } | ||
| }; | ||
|
|
||
| // Set up listener for the changes coming in | ||
| foreach (Process functionHost in this.FunctionHostList) | ||
| { | ||
| functionHost.OutputDataReceived += MonitorOutputData; | ||
| } | ||
|
|
||
| // Now that we've set up our listener trigger the actions to monitor | ||
| this.ExecuteNonQuery("INSERT INTO [dbo].[User] VALUES (" + | ||
| "999, " + // UserId, | ||
| "'test', " + // UserName | ||
| "'Testy Test')"); // FullName | ||
|
|
||
| // Now wait until either we timeout or we've gotten all the expected changes, whichever comes first | ||
| this.LogOutput($"[{DateTime.UtcNow:u}] Waiting for Insert changes (10000ms)"); | ||
| await taskCompletion.Task.TimeoutAfter(TimeSpan.FromMilliseconds(10000), $"Timed out waiting for Insert changes."); | ||
|
|
||
| // Unhook handler since we're done monitoring these changes so we aren't checking other changes done later | ||
| foreach (Process functionHost in this.FunctionHostList) | ||
| { | ||
| functionHost.OutputDataReceived -= MonitorOutputData; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Ensures that all column types are serialized correctly. | ||
| /// </summary> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.Sql.Tests.Integration | ||
| { | ||
| public static class ReservedTableNameTrigger | ||
| { | ||
| /// <summary> | ||
| /// Used in verification of the trigger function execution on table with reserved keys as name. | ||
| /// </summary> | ||
| [FunctionName(nameof(ReservedTableNameTrigger))] | ||
| public static void Run( | ||
| [SqlTrigger("[dbo].[User]", "SqlConnectionString")] | ||
| IReadOnlyList<SqlChange<User>> changes, | ||
| ILogger logger) | ||
| { | ||
| // The output is used to inspect the trigger binding parameter in test methods. | ||
| logger.LogInformation("SQL Changes: " + Utils.JsonSerializeObject(changes)); | ||
| } | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public string UserName { get; set; } | ||
| public int UserId { get; set; } | ||
| public string FullName { get; set; } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| if (obj is User) | ||
| { | ||
| var that = obj as User; | ||
| return this.UserId == that.UserId && this.UserName == that.UserName && this.FullName == that.FullName; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
test/Integration/test-csx/ReservedTableNameTrigger/function.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "[dbo].[User]", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
32 changes: 32 additions & 0 deletions
32
test/Integration/test-csx/ReservedTableNameTrigger/run.csx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #r "Newtonsoft.Json" | ||
| #r "Microsoft.Azure.WebJobs.Extensions.Sql" | ||
|
|
||
| using System.Net; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.Extensions.Primitives; | ||
| using Newtonsoft.Json; | ||
| using Microsoft.Azure.WebJobs.Extensions.Sql; | ||
|
|
||
| public static void Run(IReadOnlyList<SqlChange<User>> changes, ILogger log) | ||
| { | ||
| // The output is used to inspect the trigger binding parameter in test methods. | ||
| log.LogInformation("SQL Changes: " + Microsoft.Azure.WebJobs.Extensions.Sql.Utils.JsonSerializeObject(changes)); | ||
| } | ||
|
|
||
| public class User | ||
| { | ||
| public string UserName { get; set; } | ||
| public int UserId { get; set; } | ||
| public string FullName { get; set; } | ||
|
|
||
| public override bool Equals(object obj) | ||
| { | ||
| if (obj is User) | ||
| { | ||
| var that = obj as User; | ||
| return this.UserId == that.UserId && this.UserName == that.UserName && this.FullName == that.FullName; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
test/Integration/test-java/src/main/java/com/function/Common/User.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for | ||
| * license information. | ||
| */ | ||
|
|
||
| package com.function.Common; | ||
|
|
||
| public class User { | ||
| private int UserId; | ||
| private String UserName; | ||
| private String FullName; | ||
|
|
||
| public User(int userId, String userName, String fullName) { | ||
| UserId = userId; | ||
| UserName = userName; | ||
| FullName = fullName; | ||
| } | ||
|
|
||
| public int getUserId() { | ||
| return UserId; | ||
| } | ||
|
|
||
| public String getUserName() { | ||
| return UserName; | ||
| } | ||
|
|
||
| public String getFullName() { | ||
| return FullName; | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
test/Integration/test-java/src/main/java/com/function/ReservedTableNameTrigger.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for | ||
| * license information. | ||
| */ | ||
|
|
||
| package com.function; | ||
|
|
||
| import com.function.Common.User; | ||
| import com.google.gson.Gson; | ||
| import com.microsoft.azure.functions.ExecutionContext; | ||
| import com.microsoft.azure.functions.annotation.FunctionName; | ||
| import com.microsoft.azure.functions.sql.annotation.SQLTrigger; | ||
|
|
||
| import java.util.logging.Level; | ||
|
|
||
| public class ReservedTableNameTrigger { | ||
| @FunctionName("ReservedTableNameTrigger") | ||
| public void run( | ||
| @SQLTrigger( | ||
| name = "changes", | ||
| tableName = "[dbo].[User]", | ||
| connectionStringSetting = "SqlConnectionString") | ||
| User[] changes, | ||
| ExecutionContext context) throws Exception { | ||
|
|
||
| context.getLogger().log(Level.INFO, "SQL Changes: " + new Gson().toJson(changes)); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
test/Integration/test-js/ReservedTableNameTrigger/function.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "bindings": [ | ||
| { | ||
| "name": "changes", | ||
| "type": "sqlTrigger", | ||
| "direction": "in", | ||
| "tableName": "[dbo].[User]", | ||
| "connectionStringSetting": "SqlConnectionString" | ||
| } | ||
| ], | ||
| "disabled": false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
|
||
| module.exports = async function (context, changes) { | ||
| context.log(`SQL Changes: ${JSON.stringify(changes)}`) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.