Running MySQL and RubyOnRails on Windows

rails.jpgIf you’re trying to get RubyOnRails connected to MySQL on Windows Vista as we have done recently at Vamosa, then keep in mind that the current latest versions of MySQL and the MySQL gem are incompatible with each other.

At the time of writing, the current versions of MySQL is 5.1 and the MySQL gem is currently at version 2.7.3.

We were unable to get this combination to work together and ended up reverting to MySQL 5.0 and MySQL gem 2.7.1.

gem install -v "2.7.1" mysql

I don’t think you’re missing anything substantial from either MySQL 5.1 or the 2.7.3 gem.

Quick and dirty RubyAndRails

First of all, an admission… My name is Ijonas, I used to be a Java programmer. I’ve been clean for a year and a half. So much for my “Java Anonymous” meeting.

In that period I’ve Ruby Crystalbeen experimenting and pondering about Ruby and of course RubyOnRails. During such ponderings I’ve pondered why so many experienced Java programmers are jumping ship onto duck-typed languaged such as Ruby and Python (of which I’m a massive fan).

I’m going to write a bunch of posts about Ruby and the impact on solving certain class of computing science problems, that I believe are now more readily or more elegantly solvable – hence my interest in the Ruby language.

But first I thought I’d share with you, what I think is possibly the easiest, quickest, and dirtiest way of getting a taste of Ruby and Rails.

First of all… tooling.

Now… I wouldn’t bother buying any books until later…  I started off with books, but I find screencasts far better in conveying relevant & concise information.

With that in mind I recommend you purchase the following screencasts:

Both are from PeepCode, last about an hour, and cost $9 each. Well worth it. I recommend starting with Rails as opposed to “Pure Ruby”, because that way you get to see some of Ruby’s magic applied in the familiar setting of building a webapp. Coming from Java, it’ll be quiet a refreshing experience – no more xml deployment descriptors.

Having watched both screencasts, you can start building Rails websites with some competence. However, you’ll run into quiet a few “How do I do that ?” questions early on, such as “How do I authenicate visitors to my site?”. And before you head off to Amazon or your local bookstore to buy a Rails Cookbook… I recommend you take a look at Railscasts, which is a free (donations welcome) video podcast providing really helpful solutions to common probems.

Next… you may want to take a look at how Ruby works underneath the covers and how some of its magic manifests itself in such advancements as domain-specific languages (DSL).

Having spent many years programming Java and C#, I found that the recent advancements in test-driven development, mock objects, continuous integration are all worthy pursuits but often these advancements fought with the friction caused by stronly typed languages, i.e. all that test code I was writing was pretty verbose and clunky. Ruby’s take on the aforementioned pursuits and approaches, is positively refreshing and enjoyable.

If you’ve ever questioned why the hell you should write a unit test before the implementation itself, take a look at RSpec. RSpec is another DSL, like Rails is a DSL, this time to facilitate the writing of specification, i.e. the expected behaviour of a software programme. First you describe how your app should behave, then you write the behaviour… It takes a few moments to get your head around why you would want to do so… but then the penny drops… and once again its magical! For a great intro into RSpec I can recommend another Peepcode screencast:

Now if you’re anything like me… Java programmer or C# programmer… many years writing Enterprise applications… lots of XML… You probably know most of what there is to know about OO programming. Classes, interfaces, and object instances are your staple day-in day-out building blocks. You know about inner classes and anonymous methods but only use those if the moon is particularly blue.

Ruby’s take on “OO” borrows from many different OO languages and to fully appreciate some of the magic and how to cast your own Ruby spells, you need to look at Ruby’s object-oriented basics. To that end, I recommend downloading all five episodes (2.5 hours) of:

from the guys at Pragmantic Bookshelf, which will set you back a grant total of $25.

Consuming all this information will take some time… If you’ve got a “day job” that needs to pay the bills… you’ll probably take a week or two to really sift through and appreciate what amounts to a couple of days of screencasts.

All the screencasts I’ve mentioned are of the highest quality and provide amazing value for money… Far more so than the books I’ve purchased.

Vamosa job opening for a Java/.Net Team Leader

