# Saturday, February 09, 2008

I thought this looked like an interesting approach to validating your SQL statements in a continuous integration environment.  Basically you execute the script with the NOEXEC option.

I'm not sure I would actually us it though since I think I would prefer to actually run the scripts and and then do some further processing, however maybe this is a good prebuild step?

Saturday, February 09, 2008 5:39:37 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [3]  | 
# Sunday, February 03, 2008

I'm working on creating a SQL migration tool to help automate some of the drudgery in moving and upgrading databases.  Initially I'm only supporting SQL Server 2000 since this is what I primarily use.

The SQL scripts are being generated from the SQL entities (sort of like a generic SMO) via NVelocity templates.  I think the NVelocity approach is working very well because the C# code only needs to populate the top most object, like Table or Column, and the template can walk the hierarchy db objects as needed.

Right now I have Create/Drop table, Add/Drop/Alter column, Create/Drop index, Create/Drop foreign key complete.  This should be enough to modify a table for the most common scenarios.  Currently the committed code only has SQL script generation via the scripting API, so there's no intelligence currently backing it in regards to migration.  It only will write SQL scripts at this point.

The one thing I'm making sure to do, is to bulletproof the generated scripts so they can safely be run multiple times without error.  Here's an example template which safely adds a column to a table.

/****** Object:  Column [$column.Table.Schema].[$column.Table.Name].[$column.Name] $time ******/

IF NOT EXISTS
(
  SELECT * FROM [information_schema].[columns]
  WHERE table_name = '$column.Table.Name'
  AND table_schema = '$column.Table.Schema'
  AND column_name = '$column.Name'
)
BEGIN
  ALTER TABLE [$column.Table.Schema].[$column.Table.Name]
  ADD $SqlScriptHelper.WriteColumn($column)
END
GO

 

I chose to use NVelocity templates because of their flexibility and the ease of use for generating text documents.  NVelocity is also faster for script generation then SMO (and it actually works unlike SMO), and it gives me the flexibility to do things the non-SQL Server way which sometimes is inappropriate for production systems (change a column's nullability and you'll see what I mean).  One more benefit, is it allows me to customize the output just by modifying the template, no recompile necessary.

Keeping my SQL generation separate from SQL Server will also allow me to re-use this migration tool for other databases like MySql.  It could even potentially allow me to export scripts from SQL Server to MySql syntax since I have an abstraction layer over the database like an ORM would.

Sunday, February 03, 2008 8:10:27 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Saturday, February 02, 2008

What would the minimum set of features be to create a SQL Schema Migration tool?

Add FK Contraints
Remove FK Contraints

Add Non-Clustered Indexes
Remove Non-Clustered Indexes

Add Tables
Remove Tables

Add Columns
Remove Columns

Add Sprocs
Remove Sprocs
Alter Sprocs

Add Views
Remove Views
Alter Views

LHS is a dependency of the RHS

Add dependency order (removal dependency order is the opposite):
Tables -> Columns -> FKs -> Views -> Sprocs

Are there other common scenarios I'm not think about?  Like changing the nullability of a column or adding/removing a default?

Saturday, February 02, 2008 11:03:23 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Wednesday, January 23, 2008

Over the past few months on the RhinoMocks group I have seen several good ideas for enhancements to the library.  One of the ideas was to add a DoNotExpect.Call helper, which would do the same thing as the LastCall.Repeat.Never() syntax, just in a much more concise way.

This past weekend I added the DoNotExpect.Call() syntax to RhinoMocks, and Ayende has already applied the patch to the RhinoMocks trunk.  Here's how you use the new syntax:

IService svc = mocks.DynamicMock<IService>();

DoNotExpect.Call(svc.Load("Sneal"));

DoNotExpect.Call(delegate { svc.Save("Sneal"); });

 

There are two overloads for the method, one takes an object, and the other a delegate.  For methods that return a value you can use the simpler one without the delegate, however for void methods you need to use the delegate syntax (since something has to be passed in, even if it is technically ignored).

I'm excited about the syntax since I know I will use it quite often on my controller tests that use a lot of dynamically mocked objects.  The syntax I think is also easier to understand, especially for RhinoMocks newbies who have used NMock2 before.

Hopefully I'll have time to add some other enhancements to RhinoMocks in the near future.  It feels good to give back to the software ecosystem every once in a while.

Wednesday, January 23, 2008 6:16:24 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 

I just added another tool to my PC tool belt today thanks to Jeff Atwood.  This got me thinking about all the tools I use for development and just general day to day use of my PC.  Here's a list of one's that I use pretty often.

