Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding postgres support to ombi (beta) #5050

Merged
merged 7 commits into from
Jul 11, 2024
Merged
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
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
Loading