Hi folks,

I’m the CTO for Vamosa, a software company headquartered in Glasgow, with an rapidly expanding office in Boston (across the pond). We’re a 30-man strong company with a small development team in Glasgow that needs expansion. Our software allows clients to migrate and transform huge websites, from one content management system to another.

The software is built on both J2EE and .NET, using the usual suspects in componentry: Spring, xUnit, Hibernate, etc. etc. Woven through all of this is a big helping of both Jython and IronPython.

I’m looking for an opinionated, technology-obsessed, fun-loving, and inspiring team-leader, who knows his way around the Java and .NET spaces.

Working at Vamosa, you’ll occupy a fundemental role at Vamosa. We’re a product company, and you’ll be in charge of the products development. So we want you to bring ideas, be opinionated, tell us where we’re going wrong, but also be amazed by some of the stuff we’ve already done, because we think we pretty clever.

So if you are that person, contact me through email at ijonas.kisselbach@vamosa.com.

Cheers,
Ijonas.

P.S. Please, no agencies!

Storing URIs in SQL Server 2008 using the HIERARCHYID datatype and LINQ-To-SQL

I’ve been playing around with SQL Server 2008 CTP, exploring the benefits of the new HIERARCHYID datatype, which has been designed to efficiently store depth-first tree structures in SQL Server 2008.

My requirements were to store URI paths such as ones you might find in URLs, whereby each path component is held as a separate record in a table. Each path component refers to a folder in file system, e.g.

URL: http://www.vamosa.com/index/information_solutions/technology_and_products/vamosa_content_migrator.htm
URI: path: /index/information_solutions/technology_and_products/vamosa_content_migrator.htm

So traditionally in the database I would end up with parent/child relationship as such

id 	label					parent_id
0	NULL					NULL		<--- root element
1	index					0
2	information_solutions			1
3	technology_and_products			2
4	vamosa_content_migration.htm		3

HIERARCHYID allows you to perform all sorts of fast hiearchical queries without having to wander up and down the parent/child foreign key relationship. Those are the benefits, however there’s a problem…

At the time of writing the .NET Framework 3.5′s System.Data.SqlTypes namespace does not provide any support for HIERARCHID datatypes, e.g. SqlHierarchId as a class is missing, meaning that if you want to work with the new type you’ll need hide it from your .NET code using stored procedures, and that’s the approach I’ve taken in this post.

The other thing to be aware of is that the HIERARCHYID column is not a foreign key to a record further up the tree. The HIERARCHYID column stores position of the current record within the tree, e.g.

id  	label					hierarchyid_field.ToString()
0	NULL					/		<--- root element
1	index					/1/
2	information_solutions			/1/1/
3	technology_and_products			/1/1/1/
4	vamosa_content_migrator.htm		/1/1/1/1/

The table I’ve created is called URI and is defined as follows:

CREATE TABLE dbo.URI
(
	Id uniqueidentifier NOT NULL,
	Label nvarchar(256) NULL,
	UriHID hierarchyid NOT NULL,           -- the magical new datatype
	CONSTRAINT [PK_URI] PRIMARY KEY
)

I want to be able call my stored proc as follows:

  MyLINQDataContext db = new MyLINQDataContext(); // a previously defined context via .dbml file
  System.Guid? uri_id=null;			  // variable to capture the URI.Id of the last node inserted
  db.InsertURI("/products/vcm/index.html", ref uri_id);	// call to stored proc
  Console.Out.Writeline( uri_id );		 // doing something useful with the return result

Stored procedures aren’t my strongest point by a long stretch so feel free to comment and improve the code, but here’s the InsertURI stored proc:

CREATE Procedure InsertURI
	@uri nvarchar(4000),
	@uri_id uniqueidentifier OUTPUT
