Skip to content

Instantly share code, notes, and snippets.

@jschementi
Created January 10, 2010 10:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschementi/273430 to your computer and use it in GitHub Desktop.
Save jschementi/273430 to your computer and use it in GitHub Desktop.
IronRuby and ActiveRecord using activerecord-sqlserver-adapter and ironruby-dbi
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.string :first_name
t.string :last_name
t.integer :age
t.timestamps
end
end
def self.down
drop_table :people
end
end

IronRuby and ActiveRecord setup

Install SQL Server Express

Create the "ironrubytest1" database in SQLServer Express

Download IronRuby:

Extract to a no-space path; C:\IronRuby is preferred. Also add C:\IronRuby\bin to the path: set PATH=%PATH%;c:\IronRuby\bin

Install ActiveRecord:

igem update --system
igem install activerecord --no-rdoc --no-ri

Download and extract to following archives into the given directory (all siblings of each other):

Run the IronRuby ActiveRecord test:

cd path\to\ir-ar-test
ir.exe ir-ar-test.rb

Which outputs this:

loading rubygems ...
loading activerecord ...
creating logger ...
adjusting path for ironruby-dbi and sqlserver-adapter ...
loading sqlserver-adapter ...
patching sqlserver-adapter for ADO integrated security ...
Establish connection to sqlserver ...
Migrating ...
==  CreatePeople: migrating ===================================================
-- create_table(:people)
   -> 0.0320s
==  CreatePeople: migrated (0.0470s) ==========================================

Defining models ...
Loaded suite TestCRUD
Started
....
Finished in 1.255072 seconds.

4 tests, 12 assertions, 0 failures, 0 errors
###
### CONFIGURATION
###
IRONRUBY_DBI_PATH = "#{File.dirname(__FILE__)}/../ironruby-dbi"
SQLSERVER_ADAPTER_PATH = "#{File.dirname(__FILE__)}/../sqlserver-adapter"
SQLSERVER_ADAPTER_PATCH_PATH = "#{File.dirname(__FILE__)}/../sqlserver-adapter-patch"
###
###
###
puts 'loading rubygems ...'
require 'rubygems'
puts 'loading activerecord ...'
gem 'activerecord', '=2.3.5'
require 'active_record'
puts 'creating logger ...'
require 'logger'
ActiveRecord::Base.logger = Logger.new("ir-ar-test.log")
puts 'adjusting path for ironruby-dbi and sqlserver-adapter ...'
$:.unshift "#{IRONRUBY_DBI_PATH}/src/lib"
$:.unshift "#{SQLSERVER_ADAPTER_PATH}/lib"
puts 'loading sqlserver-adapter ...'
require 'activerecord-sqlserver-adapter'
puts 'patching sqlserver-adapter for ADO integrated security ...'
require "#{SQLSERVER_ADAPTER_PATCH_PATH}/ironruby-sqlserver-patch"
puts 'Establish connection to sqlserver ...'
ActiveRecord::Base.establish_connection(
:mode => 'ADONET',
:adapter => 'sqlserver',
:host => ENV['COMPUTERNAME'] + "\\SQLEXPRESS",
:integrated_security => 'true',
:database => 'ironrubytest1'
)
puts 'Migrating ...'
ActiveRecord::Migrator.rollback 'migrations'
ActiveRecord::Migrator.migrate 'migrations'
puts 'Defining models ...'
class Person < ActiveRecord::Base
end
if __FILE__ == $0
require 'test/unit'
DATA = [
{:first_name => "Jimmy", :last_name => "Schementi", :age => '25'},
{:first_name => "Felicia", :last_name => "Cutrone", :age => '23'}
] unless defined?(DATA)
class TestCRUD < Test::Unit::TestCase
def setup
@p1 = Person.create DATA[0]
@p2 = Person.create DATA[1]
end
def teardown
@p1.destroy
@p2.destroy
end
def test_create
assert_equal DATA[0][:first_name], @p1.first_name
assert_equal DATA[0][:last_name], @p1.last_name
assert_equal DATA[0][:age].to_i, @p1.age
assert_equal DATA[1][:first_name], @p2.first_name
assert_equal DATA[1][:last_name], @p2.last_name
assert_equal DATA[1][:age].to_i, @p2.age
end
def test_read
assert_equal 2, Person.all.size
assert_equal @p1, Person.all[0]
assert_equal @p2, Person.all[1]
end
def test_update
Person.find_by_first_name(@p2.first_name).update_attributes :last_name => 'Schementi'
assert_equal "Schementi", Person.find_by_first_name(@p2.first_name).last_name
end
def test_delete
Person.find_by_first_name(@p1.first_name).destroy
assert_equal 1, Person.all.size
assert_equal @p2, Person.all[0]
end
end
require 'test/unit/ui/console/testrunner'
Test::Unit::UI::Console::TestRunner.run(TestCRUD)
if ARGV[0] == '--irb'
ARGV.shift
puts 'Starting irb ...'
require 'irb'
IRB.start
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment