-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
60 lines (50 loc) · 1.71 KB
/
Rakefile
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
# encoding: UTF-8 (magic comment)
require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = "tests/*.rb"
end
namespace :db do
task :environment do
require_relative 'app_config'
require_relative 'models/init'
end
desc 'create an ActiveRecord migration in ./db/migrate'
task :create_migration do
name = ENV['NAME']
if name.nil?
raise 'No NAME specified. Example usage: `rake db:create_migration NAME=create_users`'
end
migrations_dir = File.join('db', 'migrate')
version = ENV['VERSION'] || Time.now.utc.strftime('%Y%m%d%H%M%S')
filename = "#{ version }_#{ name }.rb"
migration_class = name.split('_').map(&:capitalize).join
FileUtils.mkdir_p(migrations_dir)
File.open(File.join(migrations_dir, filename), 'w') do |file|
file.write <<-MIGRATION.strip_heredoc
class #{ migration_class } < ActiveRecord::Migration
def up
end
def down
end
end
MIGRATION
end
end
desc 'migrate the database (use version with VERSION=n)'
# NOTE: set 'RACK_ENV' environment variable to specify deployment
# environment (:-\)
# It can be 'development', 'test', 'production'.
# The default is usually 'development'.
# Example: rake RACK_ENV=test db:migrate
task(:migrate => :environment) do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.verbose = true
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
ActiveRecord::Migrator.migrate('db/migrate', version)
end
desc 'rolls back the migration (use steps with STEP=n)'
task(:rollback => :environment) do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
ActiveRecord::Migrator.rollback('db/migrate', step)
end
end