AS
	declare @root_uri_id uniqueidentifier
	declare @path_remainder nvarchar(4000)
	declare @pos_slash int
	declare @parent_uri_hid HIERARCHYID

	set @parent_uri_hid = HIERARCHYID::GetRoot()

	-- ensure the root exists
	select @root_uri_id = u.id from URI u where u.label is null
	if @root_uri_id is null
		insert into URI values (NEWID(), NULL, @parent_uri_hid)

	set @path_remainder = @uri
	set @pos_slash = charindex( '/', @path_remainder )
	while (len(@path_remainder) > 0)
	begin
		declare @next_slash int
		declare @uri_label nvarchar(256)
		declare @current_uri_hid HIERARCHYID
		set @next_slash = charindex( '/', @path_remainder, @pos_slash+1 )

		-- determine the next label in the sequence of depth-first node names
		if @next_slash > 0
		begin
			set @uri_label = SUBSTRING( @path_remainder, @pos_slash+1, @next_slash - @pos_slash - 1 )
			set @path_remainder = substring( @path_remainder, @next_slash, len(@path_remainder) )
		end
		else
		begin
			set @uri_label = SUBSTRING( @path_remainder, @pos_slash+1, len(@path_remainder) )
			set @path_remainder = ''
		end
		set @pos_slash = charindex( '/', @path_remainder )
		-- determine if current @uri_label exists as child of @parent_uri_hid
		set @current_uri_hid = NULL
		select @current_uri_hid = u.UriHID from URI u where u.UriHID.GetAncestor(1) = @parent_uri_hid and u.Label=@uri_label
		if @current_uri_hid is null
		begin
			-- the label doesn't exist as a child, hence create it
			-- first determine the new hierarchyid - new node will be last in row of siblings
			declare @last_child_uri_hid HIERARCHYID
			SELECT @last_child_uri_hid = MAX(UriHID) FROM URI u WHERE u.UriHID.GetAncestor(1) = @parent_uri_hid
			set @current_uri_hid = @parent_uri_hid.GetDescendant(@last_child_uri_hid, NULL)

			set @uri_id = NEWID()
			insert into uri values (@uri_id, @uri_label, @current_uri_hid )
		end
		set @parent_uri_hid = @current_uri_hid
	end

The stored proc takes the @uri string and chops it using the ‘/’-separator (orange highlighted), looping through each element in the path (red brown highlighted), first ‘index’ then ‘information_solutions’, etc. For each element it will check wether or not the element exists (red highlighted code), creating it if necessary (purple highlighted). The position of the new node will always be on the ‘far right’ of its siblings (green highlighted code).
 

 

Compiling ActionScript 3 Corelib compiled for Flex 3 Moxie & Adobe AIR

I’ve been playing around with Adobe AIR and Flex 3 Beta 1… I reserve opinion on these technologies because I haven’t used them enough. But I thought I’d leave a wee tip…

One thing that’s really useful to Flex 3/AIR coders is the ActionScript 3 CoreLib (AS3CoreLib). It contains MD5, JSON, and some other really useful ‘routines’.

Following are guidelines on getting AS3CoreLib compiled for Flex 3 Moxie.

Continue reading

Hoopla

As part of the Upstart Hoopla! project I’ve started programming in Ruby, specifically using RubyOnRails(RoR). RoR an interesting programming language/framwork that is obviously the darling of the industry at the moment.
Its easy to see why… RoR cuts through so much of the redtape that’s associated with frameworks such as J2EE, or even .Net. The Ruby language itself is bit tougher to get to grips with. It has some mannerism that can be currently best described as strange, compared to the stuff I’m used to, i.e. Python & Java. I say “currently” because I’m still learning these mannerisms and fromwhat I’ve read its supposed to be an amazing programming language, once the penny drosp.
Upstart Hoopla is at the time of this writing a blog using the Typo blogging engine (as is this site I might add). I’ve chosen the Typo engine as the foundation for the Upstart project because the code’s in excellent shape and is relatively easy to manipulate into the functonality required for the project.
Hence I’m forking the Typo engine into the Hoopla engine so that it may provide amongst other things:

  • multiple-user signon and registration
  • ‘related article’ functionality
  • relevance-based browsing of tags via user-profiles and a snazzy UI
  • podcast-hosting via Amazon S3

Once these features are made available and have proven to be robust, I’m considering republishing under the same open source license as the Typo engine. The other option is to get in touch with the Typo development team and suggest introducing the additional Hoopla functionality as add-ons or plugins.