Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:sam/dm-more
Browse files Browse the repository at this point in the history
  • Loading branch information
david committed Apr 9, 2008
2 parents a8224d9 + aeccc4b commit 74e2601
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 84 deletions.
3 changes: 3 additions & 0 deletions dm-migrations/examples/sample_migration.rb
@@ -1,10 +1,13 @@
require File.dirname(__FILE__) + '/../lib/migration_runner'

DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/migration_test.db")
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.logger.debug( "Starting Migration" )

migration 1, :create_people_table do
up do
create_table :people do
column :id, :int, :primary_key => true
column :name, :string
column :age, :int
end
Expand Down
30 changes: 25 additions & 5 deletions dm-migrations/examples/sample_migration_spec.rb
@@ -1,14 +1,34 @@
require File.dirname(__FILE__) + '/sample_migration'
require File.dirname(__FILE__) + '/../lib/spec/example/migration_example_group.rb'
require File.dirname(__FILE__) + '/../lib/spec/example/migration_example_group'

describe :create_people_table, :type => :migration do

before(:all) do
puts "Inserts here"
before do
run_migration
end

it 'should do something' do
puts "hi"
it 'should create a people table' do
repository(:default).should have_table(:people)
end

it 'should have an id column as the primary key' do
table(:people).should have_column(:id)
table(:people).column(:id).type.should == 'int'
table(:people).column(:id).should be_primary_key
end

it 'should have a name column as a string' do
puts table(:people).inspect
puts query("PRAGMA table_info(people)")
table(:people).should have_column(:name)
table(:people).column(:name).type.should == 'string'
table(:people).column(:name).should_not permit_null
end

it 'should have a nullable age column as a int' do
table(:people).should have_column(:age)
table(:people).column(:age).type.should == 'int'
table(:people).column(:age).should_not permit_null
end

end
11 changes: 6 additions & 5 deletions dm-migrations/lib/migration.rb
Expand Up @@ -13,7 +13,7 @@ def initialize(migration)
class Migration
include SQL

attr_accessor :position, :name
attr_accessor :position, :name, :database, :adapter

def initialize( position, name, opts = {}, &block )
@position, @name = position, name
Expand All @@ -22,10 +22,11 @@ def initialize( position, name, opts = {}, &block )
@database = DataMapper.repository(@options[:database] || :default)
@adapter = @database.adapter

puts @adapter.class
case @adapter.class.to_s
when /Sqlite3/ then extend(SQL::Sqlite3)
when /Mysql/ then extend(SQL::Mysql)
when /Postgres/ then extend(SQL::Postgres)
when /Sqlite3/ then @adapter.extend(SQL::Sqlite3)
when /Mysql/ then @adapter.extend(SQL::Mysql)
when /Postgres/ then @adapter.extend(SQL::Postgres)
else
raise "Unsupported Migration Adapter #{@adapter.class}"
end
Expand Down Expand Up @@ -156,7 +157,7 @@ def quoted_name
end

def migration_info_table_exists?
table_exists?('migration_info')
adapter.table_exists?('migration_info')
end

# Fetch the record for this migration out of the migration_info table
Expand Down
47 changes: 40 additions & 7 deletions dm-migrations/lib/spec/example/migration_example_group.rb
@@ -1,39 +1,72 @@
require 'spec'

require File.dirname(__FILE__) + '/../lib/spec/matchers/migration_matchers'

module Spec
module Example
class MigrationExampleGroup < Spec::Example::ExampleGroup
include Spec::Matchers::Migration

before(:all) do
# drop & create db
run_prereq_migrations
if this_migration.adapter.supports_schema_transactions?
run_prereq_migrations
end
end

before(:each) do
run_migration
if ! this_migration.adapter.supports_schema_transactions?
run_prereq_migrations
else
this_migration.adapter.begin_transaction
end
end

after(:each) do
if this_migration.adapter.supports_schema_transactions?
this_migration.adapter.rollback_transaction
end
end

after(:all) do
# drop db
this_migration.adapter.drop_database
end

def run_prereq_migrations
"running n-1 migrations"
all_databases.each do |db|
db.adapter.drop_database
db.adapter.create_database
end
@@migrations.sort.each do |migration|
break if migration.name.to_s == migration_name.to_s
migration.perform_up
end
end

def run_migration
@@migrations.sort.each do |migration|
migration.perform_up if migration.name.to_s == migration_name
end
this_migration.perform_up
end

def migration_name
@migration_name ||= self.class.instance_variable_get("@description_text").to_s
end

def all_databases
@@migrations.map(&:database).uniq
end

def this_migration
@@migrations.select { |m| m.name.to_s == migration_name }.first
end

def query(sql)
this_migration.adapter.query(sql)
end

def table(table_name)
this_migration.adapter.table(table_name)
end

Spec::Example::ExampleGroupFactory.register(:migration, self)

end
Expand Down
100 changes: 100 additions & 0 deletions dm-migrations/lib/spec/matchers/migration_matchers.rb
@@ -0,0 +1,100 @@

module Spec
module Matchers
module Migration

def have_table(table_name)
HaveTableMatcher.new(table_name)
end

def have_column(column_name)
HaveColumnMatcher.new(column_name)
end

def permit_null
NullableColumnMatcher.new
end

def be_primary_key
PrimaryKeyMatcher.new
end

class HaveTableMatcher

attr_accessor :table_name, :repository

def initialize(table_name)
@table_name = table_name
end

def matches?(repository)
repository.adapter.table_exists?(table_name)
end

def failure_message
%(expected #{repository} to have table '#{table_name}')
end

def negative_failure_message
%(expected #{repository} to not have table '#{table_name}')
end

end

class HaveColumnMatcher

attr_accessor :table, :column_name

def initialize(column_name)
@column_name = column_name
end

def matches?(table)
@table = table
table.columns.map(&:name).include?(column_name.to_s)
end

def failure_message
%(expected #{table} to have column '#{column_name}')
end

def negative_failure_message
%(expected #{table} to not have column '#{column_name}')
end

end

class NullableColumnMatcher

attr_accessor :column

def matches?(column)
@column = column
! column.not_null
end

def failure_message
%(expected #{column.name} to permit NULL)
end

def negative_failure_message
%(expected #{column.name} to be NOT NULL)
end

end

class PrimaryKeyMatcher

attr_accessor :column

def matches?(column)
@column = column
column.primary_key
end

end

end
end
end

0 comments on commit 74e2601

Please sign in to comment.