public async Task DeleteAllContentAsync() { _logger.LogInformation("Wiping all database tables and media files..."); var threadIdList = await _context.Threads.Select(thread => thread.Id).ToListAsync(); _logger.LogDebug($"Deleting content files (media) from {threadIdList.Count} threads"); foreach (var threadId in threadIdList) { _logger.LogDebug($"Processing of {threadId} container..."); try { await _storageProvider.DeleteContainerAsync(threadId.ToString()); } catch (Exception ex) { _logger.LogError(ex, $"{nameof(DeleteAllContentAsync)} error"); } _logger.LogDebug("OK"); _logger.LogDebug($"Processing of {threadId + Defaults.ThumbnailPostfix} container..."); try { await _storageProvider.DeleteContainerAsync(threadId + Defaults.ThumbnailPostfix); } catch (Exception ex) { _logger.LogError(ex, $"{nameof(DeleteAllContentAsync)} error"); } _logger.LogDebug("OK"); } _logger.LogDebug("Wiping database tables..."); await WipeDatabase(); _logger.LogDebug("OK"); _logger.LogDebug("Running migrations and seed..."); await RunSeedInNewScopeAsync(); _logger.LogDebug("OK"); }
public async Task DeleteAllContentAsync() { _logger.LogInformation("Wiping all database tables and media files..."); var threadIdList = await _context.Threads.Select(thread => thread.Id).ToListAsync(); _logger.LogDebug($"Deleting content files (media) from {threadIdList.Count} threads"); foreach (var threadId in threadIdList) { _logger.LogDebug($"Processing of {threadId} container..."); try { await _storageProvider.DeleteContainerAsync(threadId.ToString()); } catch (Exception ex) { _logger.LogError(ex.ToString()); } _logger.LogDebug("OK"); _logger.LogDebug($"Processing of {threadId + Defaults.ThumbnailPostfix} container..."); try { await _storageProvider.DeleteContainerAsync(threadId + Defaults.ThumbnailPostfix); } catch (Exception ex) { _logger.LogError(ex.ToString()); } _logger.LogDebug("OK"); } _logger.LogDebug("Deleting all database tables..."); await _context.Database.ExecuteSqlCommandAsync( @" DECLARE @sql NVARCHAR(2000); WHILE EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE (CONSTRAINT_TYPE = 'FOREIGN KEY') AND (TABLE_NAME IN ( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_TYPE='BASE TABLE') AND (TABLE_NAME NOT LIKE 'sys.%') )) ) BEGIN SELECT TOP 1 @sql = 'ALTER TABLE '+TABLE_SCHEMA+'.['+TABLE_NAME+'] DROP CONSTRAINT ['+CONSTRAINT_NAME+']' FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE (CONSTRAINT_TYPE = 'FOREIGN KEY') AND (TABLE_NAME IN ( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_TYPE='BASE TABLE') AND (TABLE_NAME NOT LIKE 'sys.%') )) EXEC (@sql); END; WHILE EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_TYPE='BASE TABLE') AND (TABLE_NAME NOT LIKE 'sys.%') ) BEGIN SELECT TOP 1 @sql = 'DROP TABLE '+TABLE_SCHEMA+'.['+TABLE_NAME+']' FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_TYPE='BASE TABLE') AND (TABLE_NAME NOT LIKE 'sys.%'); EXEC (@sql); END; "); _logger.LogDebug("OK"); _logger.LogDebug("Running migrations and seed..."); await _context.Database.MigrateAsync(); _logger.LogDebug("OK"); }