Skip to content

Commit 2e22e8b

Browse files
committed
Added drop functions
1 parent ec7743a commit 2e22e8b

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

Readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ features.
1515
enhancing security and balancing database load.
1616
3. **Natural Sorting**: Provides way to calculate natural sort compliant order for string, which can be used
1717
in `ORDER BY` clause. This is useful for sorting strings that contain numbers in a human-friendly way.
18+
4. **Schema Rollback Helpers**: Extension methods `DropRandomIdSequence` and `DropNaturalSortKeyFunction` simplify
19+
cleanup in `Down` migrations.
1820

1921
## Installation
2022

@@ -109,6 +111,8 @@ public partial class PgFunction : Migration
109111
/// <inheritdoc />
110112
protected override void Down(MigrationBuilder migrationBuilder)
111113
{
114+
migrationBuilder.DropRandomIdSequence("animal", "id");
115+
112116
migrationBuilder.DropTable(
113117
name: "animal");
114118
}
@@ -138,6 +142,10 @@ substrings numerically.
138142
// Create the natural sort key function in PostgreSQL
139143
migrationBuilder.CreateNaturalSortKeyFunction();
140144

145+
protected override void Down(MigrationBuilder migrationBuilder)
146+
{
147+
migrationBuilder.DropNaturalSortKeyFunction();
148+
}
141149
}
142150
}
143151
```
@@ -154,6 +162,7 @@ substrings numerically.
154162
}
155163
}
156164
```
165+
157166
When you query the entity, simply `ORDER BY AddressNaturalSortKey` to get truenaturalordering in PostgreSQL.
158167

159168
## License

src/EFCore.PostgresExtensions/EFCore.PostgresExtensions.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>5.1.0</Version>
11+
<Version>5.1.1</Version>
1212
<PackageId>Pandatech.EFCore.PostgresExtensions</PackageId>
1313
<Title>Pandatech.EFCore.PostgresExtensions</Title>
1414
<PackageTags>Pandatech, library, EntityFrameworkCore, PostgreSQL, For Update, Lock, LockingSyntax, Bulk insert, BinaryCopy</PackageTags>
1515
<Description>The Pandatech.EFCore.PostgresExtensions library enriches Entity Framework Core applications with advanced PostgreSQL functionalities, starting with the ForUpdate locking syntax and BulkInsert function. Designed for seamless integration, this NuGet package aims to enhance the efficiency and capabilities of EF Core models when working with PostgreSQL, with the potential for further PostgreSQL-specific extensions.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-efcore-postgres-extensions</RepositoryUrl>
17-
<PackageReleaseNotes>Added support to generate unique index on encrypted columns</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added down function for natural sort key and random id sequence</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

src/EFCore.PostgresExtensions/Extensions/MigrationBuilderExtensions.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,52 @@ CREATE UNIQUE INDEX {indexName}
4040

4141
return migrationBuilder;
4242
}
43+
44+
/// <summary>
45+
/// Removes the random‑ID generator function *and* its backing sequence.
46+
/// </summary>
47+
public static void DropRandomIdSequence(this MigrationBuilder migrationBuilder,
48+
string tableName,
49+
string pkName)
50+
{
51+
var sequenceName = $"{tableName}_{pkName}_seq";
52+
var functionName = $"{tableName}_random_id_generator";
53+
54+
var sql = $"""
55+
DO $$
56+
BEGIN
57+
-- drop function if it exists
58+
IF EXISTS (
59+
SELECT 1
60+
FROM pg_proc
61+
WHERE proname = '{functionName}'
62+
) THEN
63+
DROP FUNCTION IF EXISTS {functionName}();
64+
END IF;
65+
66+
-- drop sequence if it exists
67+
IF EXISTS (
68+
SELECT 1
69+
FROM pg_class
70+
WHERE relkind = 'S'
71+
AND relname = '{sequenceName}'
72+
) THEN
73+
DROP SEQUENCE IF EXISTS {sequenceName};
74+
END IF;
75+
END
76+
$$;
77+
""";
78+
79+
migrationBuilder.Sql(sql);
80+
}
81+
82+
/// <summary>
83+
/// Removes the natural‑sort‑key function.
84+
/// </summary>
85+
public static void DropNaturalSortKeyFunction(this MigrationBuilder migrationBuilder)
86+
{
87+
migrationBuilder.Sql("DROP FUNCTION IF EXISTS get_natural_sort_key(TEXT);");
88+
}
4389

4490
public static MigrationBuilder CreateUniqueIndexOnEncryptedColumn(this MigrationBuilder migrationBuilder,
4591
string tableName,

test/PandaNuGet.Demo/PandaNuGet.Demo.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
<ItemGroup>
1111
<PackageReference Include="Dapper" Version="2.1.66" />
1212
<PackageReference Include="EFCore.BulkExtensions.PostgreSql" Version="9.0.1" />
13-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.3" />
14-
<PackageReference Include="Pandatech.Crypto" Version="4.1.2" />
13+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
1514
<PackageReference Include="Pandatech.EFCoreQueryMagic" Version="1.1.0" />
16-
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.0.0" />
15+
<PackageReference Include="Swashbuckle.AspNetCore" Version="8.1.1" />
1716
</ItemGroup>
1817

1918
<ItemGroup>

test/PandaNuGet.Demo/Services/GetByFirstBytesService.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using PandaNuGet.Demo.Context;
44
using PandaNuGet.Demo.Entities;
55
using Pandatech.Crypto;
6-
using Pandatech.Crypto.Helpers;
76
using PostgresDbContext = PandaNuGet.Demo.Context.PostgresDbContext;
87

98
namespace PandaNuGet.Demo.Services;
@@ -81,12 +80,11 @@ public static IQueryable<T> WhereStartWithBytes<T>(this IQueryable<T> source,
8180

8281
// MethodInfo for the substr method
8382
var methodInfo = typeof(PostgresDbContext).GetMethod(nameof(PostgresDbContext.substr),
84-
new[]
85-
{
86-
typeof(byte[]),
83+
[
84+
typeof(byte[]),
8785
typeof(int),
8886
typeof(int)
89-
});
87+
]);
9088

9189
// Call to substr method
9290
var call = Expression.Call(

test/PandaNuGet.Tests/PandaNuGet.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
1414
<PackageReference Include="xunit" Version="2.9.3" />
15-
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
15+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.0">
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
<PrivateAssets>all</PrivateAssets>
1818
</PackageReference>

0 commit comments

Comments
 (0)