Getting started with LightSpeed migrations
Tagged as LightSpeedOne of the big new features of LightSpeed 3 is migrations support. We’ll be providing more detailed how-tos on this over the next few weeks, but I thought it would be useful to kick off with a quick overview of the what and how of LightSpeed migrations, starting with the question on everybody’s lips…
Just what are migrations anyway?
Let’s suppose you have an application, and it uses a database. If you’re wise, you’ll have a SQL script for setting up that database, probably consisting of a truckload of CREATE TABLE statements and maybe a few INSERTs for reference data. By running these scripts, you can easily and reliably create a new database for your application to run on.
Now let’s suppose you upgrade the application, and the upgrade requires you to store some new data in the database. Maybe a new field (column) for one kind of entity, and a couple of new kinds of entities (tables). You can update your create script, no problem… but what about existing databases?
Well, okay, instead of updating your create script, you can create an update script, which does only the new bits (an ALTER TABLE and a couple of CREATE TABLEs). For new databases, you’ll run the create script followed by the update script; for existing databases, you’ll just run the update script. No worries.
But then it happens again! (Thanks a bunch, Marketing.) Sure, you can create another update script. But now for new databases, you have to run the create script and two update scripts, but for existing databases, you might have to run both update scripts or just the second, depending on whether the original update script had already been applied. So now you need to detect what level an existing database is at before you run your scripts. Well, maybe you can do that now. But what about after the fifth upgrade? The tenth? And what if someone doesn’t want to go straight to your latest and greatest — they want the same version you installed in their Ruritanian office three years ago.
That’s where migrations come in. A migrations framework automates tracking and applying database version changes — typically schema changes but could also include data changes such as if a new column needs to be defaulted or new reference data needs to be added. You write each migration step — the create and update scripts in the story above — and the migration framework figures out which steps need to be applied in any given situation, and applies them. For example, if you tell the migration framework to upgrade the Podunk office to version 9, which is what they’ve got in the Ruritanian office, the migrations framework can figure out that Zimiamvia is currenly on version 6, and apply only steps 7, 8 and 9.
How do I write migration scripts?
You might think the obvious way to write your migration steps would be as the SQL statements to be executed for each step — the CREATE TABLE and ALTER TABLE statements mentioned above. This can work okay but is fairly labour-intensive, and gets plain vexing when multiple databases enter the picture — for example, if some of your customers use an Oracle back-end, but others use SQL Server or MySQL. Many migration frameworks, including LightSpeed’s, therefore get you to write your migration scripts using a higher-level API; the migration framework will then generate the low-level SQL scripts when you run a migration.
So in LightSpeed, the way to write a migration (by which I mean a single version-to-version step) is to create a class in C# or Visual Basic which derives from the Migration base class:
public class AddProductColumns : Migration { }
This migration doesn’t do anything yet. The actual migration logic goes in your override of the Up() method:
public override void Up() { AddColumn("Products", "Name", ModelDataType.String); AddColumn("Products", "Description", ModelDataType.String); }
Now when you run this migration, it’ll add two columns, Name and Description, to the Products table, of data type NVARCHAR(MAX) or VARCHAR2(MAX) or however your database likes to spell Big Honking String.
This sounds dangerously like work. Can’t you write the code for me?
If you’re using the LightSpeed designer, you can get it to create migrations for you. To do this, you first need to associate a migrations project with your model. This tells the designer where to create the migration class files. A migrations project is just an ordinary C# or Visual Basic class library project (it does have to be in the same solution as the project containing your model though). To associate a migrations project with a model, open the model and choose Set Migrations Project from the Migrations menu.
Once you’ve done this, you can choose Create Migration from the Migrations menu, and LightSpeed will compare your model with the previous version (the last one you created a migration for), and suggest the steps that should go in the migration. Uncheck anything that you don’t want, give the migration a name, and LightSpeed will create the migration class and implementation for you.
You’re not restricted by what the designer gives you, though. Once the migration is created, it’s just C# or Visual Basic source code, and you can edit it to your heart’s content.
How do I run migrations?
There are three ways of running migrations: from the command line, from your own programs and from the LightSpeed designer. We’ll talk about the first two options in a future post: for today, I just want to describe how to run migrations from the designer.
Again, open your model and go to the Migrations menu. Choose Run Migrations to display the migrations window.
There are a number of things you can do in this window, but I want to concentrate on a specific workflow, of applying migrations to a connected database. You can connect to a database, or choose another workflow such as generating SQL scripts for a DBA to run, via the Change button. Once you’re connected, LightSpeed will show you the current database version (if any). Click on the desired target version and choose Apply Now. And that’s it. LightSpeed figures out which migrations to apply, and applies the changes specified in your migration code for each migration that’s needed.
This has been a whistlestop tour of migration concepts and practice and we’ll have a lot more to say about this over time. If it’s been enough to whet your appetite, why not download the free Express edition and give it a go!
Leave a Reply
![]()
BrainDump (1)
Community Code (1)
Events (6)
General (31)
Lab Samples (2)
LightSpeed (132)
MegaPack (3)
News (48)
Products (64)
Projects (4)
Screencast (6)
SharePoint (1)
Silverlight (5)
Silverlight Elements (12)
SimpleDB Management Tools (11)
Visual Studio (4)
VS File Explorer (5)
WPF (31)
WPF Diagramming (14)
WPF Elements (22)
WPF Property Grid (24)
![]()
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007







Posted by Ivan Towlson on 15 December 2009