Development specific tools:

  • Consolas Font Family for Visual Studio 2005/2008 - Makes a nice legible Visual Studio font
  • Fiddler2 - Good for debugging anything over HTTP
  • GhostDoc2 - Quick start my class and method documentation - intelligently.
  • HM NIS Edit - IDE for NSIS installers.
  • IE Developer ToolBar - Firebug light...
  • ReSharper 4 - VS Refactoring tool.
  • MBUnit - xUnit tools for .NET
  • NUnit - xUnit tools for .NET
  • Firebug - Firefox plugin for debugging JavaScript, CSS, and HTML.  Absolutely essential for any web dev.
  • YSlow - Yahoo's web performance analysis tool.
  • WatiN - I use this to drive tests through IE.  Its pretty easy to setup and use.  The newest version supports FireFox!
  • Notepad++ - Opens as fast as Notepad, but has tabs and syntax highlighting.  It comes with a nice installer which adds it to the IE view source context menu.
  • SharpDevelop - Used for editing Boo/Brail.  I think I also used this when I was developing Mono applications on Ubuntu.
  • Subversion - This is what I use for source control at work and at home.
  • Tortoise SVN - Explorer plugin for Subversion.
  • Visual SVN - Non-intrusive Visual Studio plugin for Subversion that just works.
  • Visual Studio 2005 - My C#/C++ IDE.
  • Visual Studio 2005 SP1 - You gotta have web projects.
  • Visual Studio 2005 Extensions for .NET 3 - Use this mainly for WCF.
  • CSAH - Allows you to cut and paste as HTML from Visual Studio.  Essential for blog posts.
  • Syntax Highlighter for WLW - This one works well with every programming language I use.
  • SQL Server 2005 Dev Edition - Primarily the DB I use.
  • SQL Server CE - Sometimes use this for unit tests with NHibernate.
  • MySQL - Started using this recently, the installer for Windows makes it dumb easy to setup.
  • Java SDK 1.6 - uh yeah.
  • Eclipse - Sometimes need this for Java (and C++ programming on Linux).
  • ActivePerl - Our C++ xUnit test framework requires this.  There's a free version if you click the right link.
  • CxxTest -C++ xUnit test framework.
  • RhinoMocks - Fluent mock object framework for .NET.
  • PowerShell - A powerful Windows command shell.
  • CruiseControl.NET (CCTray) - Keep the team in touch with the build.
  • WinMerge - Nice merge/diff tool especially considering that its free like beer.
  • TestDriven.NET - Since I'm now force to use MSTest, this an essential tool to keep the TDD rythm.
  • FxCop - Nice to run every once in a while... to catch stuff, especially related to globalization.

