-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
77 lines (68 loc) · 2.11 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'rake'
require 'bundler/setup'
require 'rspec/core/rake_task'
require 'byebug'
require './environment'
task :default => :test
task :test => :spec
if !defined?(RSpec)
puts "spec targets require RSpec"
else
desc "Run all examples"
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*.rb'
end
end
# usage: rake generate:migration[name_of_migration]
namespace :generate do
task(:migration, :migration_name) do |t, args|
timestamp = Time.now.gmtime.to_s[0..18].gsub(/[^\d]/, '')
migration_name = args[:migration_name]
file_name = "%s_%s.rb" % [timestamp, migration_name]
class_name = migration_name.split("_").map {|w| w.capitalize}.join('')
path = File.join(File.expand_path(File.dirname(__FILE__)), 'db', 'migrate', file_name)
f = open(path, 'w')
content = "class #{class_name} < ActiveRecord::Migration
def up
end
def down
end
end
"
f.write(content)
puts "Generated migration %s" % path
f.close
end
end
namespace :db do
require 'active_record'
conf = YAML.load(open(File.join(File.expand_path(File.dirname(__FILE__)), 'config.yml')).read).deep_symbolize_keys
env = Sinatra::Application.settings.environment
desc "Migrate the database"
task(:migrate => :environment) do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("db/migrate")
end
namespace :drop do
task(:all) do
if ['0.0.0.0', '127.0.0.1', 'localhost'].include?(conf[env][:host].strip)
database = conf[env].delete(:database)
ActiveRecord::Base.establish_connection(conf[env])
ActiveRecord::Base.connection.execute("drop database if exists #{database}")
end
end
end
namespace :create do
task(:all) do
if ['0.0.0.0', '127.0.0.1', 'localhost'].include?(conf[env][:host].strip)
database = conf[env].delete(:database)
ActiveRecord::Base.establish_connection(conf[env])
ActiveRecord::Base.connection.execute("create database if not exists #{database}")
end
end
end
end
task :environment do
require_relative './environment'
end