Frontend gaps fill work. Testing fixes work. Auditing in progress.
This commit is contained in:
@@ -160,33 +160,43 @@ public sealed class PostgresFixture : IAsyncDisposable
|
||||
await using var connection = new NpgsqlConnection(_connectionString);
|
||||
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Get all tables in the schema
|
||||
// Get all non-system tables so hard-coded schemas (e.g., vuln) are also truncated.
|
||||
await using var getTablesCmd = new NpgsqlCommand(
|
||||
"""
|
||||
SELECT table_name FROM information_schema.tables
|
||||
WHERE table_schema = @schema AND table_type = 'BASE TABLE'
|
||||
AND table_name != 'schema_migrations';
|
||||
SELECT table_schema, table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_type = 'BASE TABLE'
|
||||
AND table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||
AND table_schema NOT LIKE 'pg_%'
|
||||
AND table_name != 'schema_migrations';
|
||||
""",
|
||||
connection);
|
||||
getTablesCmd.Parameters.AddWithValue("schema", _schemaName);
|
||||
|
||||
var tables = new List<string>();
|
||||
await using (var reader = await getTablesCmd.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
tables.Add(reader.GetString(0));
|
||||
var schema = reader.GetString(0);
|
||||
var table = reader.GetString(1);
|
||||
tables.Add($"{QuoteIdentifier(schema)}.{QuoteIdentifier(table)}");
|
||||
}
|
||||
}
|
||||
|
||||
if (tables.Count == 0) return;
|
||||
|
||||
// Truncate all tables
|
||||
var truncateSql = $"TRUNCATE TABLE {string.Join(", ", tables.Select(t => $"{_schemaName}.{t}"))} CASCADE;";
|
||||
var truncateSql = $"TRUNCATE TABLE {string.Join(", ", tables)} CASCADE;";
|
||||
await using var truncateCmd = new NpgsqlCommand(truncateSql, connection);
|
||||
await truncateCmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_logger.LogDebug("Truncated {Count} tables in schema {Schema}", tables.Count, _schemaName);
|
||||
_logger.LogDebug("Truncated {Count} tables across non-system schemas", tables.Count);
|
||||
}
|
||||
|
||||
private static string QuoteIdentifier(string identifier)
|
||||
{
|
||||
var escaped = identifier.Replace("\"", "\"\"", StringComparison.Ordinal);
|
||||
return $"\"{escaped}\"";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user