Skip to content

Instantly share code, notes, and snippets.

@nelsnelson
Created March 21, 2011 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsnelson/880051 to your computer and use it in GitHub Desktop.
Save nelsnelson/880051 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
require 'rubygems'
require 'sequel'
# class TestDatabase
# def up; [nil, nil, nil]; end # (blah blah blah)
# def down; [nil, nil, nil]; end
# end
#
# class TestSetup < Sequel::Migration
# def up
# create_table! :test do
# primary_key :id
# foreign_key :parent_id, :test
# String :name, :null => false
# end unless table_exists? :test
# # Look! Here, the Test class has not yet been defined! O_o
# Test.set_dataset :test
# Test.plugin :class_table_inheritance, :key => :object_type
# end
#
# def down; drop_table :test if table_exists? :test; end
# end
# Setup schema correctly
TestDatabase.down # Placing the migration code in the main Object like this requires
TestDatabase.up # that the classes defining TestDatabase and TestSetup be defined
TestSetup.down # within the main Object namespace previous to these invocations.
TestSetup.up # This means that every time I wish to re-apply a plugin or dataset,
# it must be done _outside_ these migration helpers. This thwarts
# my intention to keep the code for the db+schema setup/tear-down in
# one reusable place, since if it remained in the TestDatabase or
# TestSetup classes (which, remember, have been defined prior to the
# Test model), the Test model class would not yet exist.
# Define models
class Test < Sequel::Model
end
# Test code
class TestSomeDatabaseRoutines
def setup
# Rebuild schema
TestDatabase.down
TestDatabase.up
TestSetup.down
TestSetup.up
end
def test_create
a = Test.create(:name => 'Hello!')
assert(a != nil and a.name == 'Hello!')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment