Skip to content

Commit

Permalink
chore: pg distributed cache (#14031)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic authored Nov 11, 2024
1 parent 0e4b916 commit 1fc22c5
Show file tree
Hide file tree
Showing 24 changed files with 301 additions and 55 deletions.
1 change: 1 addition & 0 deletions backend/packagegroups/NuGet.props
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<PackageReference Update="DotNetEnv" Version="3.1.1" />
<PackageReference Update="NuGet.Versioning" Version="6.11.1" />
<PackageReference Update="DistributedLock.Postgres" Version="1.2.0" />
<PackageReference Update="Community.Microsoft.Extensions.Caching.PostgreSql" Version="4.0.6" />
</ItemGroup>

<ItemGroup Label="Packages used for testing">
Expand Down
3 changes: 2 additions & 1 deletion backend/src/Designer/Designer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="Altinn.Common.AccessTokenClient" />
<PackageReference Include="Altinn.Platform.Storage.Interface" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
<PackageReference Include="Community.Microsoft.Extensions.Caching.PostgreSql" />
<PackageReference Include="CompilerAttributes" />
<PackageReference Include="DistributedLock.Postgres" />
<PackageReference Include="DotNetEnv" />
Expand Down Expand Up @@ -167,7 +168,7 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Migrations/InitialSqlScripts/*.sql" />
<EmbeddedResource Include="Migrations/SqlScripts/**/*.sql" />
</ItemGroup>

<Target Name="AfterPublishScript" AfterTargets="Publish">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ public class PostgreSQLSettings : ISettingsMarker
/// Password for app user for the postgres db
/// </summary>
public string DesignerDbPwd { get; set; }

public string FormattedConnectionString()
{
return string.Format(ConnectionString, DesignerDbPwd);
}
}
}
5 changes: 1 addition & 4 deletions backend/src/Designer/Infrastructure/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public static IServiceCollection RegisterServiceImplementations(this IServiceCol
services.AddDbContext<DesignerdbContext>(options =>
{
PostgreSQLSettings postgresSettings = configuration.GetSection(nameof(PostgreSQLSettings)).Get<PostgreSQLSettings>();
string connectionString = string.Format(
postgresSettings.ConnectionString,
postgresSettings.DesignerDbPwd);
options.UseNpgsql(connectionString);
options.UseNpgsql(postgresSettings.FormattedConnectionString());
});

services.AddScoped<IReleaseRepository, ORMReleaseRepository>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public static IServiceCollection RegisterSynchronizationServices(this IServiceCo
services.AddSingleton<IDistributedLockProvider>(_ =>
{
PostgreSQLSettings postgresSettings = configuration.GetSection(nameof(PostgreSQLSettings)).Get<PostgreSQLSettings>();
string connectionString = string.Format(
postgresSettings.ConnectionString,
postgresSettings.DesignerDbPwd);
return new PostgresDistributedSynchronizationProvider(connectionString);
return new PostgresDistributedSynchronizationProvider(postgresSettings.FormattedConnectionString());
});
return services;
}
Expand Down
12 changes: 6 additions & 6 deletions backend/src/Designer/Migrations/20240409201554_InitMigration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Altinn.Studio.Designer.Migrations.InitialSqlScripts;
using Altinn.Studio.Designer.Migrations.SqlScripts;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable
Expand All @@ -12,11 +12,11 @@ public partial class InitMigration : Migration
protected override void Up(MigrationBuilder migrationBuilder)
{
// initial sql used in yuniql migration are idempotent so it is safe to execute them as initial ef-core migration
migrationBuilder.Sql(InitialScriptsReadHelper.ReadInitialSqlScript("01-setup-tables.sql"));
migrationBuilder.Sql(InitialScriptsReadHelper.ReadInitialSqlScript("02-setup-deployments.sql"));
migrationBuilder.Sql(InitialScriptsReadHelper.ReadInitialSqlScript("03-setup-releases.sql"));
migrationBuilder.Sql(InitialScriptsReadHelper.ReadInitialSqlScript("04-setup-grants.sql"));
migrationBuilder.Sql(InitialScriptsReadHelper.ReadInitialSqlScript("05-setup-index.sql"));
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("InitialSqlScripts/01-setup-tables.sql"));
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("InitialSqlScripts/02-setup-deployments.sql"));
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("InitialSqlScripts/03-setup-releases.sql"));
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("InitialSqlScripts/04-setup-grants.sql"));
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("InitialSqlScripts/05-setup-index.sql"));
}

/// <inheritdoc />
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Altinn.Studio.Designer.Migrations.SqlScripts;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Altinn.Studio.Designer.Migrations
{
/// <inheritdoc />
public partial class DistributedCacheTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("DistributedCache/Up/01-setup-distributedcache-table.sql"));
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("DistributedCache/Up/02-setup-grants.sql"));
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(SqlScriptsReadHelper.ReadSqlScript("DistributedCache/Down/01-drop-distributedcache-table.sql"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.8")
.HasAnnotation("ProductVersion", "8.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseSerialColumns(modelBuilder);
Expand All @@ -36,7 +36,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("character varying")
.HasColumnName("app");

b.Property<DateTime>("Created")
b.Property<DateTimeOffset>("Created")
.HasColumnType("timestamptz")
.HasColumnName("created");

Expand All @@ -58,6 +58,12 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("jsonb")
.HasColumnName("scopes");

b.Property<uint>("Version")
.IsConcurrencyToken()
.ValueGeneratedOnAddOrUpdate()
.HasColumnType("xid")
.HasColumnName("xmin");

b.HasKey("Id")
.HasName("app_scopes_pkey");

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS designer.distributedcache;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS designer.distributedcache
(
"Id" text COLLATE pg_catalog."default" NOT NULL,
"Value" bytea,
"ExpiresAtTime" timestamp with time zone,
"SlidingExpirationInSeconds" double precision,
"AbsoluteExpiration" timestamp with time zone,
CONSTRAINT "DistCache_pkey" PRIMARY KEY ("Id")
)

TABLESPACE pg_default;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRANT SELECT,INSERT,UPDATE,DELETE ON ALL TABLES IN SCHEMA designer TO designer;
25 changes: 25 additions & 0 deletions backend/src/Designer/Migrations/SqlScripts/SqlScriptsReadHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.IO;
using System.Linq;

namespace Altinn.Studio.Designer.Migrations.SqlScripts;

public class SqlScriptsReadHelper
{
public static string ReadSqlScript(string scriptName)
{
string resourceNameEnding = scriptName.Replace('/', '.');
if (!resourceNameEnding.EndsWith(".sql"))
{
throw new ArgumentException("Invalid script name");
}

var assembly = typeof(SqlScriptsReadHelper).Assembly;
string resourceName = assembly.GetManifestResourceNames()
.Single(x => x.EndsWith(resourceNameEnding));

using Stream stream = assembly.GetManifestResourceStream(resourceName);
using StreamReader reader = new(stream!);
return reader.ReadToEnd();
}
}
Loading

0 comments on commit 1fc22c5

Please sign in to comment.