Fix database initialization detection - check for TABLES not just schemas

This commit is contained in:
2026-03-08 11:07:16 -04:00
parent ac1c2c6c82
commit 7657caa960
4 changed files with 275 additions and 10 deletions
@@ -391,17 +391,18 @@ public sealed class PostgresDatabaseProvider : IJellyfinDatabaseProvider
wasConnectionOpened = true;
}
// Check if the library schema exists (indicates database is initialized)
var schemaCheckQuery = "SELECT COUNT(*) FROM information_schema.schemata WHERE schema_name = 'library'";
await using (var schemaCheckCommand = connection.CreateCommand())
// Check if database has tables (not just schemas)
// Query for a critical table that must exist - if it doesn't, database needs initialization
var tableCheckQuery = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'users' AND table_name = 'Users' AND table_type = 'BASE TABLE'";
await using (var tableCheckCommand = connection.CreateCommand())
{
schemaCheckCommand.CommandText = schemaCheckQuery;
var schemaCount = (long?)await schemaCheckCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
tableCheckCommand.CommandText = tableCheckQuery;
var usersTableExists = (long?)await tableCheckCommand.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
if (schemaCount == 0)
if (usersTableExists == 0)
{
// Database is empty - initialize from SQL script
logger.LogInformation("Database is empty (library schema not found). Initializing from SQL script...");
// Database tables don't exist - initialize from SQL script
logger.LogInformation("Database tables not found (users.Users table missing). Initializing from SQL script...");
var schemaScriptPath = Path.Combine(AppContext.BaseDirectory, "sql", "schema_init", "create_database_schema.sql");
@@ -440,7 +441,7 @@ public sealed class PostgresDatabaseProvider : IJellyfinDatabaseProvider
}
else
{
logger.LogInformation("Database schema exists (library schema found)");
logger.LogInformation("Database tables exist (users.Users table found)");
}
}