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:
@@ -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())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user