Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1528 from Invinsiber/feature/postgresql-overridin…
…g-system-value Add support for PostgreSQL Overriding System|User Value
- Loading branch information
Showing
8 changed files
with
335 additions
and
2 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...luentMigrator.Extensions.Postgres/Postgres/PostgresExtensions.OverridingIdentityValues.cs
This file contains 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,73 @@ | ||
#region License | ||
// Copyright (c) 2021, FluentMigrator Project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#endregion | ||
|
||
using System; | ||
|
||
using FluentMigrator.Builders.Insert; | ||
using FluentMigrator.Infrastructure; | ||
using FluentMigrator.Infrastructure.Extensions; | ||
|
||
namespace FluentMigrator.Postgres | ||
{ | ||
public static partial class PostgresExtensions | ||
{ | ||
public const string OverridingIdentityValues = "PostgresOverridingIdentityValues"; | ||
|
||
/// <summary> | ||
/// Adds an OVERRIDING SYSTEM VALUE clause in the current <see cref="IInsertDataSyntax"/> expression. | ||
/// This enables the system-generated values to be overriden with the user-specified explicit values (other than <c>DEFAULT</c>) | ||
/// for identity columns defined as <c>GENERATED ALWAYS</c> | ||
/// </summary> | ||
/// <param name="expression">The current <see cref="IInsertDataSyntax"/> expression</param> | ||
/// <returns>The current <see cref="IInsertDataSyntax"/> expression</returns> | ||
public static IInsertDataSyntax WithOverridingSystemValue(this IInsertDataSyntax expression) => | ||
SetOverridingIdentityValues(expression, PostgresOverridingIdentityValuesType.System, nameof(WithOverridingSystemValue)); | ||
|
||
/// <summary> | ||
/// Adds an OVERRIDING USER VALUE clause in the current <see cref="IInsertDataSyntax"/> expression. | ||
/// Any user-specified values will be ignored and the system-generated values will be applied | ||
/// for identity columns defined as <c>GENERATED BY DEFAULT</c> | ||
/// </summary> | ||
/// <param name="expression">The current <see cref="IInsertDataSyntax"/> expression</param> | ||
/// <returns>The current <see cref="IInsertDataSyntax"/> expression</returns> | ||
public static IInsertDataSyntax WithOverridingUserValue(this IInsertDataSyntax expression) => | ||
SetOverridingIdentityValues(expression, PostgresOverridingIdentityValuesType.User, nameof(WithOverridingUserValue)); | ||
|
||
/// <summary> | ||
/// Set the additional feature for overriding identity values with the specified <see cref="PostgresOverridingIdentityValuesType"/> | ||
/// on the provided <see cref="IInsertDataSyntax"/> expression | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException"></exception> | ||
private static IInsertDataSyntax SetOverridingIdentityValues( | ||
IInsertDataSyntax expression, | ||
PostgresOverridingIdentityValuesType overridingType, | ||
string callingMethod) | ||
{ | ||
if (!(expression is ISupportAdditionalFeatures castExpression)) | ||
{ | ||
throw new InvalidOperationException( | ||
string.Format( | ||
ErrorMessages.MethodXMustBeCalledOnObjectImplementingY, | ||
callingMethod, | ||
nameof(ISupportAdditionalFeatures))); | ||
} | ||
|
||
castExpression.SetAdditionalFeature(OverridingIdentityValues, overridingType); | ||
|
||
return expression; | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/FluentMigrator.Extensions.Postgres/PostgresOverridingIdentityValuesType.cs
This file contains 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,33 @@ | ||
#region License | ||
// Copyright (c) 2021, FluentMigrator Project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#endregion | ||
|
||
namespace FluentMigrator | ||
{ | ||
/// <summary> | ||
/// Determines if the system-generated values or the user-supplied values are applied for identity columns | ||
/// </summary> | ||
public enum PostgresOverridingIdentityValuesType | ||
{ | ||
/// <summary> | ||
/// Overrides the system-generated values with user-supplied values for identity columns | ||
/// </summary> | ||
System, | ||
/// <summary> | ||
/// Ignores the user-supplied values and applies the system-generated values for identity columns | ||
/// </summary> | ||
User, | ||
} | ||
} |
This file contains 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 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 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 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
62 changes: 62 additions & 0 deletions
62
test/FluentMigrator.Tests/Unit/Generators/Postgres10_0/Postgres10_0DataTests.cs
This file contains 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,62 @@ | ||
#region License | ||
// Copyright (c) 2021, FluentMigrator Project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#endregion | ||
|
||
using FluentMigrator.Postgres; | ||
using FluentMigrator.Runner.Generators.Postgres; | ||
using FluentMigrator.Tests.Unit.Generators.Postgres; | ||
|
||
using NUnit.Framework; | ||
|
||
using Shouldly; | ||
|
||
namespace FluentMigrator.Tests.Unit.Generators.Postgres10_0 | ||
{ | ||
[TestFixture] | ||
public class Postgres10_0DataTests : PostgresDataTests | ||
{ | ||
/// <inheritdoc /> | ||
protected override PostgresGenerator CreateGenerator(PostgresQuoter quoter) | ||
{ | ||
return new Postgres10_0Generator(quoter); | ||
} | ||
|
||
[Test] | ||
public override void CanInsertWithOverridingSystemValue() | ||
{ | ||
var expression = GeneratorTestHelper.GetInsertDataExpression(); | ||
expression.AdditionalFeatures[PostgresExtensions.OverridingIdentityValues] = PostgresOverridingIdentityValuesType.System; | ||
|
||
var expected = "INSERT INTO \"public\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") OVERRIDING SYSTEM VALUE VALUES (1,'Just''in','codethinked.com');"; | ||
expected += "INSERT INTO \"public\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") OVERRIDING SYSTEM VALUE VALUES (2,'Na\\te','kohari.org');"; | ||
|
||
var result = Generator.Generate(expression); | ||
result.ShouldBe(expected); | ||
} | ||
|
||
[Test] | ||
public override void CanInsertWithOverridingUserValue() | ||
{ | ||
var expression = GeneratorTestHelper.GetInsertDataExpression(); | ||
expression.AdditionalFeatures[PostgresExtensions.OverridingIdentityValues] = PostgresOverridingIdentityValuesType.User; | ||
|
||
var expected = "INSERT INTO \"public\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") OVERRIDING USER VALUE VALUES (1,'Just''in','codethinked.com');"; | ||
expected += "INSERT INTO \"public\".\"TestTable1\" (\"Id\",\"Name\",\"Website\") OVERRIDING USER VALUE VALUES (2,'Na\\te','kohari.org');"; | ||
|
||
var result = Generator.Generate(expression); | ||
result.ShouldBe(expected); | ||
} | ||
} | ||
} |
Oops, something went wrong.