Skip to content

Commit

Permalink
add migrations for postgresql distributed cache
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic committed Nov 11, 2024
1 parent 83dc419 commit d133fff
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 27 deletions.
2 changes: 1 addition & 1 deletion backend/src/Designer/Designer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
</ItemGroup>

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

<Target Name="AfterPublishScript" AfterTargets="Publish">
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.distribuedcache;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS designer.distribuedcache
(
"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();
}
}

0 comments on commit d133fff

Please sign in to comment.