Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.WebPages;
namespace Whatever
{
public static class HtmlHelperExtensions
{
/// <summary>
# To run:
# 1. Copy this script somewhere and amend the variables
# 2. Open Powershell command prompt
# 3. Run: set-executionpolicy unrestricted -scope process -force
# 4. "dot-source" this script, e.g. ./rename.ps1
$old_namespace = "My.Old.Namespace"
$new_namespace = "My.New.Namespace"
$solution_folder = "C:\Users\Dan\Documents\GitHub\ExampleProject\src"
Get-PSDrive -PSProvider FileSystem | Where-Object -Property Free | %{ write-host 'Checking drive ' $_.Root; set-location $_.Root; Get-ChildItem -File -Filter *.sln -Recurse; write-host 'Checked drive ' $_.Root; } > "C:\temp\mysterious solutions.txt"
@danmalcolm
danmalcolm / blog-001-custom.sql
Last active December 10, 2015 21:48
Example of some custom code to clear data from a table with a self-referencing FK constraint
DELETE FROM ...
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Customer_Customer_MergedToCustomerId]') AND parent_object_id = OBJECT_ID(N'[dbo].[Customer]'))
ALTER TABLE [dbo].[Customer] DROP CONSTRAINT [FK_Customer_Customer_MergedToCustomerId]
DELETE FROM [dbo].[Customer]
ALTER TABLE [dbo].[Customer] WITH CHECK ADD CONSTRAINT [FK_Customer_Customer_MergedToCustomerId] FOREIGN KEY([MergedToCustomerId])
REFERENCES [dbo].[Customer] ([CustomerId])
ALTER TABLE [dbo].[Customer] CHECK CONSTRAINT [FK_Customer_Customer_MergedToCustomerId]
@danmalcolm
danmalcolm / blog-001-reset-database.cs
Last active December 10, 2015 21:48
Example of resetting database by executing a simple SQL script
const string ResetSql = @"delete from a
delete from b
...
delete from m";
// Removes any data generated by tests from the database
private void ResetDatabase()
{
InUnitOfWork(session =>
{
@danmalcolm
danmalcolm / generate-deletes.sql
Last active December 10, 2015 20:38
A SQL script that can be used to generate a script to reset data in a set of database tables. It uses foreign key meta-data to determine the order in which tables can be cleared.
-- Calculates the dependencies between database tables in a SQL Server database
-- to assist with writing a script to clear data from a set of database tables.
-- This works in SQL Server only. The information schema views follow an ISO
-- standard and this script could probably be adapted to another platform fairly easily.
declare @crlf varchar(2)
set @crlf = char(13) + char(10)
-- 1. Get the tables that we want to clear data from. We're restricting ourselves
@danmalcolm
danmalcolm / delete-order
Last active December 10, 2015 20:28
A sql script to help automate resetting data in a set of database tables. It uses foreign key meta-data to determine the order in which tables can be cleared.
-- Calculates the dependencies between database tables in a SQL Server database
-- to assist with writing a script to clear data from a set of database tables.
-- This works in SQL Server only. The information schema views follow an ISO
-- standard and this script could probably be adapted to another platform fairly easily.
-- 1. Get the tables that we want to clear data from. We're restricting ourselves
-- to a single schema here. To support multiple schemata we'd need to track
-- schema names along with table names. Is it common to have foreign key