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
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public override void Generate(IModel model, CSharpRuntimeAnnotationCodeGenerator
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
annotations.Remove(NpgsqlAnnotationNames.Encoding);

#pragma warning disable CS0618
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
Expand Down Expand Up @@ -232,6 +233,7 @@ public override void Generate(IRelationalModel model, CSharpRuntimeAnnotationCod
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
annotations.Remove(NpgsqlAnnotationNames.Encoding);

#pragma warning disable CS0618
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,20 @@ public static ModelBuilder UseTablespace(

#endregion

#region Encoding
/// <summary>
/// Specifies the encoding to use when creating a new database for this model.
/// </summary>
public static ModelBuilder UseDatabaseEncoding(this ModelBuilder modelBuilder, string encoding)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotEmpty(encoding, nameof(encoding));

modelBuilder.Model.SetDatabaseEncoding(encoding);
return modelBuilder;
}
#endregion

#region Collation management

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,24 @@ public static IReadOnlyList<PostgresCollation> GetCollations(this IReadOnlyModel
=> PostgresCollation.GetCollations(model).ToArray();

#endregion Collation management

#region Encoding
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string? GetEncoding(this IReadOnlyModel model)
=> (string?)model[NpgsqlAnnotationNames.Encoding];

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static void SetDatabaseEncoding(this IMutableModel model, string? encoding)
=> model.SetOrRemoveAnnotation(NpgsqlAnnotationNames.Encoding, encoding);
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected override void ProcessModelAnnotations(
annotations.Remove(NpgsqlAnnotationNames.DatabaseTemplate);
annotations.Remove(NpgsqlAnnotationNames.Tablespace);
annotations.Remove(NpgsqlAnnotationNames.CollationDefinitionPrefix);
annotations.Remove(NpgsqlAnnotationNames.Encoding);

#pragma warning disable CS0618
annotations.Remove(NpgsqlAnnotationNames.DefaultColumnCollation);
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore.PG/Metadata/Internal/NpgsqlAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ public static class NpgsqlAnnotationNames
/// </summary>
public const string DatabaseTemplate = Prefix + "DatabaseTemplate";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public const string Encoding = Prefix + "Encoding";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,14 @@ protected virtual void Generate(NpgsqlCreateDatabaseOperation operation, IModel?
.Append(DelimitIdentifier(operation.Collation));
}

if (!string.IsNullOrEmpty(operation.Encoding))
{
builder
.AppendLine()
.Append("ENCODING ")
.Append(DelimitIdentifier(operation.Encoding));
}

builder.AppendLine(";");

EndStatement(builder, suppressTransaction: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public class NpgsqlCreateDatabaseOperation : DatabaseOperation
/// The PostgreSQL tablespace in which to create the database.
/// </summary>
public virtual string? Tablespace { get; set; }

/// <summary>
/// The PostgreSQL encoding to use for the database.
/// </summary>
public virtual string? Encoding { get; set; }
}
3 changes: 2 additions & 1 deletion src/EFCore.PG/Storage/Internal/NpgsqlDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ private IReadOnlyList<MigrationCommand> CreateCreateOperations()
Name = connection.DbConnection.Database,
Template = designTimeModel.GetDatabaseTemplate(),
Collation = designTimeModel.GetCollation(),
Tablespace = designTimeModel.GetTablespace()
Tablespace = designTimeModel.GetTablespace(),
Encoding = designTimeModel.GetEncoding(),
}
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public virtual void CreateDatabaseOperation_with_tablespace()
CREATE DATABASE some_db
TABLESPACE "MyTablespace";

""");
}

[Fact]
public virtual void CreateDatabaseOperation_with_encoding()
{
Generate(new NpgsqlCreateDatabaseOperation { Name = "Northwind", Encoding = "UTF8" });

AssertSql(
"""
CREATE DATABASE "Northwind"
ENCODING "UTF8";

""");
}

Expand Down