public
Description: A thin wrapper to use Rails Migrations in non Rails projects
Homepage: http://gabrito.com/post/standalone-migrations-using-rails-migrations-in-non-rails-projects
Clone URL: git://github.com/thuss/standalone-migrations.git
100644 76 lines (64 sloc) 2.413 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
APP_BASE = File.dirname(File.expand_path(__FILE__))
 
# Add to load_path every "lib/" directory
Dir["#{APP_BASE}/**/lib"].each { |p| $: << p }
 
namespace :db do
  task :ar_init do
    require 'active_record'
    ENV['RAILS_ENV'] ||= 'development'
    config = YAML.load_file(APP_BASE + '/config/database.yml')[ENV['RAILS_ENV']]
    ActiveRecord::Base.establish_connection(config)
    logger = Logger.new $stderr
    logger.level = Logger::INFO
    ActiveRecord::Base.logger = logger
  end
 
  desc "Migrate the database using the scripts in the migrate directory. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
  task :migrate => :ar_init do
    require 'migration_helpers/init'
    ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
    ActiveRecord::Migrator.migrate(APP_BASE + "/migrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
    Rake::Task[ "db:schema:dump" ].execute
  end
 
  namespace :schema do
    desc "Create schema.rb file that can be portably used against any DB supported by AR"
    task :dump => :ar_init do
      require 'active_record/schema_dumper'
      File.open(ENV['SCHEMA'] || APP_BASE + "/schema.rb", "w") do |file|
        ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
      end
    end
 
    desc "Load a ar_schema.rb file into the database"
    task :load => :ar_init do
      file = ENV['SCHEMA'] || APP_BASE + "/schema.rb"
      load(file)
    end
  end
 
  desc "Create a new migration"
  task :new_migration do |t|
    unless ENV['name']
      puts "Error: must provide name of migration to generate."
      puts "For example: rake #{t.name} name=add_field_to_form"
      exit 1
    end
 
    underscore = lambda { |camel_cased_word|
      camel_cased_word.to_s.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
    }
 
    migration = underscore.call( ENV['name'] )
    file_name = "migrations/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{migration}.rb"
    class_name = migration.split('_').map { |s| s.capitalize }.join
 
    file_contents = <<eof
class #{class_name} < ActiveRecord::Migration
def self.up
end
 
def self.down
raise ActiveRecord::IrreversibleMigration
end
end
eof
    File.open(file_name, 'w') { |f| f.write file_contents }
 
    puts "Created migration #{file_name}"
  end
end