forked from revskill10/CSV-To-DSpace-XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
97 lines (85 loc) · 2.58 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'rake'
require 'bundler/setup'
require 'rspec/core/rake_task'
require_relative './environment'
# require 'sinatra-ar-example-app'
require 'sinatra/activerecord/rake'
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'
conf = YAML.load(open(File.join(File.expand_path(File.dirname(__FILE__)), 'config/config.yml')).read)["localdb"]
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
conf.each do |k, v|
if ['0.0.0.0', '127.0.0.1', 'localhost'].include?(v['host'].strip)
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) do
conf.each do |k, v|
if ['0.0.0.0', '127.0.0.1', 'localhost'].include?(v['host'].strip)
database = v.delete('database')
ActiveRecord::Base.establish_connection(v)
ActiveRecord::Base.connection.execute("create database if not exists #{database}")
end
end
end
task(:test) do
k = "test"
v = conf[k]
if ['0.0.0.0', '127.0.0.1', 'localhost'].include?(v['host'].strip)
database = v.delete('database')
ActiveRecord::Base.establish_connection(v)
ActiveRecord::Base.connection.execute("create database if not exists #{database}")
end
end
end
task :seed do
require_relative './db/seeds'
end
end
task :environment do
require_relative './environment'
end