Skip to content
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
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Version>3.0.4.0</Version>
<AssemblyVersion>3.0.4.0</AssemblyVersion>
<FileVersion>3.0.4.0</FileVersion>
<Version>3.1.0.0</Version>
<AssemblyVersion>3.1.0.0</AssemblyVersion>
<FileVersion>3.1.0.0</FileVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Jellyfin.Database.Providers.Postgres.Controllers.Models;

/// <summary>Request to apply or preview the backup retention policy.</summary>
public sealed class BackupRetentionRequest
{
/// <summary>Gets or sets the retention in days; null uses the saved value.</summary>
public int? Days { get; set; }

/// <summary>Gets or sets how many newest backups are always kept; null uses the saved value.</summary>
public int? MinKeep { get; set; }

/// <summary>Gets or sets a value indicating whether to only preview (default) instead of deleting.</summary>
public bool DryRun { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public sealed class SaveConfigRequest
/// <summary>Gets or sets a value indicating whether backups should be zipped.</summary>
public bool BackupCompression { get; set; } = true;

/// <summary>Gets or sets the number of days backups are kept (0 = keep forever).</summary>
public int? BackupRetentionDays { get; set; }

/// <summary>Gets or sets how many of the newest backups are always kept.</summary>
public int? BackupRetentionMinKeep { get; set; }

/// <summary>
/// Gets or sets the directory that contains pg_dump, psql, pg_restore, etc.
/// Leave empty to auto-detect or rely on PATH.
Expand Down
143 changes: 138 additions & 5 deletions Jellyfin.Database.Providers.Postgres/Controllers/PostgresController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Npgsql;

#pragma warning disable SA1611, SA1615

Expand All @@ -35,6 +36,7 @@
public class PostgresController : ControllerBase
{
private const string DefaultSchema = "public";
private const string PasswordMask = "*****";

private readonly IApplicationPaths _appPaths;
private readonly ISystemManager _systemManager;
Expand Down Expand Up @@ -101,13 +103,15 @@
return Ok(new
{
IsPostgresActive = isActive,
PluginVersion = typeof(PostgresPlugin).Assembly.GetName().Version?.ToString(),
InstallWarnings = PostgresPlugin.Instance?.InstallWarnings ?? Array.Empty<string>(),
ActiveConnectionString = isActive ? MaskPassword(activeConn) : null,
SqliteDefaultPath = sqliteDefault,
SqliteExists = sqliteExists,
SqliteSize = sqliteExists
? FormatBytes(new FileInfo(sqliteDefault).Length)
: null,
SavedConnectionString = config?.ConnectionString,
SavedConnectionString = MaskPassword(config?.ConnectionString),
SavedSchema = config?.Schema ?? DefaultSchema,
SavedCommandTimeout = config?.CommandTimeout ?? 60,
SavedMinPoolSize = config?.MinPoolSize ?? 4,
Expand All @@ -117,6 +121,8 @@
? defaultBackupDir
: config?.BackupDirectory ?? defaultBackupDir,
SavedBackupCompression = config?.BackupCompression ?? true,
SavedBackupRetentionDays = config?.BackupRetentionDays ?? 0,
SavedBackupRetentionMinKeep = config?.BackupRetentionMinKeep ?? BackupRetentionService.DefaultMinKeep,
SavedPgBinPath = config?.PgBinPath ?? string.Empty,
DefaultBackupDirectory = defaultBackupDir,
MigrationState = config?.MigrationState.ToString() ?? "NotStarted",
Expand Down Expand Up @@ -163,6 +169,8 @@
return BadRequest(new { Success = false, Error = "Connection string is required." });
}

request.ConnectionString = RestoreMaskedPassword(request.ConnectionString);

var error = await MaintenanceService.TestConnectionAsync(request.ConnectionString, cancellationToken)
.ConfigureAwait(false);
if (error is not null)
Expand All @@ -188,17 +196,51 @@

/// <summary>Saves the connection string and options to the plugin configuration.</summary>
/// <param name="request">Request body containing connection string and plugin options.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Object confirming the configuration was saved.</returns>
[HttpPost("SaveConfig")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<object> SaveConfig([FromBody] SaveConfigRequest request)
public async Task<ActionResult<object>> SaveConfig([FromBody] SaveConfigRequest request, CancellationToken cancellationToken)
{
var plugin = PostgresPlugin.Instance;
if (plugin is null)
{
return StatusCode(StatusCodes.Status503ServiceUnavailable, new { Error = "Plugin not loaded." });
}

if (string.IsNullOrWhiteSpace(request.ConnectionString))
{
return BadRequest(new { Success = false, Error = "La cadena de conexión es obligatoria." });
}

// The panel shows the password masked; a masked value means "keep the one already stored".
request.ConnectionString = RestoreMaskedPassword(request.ConnectionString);

try
{
_ = new NpgsqlConnectionStringBuilder(request.ConnectionString);
}
catch (ArgumentException ex)
{
return BadRequest(new { Success = false, Error = "La cadena de conexión no es válida: " + ex.Message });
}

// When PostgreSQL is the running engine the next restart reads this connection string from database.xml,
// so a typo here would leave Jellyfin unable to start. Prove it opens before anything is persisted.
var isActiveEngine = PostgresPlugin.IsPostgresActive(_appPaths);
if (isActiveEngine)
{
var tunedForTest = PostgresDatabaseProvider.BuildTunedConnectionString(
request.ConnectionString,
plugin.Configuration,
request.CommandTimeout).ToString();
var testError = await MaintenanceService.TestConnectionAsync(tunedForTest, cancellationToken).ConfigureAwait(false);
if (testError is not null)
{
return BadRequest(new { Success = false, Error = "No se guardó: la conexión falló y Jellyfin no podría arrancar con ella. " + testError });
}
}

if (request.MaxPoolSize > 0 && request.MinPoolSize > request.MaxPoolSize)
{
return BadRequest(new { Success = false, Error = "El tamaño máximo del pool no puede ser menor que el mínimo." });
Expand Down Expand Up @@ -231,6 +273,16 @@

plugin.Configuration.BackupCompression = request.BackupCompression;

if (request.BackupRetentionDays is >= 0)
{
plugin.Configuration.BackupRetentionDays = request.BackupRetentionDays.Value;
}

if (request.BackupRetentionMinKeep is >= 1)
{
plugin.Configuration.BackupRetentionMinKeep = request.BackupRetentionMinKeep.Value;
}

if (request.PgBinPath is not null)
{
plugin.Configuration.PgBinPath = request.PgBinPath;
Expand All @@ -242,7 +294,7 @@
// database.xml instead of the plugin configuration. Refresh that file so an edit in the
// advanced options reaches the provider on the next restart and does not stay as a value
// the panel shows but the runtime ignores.
if (PostgresPlugin.IsPostgresActive(_appPaths))
if (isActiveEngine)
{
var tuned = PostgresDatabaseProvider.BuildTunedConnectionString(
plugin.Configuration.ConnectionString,
Expand Down Expand Up @@ -289,6 +341,8 @@
return BadRequest(new { Error = "ConnectionString is required." });
}

request.ConnectionString = RestoreMaskedPassword(request.ConnectionString);

var started = _migrationService.StartMigration(
sqlitePath: request.SqlitePath,
postgresConnectionString: request.ConnectionString,
Expand Down Expand Up @@ -377,6 +431,8 @@
return BadRequest(new { Error = "ConnectionString is required." });
}

request.ConnectionString = RestoreMaskedPassword(request.ConnectionString);

var plugin = PostgresPlugin.Instance;

// The active connection string must carry the advanced options: Jellyfin reads it from
Expand All @@ -392,7 +448,7 @@
var connectionError = await MaintenanceService.TestConnectionAsync(activationConnStr).ConfigureAwait(false);
if (connectionError is not null)
{
_logger.LogWarning("PostgreSQL activation rejected because the connection test failed: {Error}", connectionError);

Check failure on line 451 in Jellyfin.Database.Providers.Postgres/Controllers/PostgresController.cs

View workflow job for this annotation

GitHub Actions / Analyze

Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
Logging.PostgresLog.Warn($"[ENGINE SWITCH] Activación rechazada: prueba de conexión falló: {connectionError}");
return BadRequest(new { Error = $"PostgreSQL connection test failed: {connectionError}" });
}
Expand Down Expand Up @@ -457,7 +513,7 @@
if (!TryValidateSqliteDatabase(sqlitePath, out var sqliteValidationError))
{
var message = $"SQLite validation failed; PostgreSQL remains active. {sqliteValidationError}";
_logger.LogWarning("PostgreSQL to SQLite switch rejected: {Error}", sqliteValidationError);

Check failure on line 516 in Jellyfin.Database.Providers.Postgres/Controllers/PostgresController.cs

View workflow job for this annotation

GitHub Actions / Analyze

Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
Logging.PostgresLog.Warn($"[ENGINE SWITCH] Reversión rechazada: {sqliteValidationError}");
return Conflict(new { Error = message });
}
Expand Down Expand Up @@ -766,9 +822,14 @@
.ConfigureAwait(false);

var backupSize = GetFileSizeBytes(backupPath);
var pruned = config is { BackupRetentionDays: > 0 }
? BackupRetentionService.Apply(outputDir, config.BackupRetentionDays, config.BackupRetentionMinKeep, dryRun: false)
: null;
return Ok(new
{
Success = true,
PrunedBackups = pruned?.Count ?? 0,
PrunedBytesFormatted = pruned?.BytesFormatted,
BackupPath = backupPath,
Compressed = compress,
SizeBytes = backupSize,
Expand Down Expand Up @@ -817,6 +878,30 @@
});
}

/// <summary>
/// Applies the backup retention policy to the backup folder, or previews it.
/// </summary>
/// <param name="request">Optional overrides and the dry-run flag.</param>
/// <returns>The backups removed (or that would be removed).</returns>
[HttpPost("BackupRetention")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<object> BackupRetention([FromBody] BackupRetentionRequest? request)
{
var config = PostgresPlugin.Instance?.Configuration;
var days = request?.Days ?? config?.BackupRetentionDays ?? 0;
var minKeep = request?.MinKeep ?? config?.BackupRetentionMinKeep ?? BackupRetentionService.DefaultMinKeep;
if (days <= 0)
{
return BadRequest(new { Success = false, Error = "Define primero cuántos días se conservan los respaldos (mayor que 0)." });
}

var dir = string.IsNullOrWhiteSpace(config?.BackupDirectory)
? Path.Combine(_appPaths.DataPath, "postgres-backups")
: config.BackupDirectory;
var result = BackupRetentionService.Apply(dir, days, minKeep, request?.DryRun ?? true);
return Ok(new { Success = true, result.DryRun, result.Count, result.Bytes, result.BytesFormatted, result.Names, result.Failures, Days = days, MinKeep = minKeep });
}

/// <summary>Restores a PostgreSQL backup file (.sql or .zip) using psql.</summary>
/// <param name="request">Request body with backup file path and restore options.</param>
/// <param name="cancellationToken">Cancellation token.</param>
Expand Down Expand Up @@ -1016,7 +1101,7 @@
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>Array of ranked search result objects.</returns>
[HttpGet("Search/Instant")]
[AllowAnonymous] // Search must work without re-auth; access is scoped to the DB user's data
[Authorize] // Needs a signed-in Jellyfin user: the query reads the whole BaseItems table, so it must never be anonymous.
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
public async Task<ActionResult<object>> InstantSearch(
Expand Down Expand Up @@ -1383,6 +1468,54 @@
StatusCodes.Status409Conflict,
new { Error = "PostgreSQL is not active. Activate it first or provide a connection string." });

/// <summary>
/// The panel never receives a real password, so what it sends back may carry the mask. Swaps the mask for
/// the password already stored (plugin configuration first, then the active database.xml).
/// </summary>
/// <param name="connectionString">Connection string received from the panel.</param>
/// <returns>The connection string with the real password, or the original when there is nothing to restore.</returns>
private string RestoreMaskedPassword(string connectionString)
{
NpgsqlConnectionStringBuilder builder;
try
{
builder = new NpgsqlConnectionStringBuilder(connectionString);
}
catch (ArgumentException)
{
return connectionString;
}

if (!string.Equals(builder.Password, PasswordMask, StringComparison.Ordinal))
{
return connectionString;
}

foreach (var stored in new[] { PostgresPlugin.Instance?.Configuration.ConnectionString, PostgresPlugin.ReadActivePgConnectionString(_appPaths) })
{
if (string.IsNullOrWhiteSpace(stored))
{
continue;
}

try
{
var password = new NpgsqlConnectionStringBuilder(stored).Password;
if (!string.IsNullOrEmpty(password) && !string.Equals(password, PasswordMask, StringComparison.Ordinal))
{
builder.Password = password;
return builder.ConnectionString;
}
}
catch (ArgumentException)
{
// Unreadable stored value: try the next source.
}
}

return connectionString;
}

/// <summary>Masks the password in a connection string so it's safe to return to the UI.</summary>
private static string? MaskPassword(string? connectionString)
{
Expand All @@ -1395,7 +1528,7 @@
return System.Text.RegularExpressions.Regex.Replace(
connectionString,
@"(?i)(Password\s*=)[^;]+",
"$1*****");
"$1" + PasswordMask);
}

// backupPath is built internally from a trusted output directory + timestamp — not user input (CA3003).
Expand Down Expand Up @@ -1457,7 +1590,7 @@
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to create SQLite schema at {Path}", sqlitePath);

Check failure on line 1593 in Jellyfin.Database.Providers.Postgres/Controllers/PostgresController.cs

View workflow job for this annotation

GitHub Actions / Analyze

Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@
// cadena de conexion por tercera vez.
if (Interlocked.CompareExchange(ref _pipelineLogged, 1, 0) == 0)
{
Logging.PostgresLog.Info("[Provider] Inicializado: pipeline de EF Core en marcha.");
var assembly = typeof(PostgresDatabaseProvider).Assembly;
Logging.PostgresLog.Info(
$"[Provider] Inicializado: pipeline de EF Core en marcha. Ensamblado {assembly.GetName().Version} " +
$"mvid={assembly.ManifestModule.ModuleVersionId:N} ruta={assembly.Location}");
Logging.PostgresLog.Info(
$"[Provider] Pool: min={csb.MinPoolSize} max={csb.MaxPoolSize} " +
$"maxAutoPrepare={csb.MaxAutoPrepare} cmdTimeout={csb.CommandTimeout}s");
Expand All @@ -180,16 +183,7 @@
EfCoreDatabaseDiagnostics.Write,
(eventId, level) => level >= LogLevel.Information
&& !(eventId.Name?.Contains("Command", StringComparison.Ordinal) ?? false))
.AddInterceptors(
new Jellyfin121MigrationInterceptor(),
// This must precede HomeQueryCacheInterceptor: the cache buffers a miss by executing
// the command itself, before subsequent command interceptors get a chance to run.
new DateTimeKindNormalizingInterceptor(),
new PostgresCaseInsensitiveSearchInterceptor(),
new HomeQueryCacheInterceptor(_logger),
new UpsertConflictInterceptor(),
new ItemValueReuseInterceptor(),
new DbErrorLoggingInterceptor());
.AddInterceptors(BuildInterceptors());

// SQLite-compatibility objects (min/max over uuid) have to exist before Jellyfin runs its
// first query, otherwise home-page queries fail with 'no existe la función min(uuid)'.
Expand Down Expand Up @@ -428,6 +422,35 @@
}
}

private IInterceptor[] BuildInterceptors()
{
var interceptors = new List<IInterceptor>
{
new Jellyfin121MigrationInterceptor(),

// This must precede HomeQueryCacheInterceptor: the cache buffers a miss by executing
// the command itself, before subsequent command interceptors get a chance to run.
new DateTimeKindNormalizingInterceptor(),
new PostgresCaseInsensitiveSearchInterceptor(),
};

// Diagnostic switch: lets an operator rule the query cache out when chasing a data problem.
if (string.Equals(Environment.GetEnvironmentVariable("JELLYFIN_PG_DISABLE_QUERY_CACHE"), "1", StringComparison.Ordinal))
{
Logging.PostgresLog.Warn("[Provider] JELLYFIN_PG_DISABLE_QUERY_CACHE=1: la caché de consultas está desactivada.");
}
else
{
interceptors.Add(new HomeQueryCacheInterceptor(_logger));
interceptors.Add(new HomeQueryCacheCommitInterceptor());
}

interceptors.Add(new UpsertConflictInterceptor());
interceptors.Add(new ItemValueReuseInterceptor());
interceptors.Add(new DbErrorLoggingInterceptor());
return interceptors.ToArray();
}

/// <inheritdoc/>
public void OnModelCreating(ModelBuilder modelBuilder)
{
Expand All @@ -436,6 +459,9 @@
/// <inheritdoc/>
public void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// Todas las columnas de fecha son timestamptz: se lee siempre como UTC para que Jellyfin escriba la 'Z'.
configurationBuilder.Properties<DateTime>().HaveConversion<UtcDateTimeConverter>();
configurationBuilder.Properties<DateTime?>().HaveConversion<UtcDateTimeConverter>();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -679,7 +705,7 @@
}
catch (PostgresException pgEx) when (pgEx.SqlState is "42501" or "0A000" or "58P01")
{
logger.LogWarning(

Check failure on line 708 in Jellyfin.Database.Providers.Postgres/Data/PostgresDatabaseProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze

Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
"pg_trgm could not be enabled automatically ({Reason}). InstantSearch will use ILIKE.",
pgEx.MessageText);
return false;
Expand Down Expand Up @@ -741,7 +767,7 @@
}
catch (Exception ex)
{
logger.LogWarning(ex, "Could not apply autovacuum tuning for {Table}.", table);

Check failure on line 770 in Jellyfin.Database.Providers.Postgres/Data/PostgresDatabaseProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze

Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
}
}
}
Expand Down Expand Up @@ -771,7 +797,7 @@
catch (PostgresException pgEx) when (pgEx.SqlState == "40P01" && attempt < RetryDelays.Length)
{
var delay = RetryDelays[attempt];
logger.LogWarning(

Check failure on line 800 in Jellyfin.Database.Providers.Postgres/Data/PostgresDatabaseProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze

Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
pgEx,
"Deadlock creating {IndexName} (attempt {Attempt}). Retrying in {Delay}s.",
indexName,
Expand All @@ -781,7 +807,7 @@
}
catch (Exception ex)
{
logger.LogWarning(ex, "Could not create index {IndexName}. Skipping.", indexName);

Check failure on line 810 in Jellyfin.Database.Providers.Postgres/Data/PostgresDatabaseProvider.cs

View workflow job for this annotation

GitHub Actions / Analyze

Evaluation of this argument may be expensive and unnecessary if logging is disabled (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1873)
return;
}
}
Expand Down
11 changes: 11 additions & 0 deletions Jellyfin.Database.Providers.Postgres/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ public class PluginConfiguration : BasePluginConfiguration
/// </summary>
public bool BackupCompression { get; set; } = true;

/// <summary>
/// Gets or sets how many days a backup is kept. Older backups are deleted after each backup and by the
/// retention task. 0 disables retention (nothing is ever deleted).
/// </summary>
public int BackupRetentionDays { get; set; }

/// <summary>
/// Gets or sets how many of the newest backups are never deleted, whatever their age.
/// </summary>
public int BackupRetentionMinKeep { get; set; } = 5;

/// <summary>
/// Gets or sets the directory that contains the PostgreSQL client binaries
/// (pg_dump, psql, pg_restore, etc.).
Expand Down
Loading
Loading