-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
83 lines (75 loc) · 2.23 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
78
79
80
81
82
83
require 'rake'
require 'bundler/setup'
require 'rspec/core/rake_task'
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'
t.rspec_opts = ['-cfs']
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'
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 => :environment) do
conf = ArticleSemanticizer::CONF_DATA
[:development, :test, :production].each do |k|
v = conf[k]
if ['0.0.0.0', '127.0.0.1', 'localhost'].include?(v[:host].strip)
v = v.dup
database = v.delete(:database)
ActiveRecord::Base.establish_connection(v)
ActiveRecord::Base.connection.execute("drop database if exists #{database}")
end
end
end
end
namespace :create do
task(:all => :environment) do
conf = ArticleSemanticizer::CONF_DATA
[:development, :test, :production].each do |k|
v = conf[k]
if ['0.0.0.0', '127.0.0.1', 'localhost'].include?(v[:host].strip)
v = v.dup
database = v.delete(:database)
ActiveRecord::Base.establish_connection(v)
ActiveRecord::Base.connection.execute("create database if not exists #{database}")
end
end
end
end
end
task :environment do
require_relative './environment'
end