Skip to content
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
12 changes: 11 additions & 1 deletion src/EFCore.PG/Migrations/Internal/NpgsqlHistoryRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Conventions;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Internal;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal;

Expand Down Expand Up @@ -105,7 +106,16 @@ protected override IReadOnlyList<MigrationCommand> GetCreateCommands()
var model = EnsureModel();

var operations = Dependencies.ModelDiffer.GetDifferences(null, model.GetRelationalModel());
var commandList = Dependencies.MigrationsSqlGenerator.Generate(operations, model);

// Workaround for https://github.com/npgsql/efcore.pg/issues/3496: filter out extension-related operations.
// On managed PostgreSQL services (e.g. Azure Flexible Server), CREATE EXTENSION requires sometimes superuser privileges
// even with IF NOT EXISTS. Since extension creation isn't needed for the history table, we exclude these
// operations to avoid permission errors for non-superuser application accounts.
var operationsWithoutExtensions = operations
.Where(o => !o.GetAnnotations().Any(a => a.Name.StartsWith(NpgsqlAnnotationNames.PostgresExtensionPrefix, StringComparison.Ordinal)))
.ToList();

var commandList = Dependencies.MigrationsSqlGenerator.Generate(operationsWithoutExtensions, model);
return commandList;
}

Expand Down
35 changes: 35 additions & 0 deletions test/EFCore.PG.Tests/Migrations/NpgsqlHistoryRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'my') THEN
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

""", sql, ignoreLineEndingDifferences: true);
}

[ConditionalFact]
public void GetCreateIfNotExistsScript_works_with_schema_and_extension()
{
var sql = CreateHistoryRepositoryWithNetTopologySuite("my").GetCreateIfNotExistsScript();

Assert.Equal(
"""
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = 'my') THEN
CREATE SCHEMA my;
END IF;
END $EF$;
CREATE TABLE IF NOT EXISTS my."__EFMigrationsHistory" (
"MigrationId" character varying(150) NOT NULL,
"ProductVersion" character varying(32) NOT NULL,
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

""", sql, ignoreLineEndingDifferences: true);
}

Expand Down Expand Up @@ -156,6 +178,19 @@ private static IHistoryRepository CreateHistoryRepository(string schema = null)
.Options)
.GetService<IHistoryRepository>();

private static IHistoryRepository CreateHistoryRepositoryWithNetTopologySuite(string schema = null)
=> new TestDbContext(
new DbContextOptionsBuilder()
.UseInternalServiceProvider(
NpgsqlTestHelpers.Instance.CreateServiceProvider(
new ServiceCollection().AddEntityFrameworkNpgsqlNetTopologySuite()))
.UseNpgsql(
new NpgsqlConnection("Host=localhost;Database=DummyDatabase"),
b => b.MigrationsHistoryTable(HistoryRepository.DefaultTableName, schema)
.UseNetTopologySuite())
.Options)
.GetService<IHistoryRepository>();

private class TestDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<Blog> Blogs { get; set; }
Expand Down