Skip to content

Commit

Permalink
feat: Adding postgres support to ombi (beta) (#5050)
Browse files Browse the repository at this point in the history
  • Loading branch information
dben authored Jul 11, 2024
1 parent 8ff7d99 commit f8c6102
Show file tree
Hide file tree
Showing 22 changed files with 5,660 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Ombi.Core.Tests/Engine/MovieRequestLimitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,13 @@ public async Task UserPassedIn_MovieLimit_Set_Limit_Daily_AllRequestsToday()
{
UserId = "id1",
RequestType = RequestType.Movie,
RequestDate = today.AddHours(-1),
RequestDate = today.AddMinutes(-1),
},
new RequestLog
{
UserId = "id1",
RequestType = RequestType.Movie,
RequestDate = today.AddHours(-2),
RequestDate = today.AddMinutes(-2),
},
};
var repoMock = _mocker.GetMock<IRepository<RequestLog>>();
Expand Down
4 changes: 2 additions & 2 deletions src/Ombi.Core.Tests/Engine/MusicRequestLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,13 @@ public async Task UserPassedIn_MusicLimit_Set_Limit_Daily_AllRequestsToday()
{
UserId = "id1",
RequestType = RequestType.Album,
RequestDate = today.AddHours(-1),
RequestDate = today.AddMinutes(-1),
},
new RequestLog
{
UserId = "id1",
RequestType = RequestType.Album,
RequestDate = today.AddHours(-2),
RequestDate = today.AddMinutes(-2),
},
};
var repoMock = _mocker.GetMock<IRepository<RequestLog>>();
Expand Down
8 changes: 4 additions & 4 deletions src/Ombi.Core.Tests/Engine/TvRequestLimitsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,15 @@ public async Task UserPassedIn_TvLimit_Set_Limit_Daily_AllRequestsToday()
{
UserId = "id1",
RequestType = RequestType.TvShow,
RequestDate = today.AddHours(-1),
RequestDate = today.AddMinutes(-1),
EpisodeCount = 1,
},
new RequestLog
{
UserId = "id1",
RequestType = RequestType.TvShow,
EpisodeCount = 1,
RequestDate = today.AddHours(-2),
RequestDate = today.AddMinutes(-2),
},
};
var repoMock = _mocker.GetMock<IRepository<RequestLog>>();
Expand Down Expand Up @@ -345,15 +345,15 @@ public async Task UserPassedIn_TvLimit_Set_Limit_Daily_MultipleEpisodeRequests()
{
UserId = "id1",
RequestType = RequestType.TvShow,
RequestDate = today.AddHours(-1),
RequestDate = today.AddMinutes(-1),
EpisodeCount = 5,
},
new RequestLog
{
UserId = "id1",
RequestType = RequestType.TvShow,
EpisodeCount = 4,
RequestDate = today.AddHours(-2),
RequestDate = today.AddMinutes(-2),
},
};
var repoMock = _mocker.GetMock<IRepository<RequestLog>>();
Expand Down
4 changes: 2 additions & 2 deletions src/Ombi.Schedule.Tests/IssuesPurgeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public async Task DoesNot_Delete_AnyIssues()
new Issues
{
Status = IssueStatus.Resolved,
ResovledDate = DateTime.Now.AddDays(-2)
ResovledDate = DateTime.UtcNow.AddDays(-2)
},
new Issues
{
Status = IssueStatus.Resolved,
ResovledDate = DateTime.Now.AddDays(-4)
ResovledDate = DateTime.UtcNow.AddDays(-4)
}
};

Expand Down
16 changes: 16 additions & 0 deletions src/Ombi.Store/Context/Postgres/ExternalPostgresContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;

namespace Ombi.Store.Context.Postgres
{
public sealed class ExternalPostgresContext : ExternalContext
{
private static bool _created;
public ExternalPostgresContext(DbContextOptions<ExternalPostgresContext> options) : base(options)
{
if (_created) return;

_created = true;
Database.Migrate();
}
}
}
22 changes: 22 additions & 0 deletions src/Ombi.Store/Context/Postgres/OmbiPostgresContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore;

namespace Ombi.Store.Context.Postgres
{
public sealed class OmbiPostgresContext : OmbiContext
{
private static bool _created;

public OmbiPostgresContext(DbContextOptions<OmbiPostgresContext> options) : base(options)
{
if (_created) return;
_created = true;

Database.Migrate();
}

public override void Dispose()
{
base.Dispose();
}
}
}
17 changes: 17 additions & 0 deletions src/Ombi.Store/Context/Postgres/PostgresModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Runtime.CompilerServices;

namespace Ombi.Store.Context.Postgres;

public static class PostgresModuleInitializer
{
#pragma warning disable CA2255
// This is required to ensure that Npgsql uses a timestamp behavior that does not require a timezone
// Reference: https://stackoverflow.com/a/73586129
[ModuleInitializer]
#pragma warning restore CA2255
public static void Initialize()
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
}
}
16 changes: 16 additions & 0 deletions src/Ombi.Store/Context/Postgres/SettingsPostgresContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.EntityFrameworkCore;

namespace Ombi.Store.Context.Postgres
{
public sealed class SettingsPostgresContext : SettingsContext
{
private static bool _created;
public SettingsPostgresContext(DbContextOptions<SettingsPostgresContext> options) : base(options)
{
if (_created) return;

_created = true;
Database.Migrate();
}
}
}
8 changes: 8 additions & 0 deletions src/Ombi.Store/MigrationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ INSERT INTO AspNetRoles(Id, ConcurrencyStamp, Name, NormalizedName)
SELECT '{Guid.NewGuid()}','{Guid.NewGuid()}','{role}', '{role.ToUpper()}'
WHERE NOT EXISTS(SELECT 1 FROM AspNetRoles WHERE Name = '{role}');");
}

public static void InsertRolePostgres(this MigrationBuilder mb, string role)
{
mb.Sql($@"
INSERT INTO public.""AspNetRoles""(""Id"", ""ConcurrencyStamp"", ""Name"", ""NormalizedName"")
SELECT '{Guid.NewGuid()}','{Guid.NewGuid()}','{role}', '{role.ToUpper()}'
WHERE NOT EXISTS(SELECT 1 FROM public.""AspNetRoles"" WHERE ""Name"" = '{role}');");
}
}
}
Loading

0 comments on commit f8c6102

Please sign in to comment.