Category Archives: development

Microsoft SQL Server, version control and deployment using MSDeploy and Phantom

Managing database schema’s and keeping stored procedures up to date can become quite complex during long term development. We have ways to version control source code but how do we version control the database schema and other stuff like stored procedures?

How can we cover this in our automatic deployment setup?

The goal is to never use SQL Management Studio during development and instead rely on updating version controlled .sql files and let these make updates to the database during deployment.

This post covers how to export Microsoft SQL Server stored procedures, adding them to our solution, managing schema updates and deploying changes using Phantom with MSDeploy

1. Add to version control

First we needed to export all store procedures from our database and add them as .sql script files to our solution project. This way they can be version controlled together with our project and later be used to update any target database when we make a deploy.

  1. Right click the database
  2. Choose Tasks
  3. Generate Scripts
  4. Click Next
  5. Select specific database objects
  6. Select all stored procedures
  7. Click Next
  8. Select “Single file per object”
  9. Advanced > select “Include if NOT EXISTS”
  10. Advanced > select “Script DROP and CREATE”
  11. Advanced > deselect “Script USE DATABASE”
  12. Choose where to dump the .sql files
  13. Click Next
  14. Click Next
  15. Done!


Add the .sql files to your solution. The sync.sql is where we keep all the schema updates.

-- Example sync.sql

-- 2011-05-03 : Added author to Article
--
if not exists (select 1 from INFORMATION_SCHEMA.columns where table_name = 'Articles' and column_name = 'Author')
	alter table [Articles] add [Author] [nvarchar](255) NULL

-- 2011-03-26 : Added Date to Source
--
if not exists (select 1 from INFORMATION_SCHEMA.columns where table_name = 'Sources' and column_name = 'Date')
begin
	alter table [Sources] add [Date] [datetime] NOT NULL
        alter table [Sources] add constraint [DF_Sources_Date]  DEFAULT (getdate()) for [Date]
end

2. Modify deploy procedure

We need to add steps to our deploy process where these script files are executed towards our deploy targets. Since we are using Phantom I added something like the following to our build.boo script. The reason why I chose to combine all .sql files into one big one was because it takes a very long time to execute 50+ such files one by one using MSDeploy.

//file: build.boo

import System.IO

solution_file = "./example.sln"
configuration = "release"
output_path = "./output"
sql_file_path = "c:/tmp/bigfile.sql"

target default, (build, clean):
  pass

target build:
  msbuild(file: solution_file, configuration: configuration, properties: {"OutputPath": output_path})

target deploy, (build, deploy_codebase_to_some_machine, update_sql, clean):
  pass

target deploy_codebase_to_some_machine:
  ..deploy stuff <snip>

desc "combine all .sql files into one big one"
target build_sql_script:
	file = File.Open(sql_file_path,FileMode.Create)
	writer = StreamWriter(file)
	with FileList("./.sql"):
		.Flatten(true)
		.Include("**/*.sql")
		.ForEach def(file):
			writer.Write("\n\n")
			writer.Write(File.ReadAllText(file.FullName))
	writer.Close()

target update_sql, (build_sql_script):
	msdeploy(verb:"sync", source: {"dbFullSql":sql_file_path}, dest: {"dbFullSql":"data source=127.0.0.2;initial catalog=Hermes;password=god!secure;persist security info=True;user id=admin;packet size=4096"})

target clean:
  rm(output_path)
  rm(sql_file_path)

3. Invoke

This script can be used to perform a deploy, complete with schema updates and stored procedures by invoking:

phantom.exe deploy

- or to only update schema and stored procedures:

phantom.exe update_sql

All that is left is to add this to the build server, add some triggers and you are all set. good luck!

Ze-Chat using Websockets and GO!

I decided to do something, well different. So I ended up with this redistributable online chat based on Websockets.

The client is of course just HTML and Javascript but I decided to try and write the server software using GO

The result is a basic but totaly redistributable chat, you don’t even need to have a webserver installed. Just compile and go!

The server is initially set to listen to port 8181 as you can see above, just browse to http://localhost:8181 using any browser supporting Websockets (like Chrome and Firefox 4 Beta)

If you wanna try this you can download the zipped source below, you will need to have Go installed for either Linux or Windows depending on what you are.. running, duh

Just download the source and unzip, if you are using GNU/Linux you can build and start the server using either the build or buildx64 scripts.

Windows:

  1. Download and unzip http://code.google.com/p/gomingw/downloads/list to e.g C:/Tools/go
  2. Add C:/Tools/go/bin to your PATH
  3. Add new Environment Variable GOROOT and set it to C:/Tools/go/
  4. Add new Environment Variable GOBIN and set it to C:/Tools/go/bin
  5. Download the ze-chat source and unzip it somewhere
  6. open a new command prompt and navigate to that directory
  7. type and execute ’8g server.go’
  8. type and execute ’8l -o server.exe server.8′
  9. type and execute ‘server.exe 8181′ , replace 8181 with any other port you would like
  10. done, use either Chrome or Firefox 4 Beta, open a tab/window, type in the address “http://localhost:8181″

Visual Studio copy, paste & freeze

The problem

After I installed Visual Studio 2010 I started experiencing that the IDE would freeze for 20+ seconds after I pasted a code snipped in markup edit mode. I had the same issue on my home computer also running Visual Studio 2010.

As you may suspect this became a bit frustrating quite fast.

Fumbling in the dark

I found some MS post suggesting re-installing Visual Studio / Windows or whatever but really, who would do that, really?

Also some MS people suggested that the issue may be not enough RAM for the copy paste operation. Since I have 4GB on both computers and monitoring the memory usage during copy paste at ~2GB it did not really feel like the culprit in this case.

I did find some promising theories out there, like add an exception to your antivirus not to scan your project files – did not work.

Maybe uninstalling Microsoft Visual Studio Web Authoring Component? Seemed like a good idea in general since its just some Frontpage crap – but didn’t help much.

I did find a hotfix from Microsoft with the description (Patch Available for Cut or Copy displaying “insufficient memory” error in VS 2010) not quite my scenario but hey, they seemed related so I installed it, took a long time - did nothing.

The solution

By this time I was quite fed up with this shit, I could not get any real work done with Visual Studio behaving like this. So I started looking at warnings and stuff and did find that there was a typo in the web.config in a sectiongroup.

<configuration>
  <configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScrpingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
       ...
      </sectionGroup>
    </sectionGroup>
  </configSections>
</configuration>

The sectionGroup “scripting” had a typo in in its type attribute, “ScrpingSectionGroup” instead of “ScriptingSectionGroup”.

After I had corrected this everything worked fine.

I had no idea that a typo in web.config could screw up the IDE so bad. Keep in mind that even that web.config had this typo, the project compiled and ran just fine.

Sigh