Non-Development specific tools:

  • Virtual PC 2007 - Use this run virtual PCs, generally for integration testing.
  • Miranda IM - I get tired of having multiple IM clients, this allows me to use them all at once (and it's OSS).
  • Pidgin - Multi-function IM client which just works.  Much better than Miranda.
  • Firefox3 - My preferred browser, which runs JavaScript heavy sites fast.
  • 7-Zip - An awesome zip/tar/rar/whatever utility.
  • FoxIt PDF Reader - A light and fast version of Adobe Reader that doesn't constantly nag me about updates.
  • ClipX - My newest install, this one allows you to maintain a clipboard history.  I'm loving this one.
  • DisplayFusion - I generally just use this to drag maximized windows between monitors.
  • Windows Live Writer - What I write this blog on.
  • Paint.NET - A powerful .NET paint and photo editor package.  I've dumped my old copy of Photoshop 7 for this.
  • Launchy - Since I'm stuck on XP, this makes finding and starting programs uber quick.

The one overriding theme of all these packages is that most of them are free and/or OSS.  Pretty cool.

Wednesday, January 23, 2008 2:17:41 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 
# Tuesday, January 22, 2008

While looking for information on multi-threaded debugging for Visual Studio 2005, I found this article by Peter Bromberg that has some decent Visual Studio 2005 debugger tips.  Its definitely worth reading over if you use Visual Studio 2005.  I know I learned a thing or two.

One tip I would add to this article is to use the immediate window when debugging.  It seems like all VB'ers know about this one, but relatively few C# developers do. 

The immediate window allows you to evaluate code while debugging.  Lets say I wanted to print out the value of a property in the immediate window, I could do any these expressions:

ImmediateWindow

I find this is often faster than trying to dig through a watch window, especially on a large class.

You can even call code directly from the immediate window, which I think is where it's real power lies.  With this you can create a generic debug method in your code that prints out all the values of a collection, which can then be called from the immediate window passing in any collection.

internal static void PrintAll(IEnumerable coll)
{
    foreach (object item in coll)
        Debug.Print(item.ToString());
}
Tuesday, January 22, 2008 5:17:53 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Sunday, January 20, 2008

I needed to get directions to a friends house today and sure enough I found myself using Google Maps to get directions.  Nothing astounding about that.  What did astonish, and delight me, was the fact that I found you can change your route just my dragging and dropping the route line displayed on the map!  Is this thing really a web application running on just HTML and JavaScript?

Even more amazing is that you can create hyperlink to send out to people using your new preferred route just by clicking "Link to this page."  I assumed it would lose my customized route, but it doesn't, it keeps it in the URL by specifying a direction change at an intersection.

From now on when sending directions or even my address I'm going to make sure to send it as a hyperlink to Google Maps.  It just makes sense.

Sunday, January 20, 2008 7:21:33 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, January 17, 2008

Setting up Monorail to work on IIS7 isn't necessarily straight forward.  First you need to configure IIS7 to handle the particular file extension you are using for Monorail.  This is just like you would do in IIS6, but with some slightly different menus and terminology.

I'm using .rails for this particular application.  For the website or virtual directory you are configuring in IIS7, open the Handler Mappings in IIS Manager.

HandlerMappings

From there create a new Script Map, by clicking "Add Script Map..."  This opens the Script Map dialog.  You should enter *.rails as the request path and the aspnet_isapi.dll path as the executable.

EditScriptMap

Once that is done, click "Request Restrictions" and then uncheck "Invoke Handler only if request is mapped to".

RequestRestrictions

Once that is done you may run into a problem with your monorail section in the web.config.  I had this error:

An error occurred creating the configuration section handler for monoRail: Object reference not set to an instance of an object.

 

Odd, my web.config works in IIS6 and in webdev.webserver.exe.  As it turned out I had to fill out the assembly element in my MonoRail section even though I'm using Windsor integration.  I've bolded the part I had to add below:

 

<monorail useWindsorIntegration="true">
  <controllers>
    <assembly>Sneal.Store.Commerce</assembly>
  </controllers>
  <viewEngines viewPathRoot="Views">
    <add type="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />
  </viewEngines>
</monorail>
Thursday, January 17, 2008 3:44:58 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [1]  | 

We all know that since Internet Explorer is integrated into the OS that you can't have two different versions of IE on one PC.  I have Vista on all my PCs, and there's no way to run IE6 on Vista.  What is a developer to do?  We could create Virtual PC running Windows XP with IE6, but why bother when the IE team has already created a freely downloadable IE6 Virtual PC image for you?  This is a lot easier and faster then going through the Windows setup wizard.

They have an IE7 image too.

Thursday, January 17, 2008 2:31:02 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 
# Monday, January 14, 2008

The footer now appears at the bottom of the blog in IE (IE 7 anyway).

The content area is floating so that the blog now takes full advantage of your wide screen monitor.  It certainly makes this blog a lot easier to read in 1600x1050.

Monday, January 14, 2008 7:45:02 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  | 

I posted last week about testing JavaScript through NUnit with JSUnit.  While this worked, it was a little slower to run tests than I really wanted.  It was also slower (for me) to write the tests since I had to leave the comfort of NUnit and .NET for JSUnit and JavaScript.

I'm continuing to look at the problem of JS testing for ASP.NET.  I evaluated YUI Test, Yahoo's JS unit test library.   YUI Test actually looks very good, but I really wanted to keep things integrated with my current build and test process, even if it isn't the best solution; you don't know unless you try.  ;-)

This time I'm only using NUnit and WatiN for my JavaScript tests.  Since WatiN now has the ability to evaluate JavaScript and return the results, I'm using this to run the JavaScript function under test and then using an NUnit assertion for validation.

I create each test using regular NUnit syntax, and then wrap the JavaScript function call with a C# method which passes the arguments off to the JS function to test.

[Test]
public void ShouldValidateDiscover()
{
    Assert.IsTrue(ValidateCreditCard("6011648040903965", "discover"));
}
 
private bool ValidateCreditCard(string cc, string ccType)
{
    string testJs = string.Format("var cc = new CreditCardValidator(); cc.isValidCreditCardNumber('{0}', '{1}');", cc, ccType);
    return EvalToBool(testJs);
}

The EvalToBool method just calls the WatiN eval method which accepts JavaScript as a parameter, and then converts the JS string result to a .NET boolean.

protected bool EvalToBool(string js)
{
    string result = ie.Eval(js);
    return bool.Parse(result);
}

The overall process is:

  1. Create a dummy HTML page which includes the JavaScript source file I want to test.
  2. Kick off WatiN and navigate to the dynamically created HTML file.
  3. Pass the JavaScript function to be evaluated by WatiN.
  4. Assert the eval call results using NUnit.

The test setup is very simple, and it runs fast for this type of test.  On my PC it was taking 1.7 seconds to setup the test fixture, and then .2 seconds to run each test.  The nice thing is that the errors are generally obvious and easy to diagnose since they are native NUnit tests.  Its also nice to have multiple NUnit test entries in the runner instead of one monolithic JavaScript test function which encapsulated all my JS tests that run in JSUnit. 

The tests can get ugly if a JavaScript exception is thrown, in that case you get a generic COM exception returned from WatiN rather than a nice error message.  The is one area where a JSUnit or YUI test are naturally better at.

This makes me wonder if WatiN couldn't be extended to return better error information from JavaScript, and whether the use of a dynamic .NET language along with NUnit couldn't make for a nice testing experience.

Monday, January 14, 2008 6:44:21 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |