Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Migrations rake task. Copied example migrations from examples/ to db/…
Browse files Browse the repository at this point in the history
…migrations/. Temporarily added a config.rb in the migrations directory for setting up the logger and default database.

*	rake db:migrate 				=> runs migrations up
*	DIRECTION=down rake db:migrate 	=> runs migrations down
*	MIGRATION_DIR rake db:migrate 	=> uses a directory different than ./db/migrations/
  • Loading branch information
Bernerd Schaefer committed Jun 4, 2008
1 parent 9871145 commit b65c5db
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Rakefile
Expand Up @@ -42,3 +42,26 @@ Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
end

namespace :db do

# pass the relative path to the migrations directory by MIGRATION_DIR
task :setup_migration_dir do
unless defined?(MIGRATION_DIR)
migration_dir = ENV["MIGRATION_DIR"] || File.join("db", "migrations")
MIGRATION_DIR = File.expand_path(File.join(File.dirname(__FILE__), migration_dir))
end
FileUtils.mkdir_p MIGRATION_DIR
end

# set DIRECTION to migrate down
desc "Run your system's migrations"
task :migrate => [:setup_migration_dir] do
require File.expand_path(File.join(File.dirname(__FILE__), "lib", "migration_runner.rb"))
require File.expand_path(File.join(MIGRATION_DIR, "config.rb"))

Dir[File.join(MIGRATION_DIR, "*.rb")].each { |file| require file }

ENV["DIRECTION"] != "down" ? migrate_up! : migrate_down!
end
end
12 changes: 12 additions & 0 deletions db/migrations/1_create_people_table.rb
@@ -0,0 +1,12 @@
migration 1, :create_people_table do
up do
create_table :people do
column :id, Integer, :serial => true
column :name, String, :size => 50
column :age, Integer
end
end
down do
drop_table :people
end
end
13 changes: 13 additions & 0 deletions db/migrations/2_add_dob_to_people.rb
@@ -0,0 +1,13 @@
migration 2, :add_dob_to_people do
up do
modify_table :people do
add_column :dob, DateTime, :nullable? => true
end
end

down do
modify_table :people do
drop_column :dob
end
end
end
4 changes: 4 additions & 0 deletions db/migrations/config.rb
@@ -0,0 +1,4 @@
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.logger.debug( "Starting Migration" )

DataMapper.setup(:default, 'postgres://postgres@localhost/dm_core_test')

0 comments on commit b65c5db

Please sign in to comment.