case 4690 - erase database: check CREATEDB before dropping, so a failed create can't leave no database

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-22 20:15:13 -07:00
parent 78f5863947
commit e8e7a2ec40
2 changed files with 15 additions and 1 deletions

View File

@@ -336,6 +336,19 @@ namespace AyaNova.Util
{
await conn.OpenAsync();
//Without CREATEDB the DROP below would succeed and the CREATE after it fail, leaving no database at all
//so check first and stop with the database untouched
using (var cmdCheck = new Npgsql.NpgsqlCommand("SELECT rolcreatedb OR rolsuper FROM pg_roles WHERE rolname = current_user;", conn))
{
var canCreate = await cmdCheck.ExecuteScalarAsync();
if (!(canCreate is bool b && b))
{
var err = $"E1000 - AYANOVA_PERMANENTLY_ERASE_DATABASE is set but database user \"{_dbUserName}\" does not have permission to create databases (CREATEDB). The database \"{_dbName}\" has NOT been erased. Grant CREATEDB to this user or erase the database another way, then remove the setting";
_log.LogCritical(err);
throw new ApplicationException(err);
}
}
// Create the database desired
using (var cmd = new Npgsql.NpgsqlCommand())
{