-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
103 lines (92 loc) · 2.93 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
98
99
100
101
102
103
require_relative 'config/environment.rb'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'activerecord_sane_schema_dumper'
require 'admit_one'
task default: [:rubocop, :spec]
task :rubocop do
RuboCop::RakeTask.new
end
task :spec do
RSpec::Core::RakeTask.new(:spec)
end
namespace :streaker do
desc 'Pull strava activities'
task :process do
begin
Streaker.logger.info('Running rake streaker:process')
begin
AdmitOne::LockFile.new(:streaker) do
StravaPull.new.call
JefitScraping.new.call
SnapshotFill.new.call
AlertCheck.new.call
end
rescue AdmitOne::LockFailure
Streaker.logger.info('Failed to aquire AdmitOne lock. Another '\
'instance already running?')
end
Streaker.logger.info('Finished rake streaker:process')
rescue Exception => e
Streaker.logger.fatal("streaker:process died with #{e.class}: #{e.message}")
raise e
end
end
end
namespace :db do
task :environment do
DATABASE_ENV = 'streaker'.freeze
MIGRATIONS_DIR = 'db/migrate'.freeze
end
task configuration: :environment do
env = ENV['STREAKER_ENV'] || 'development'
@config = YAML.load_file('config/database.yml')[env]
end
task configure_connection: :configuration do
ActiveRecord::Base.establish_connection @config
ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
end
task migrate: :configure_connection do
ActiveRecord::Migration.verbose = true
if ActiveRecord::Migrator.migrate(MIGRATIONS_DIR, ENV['VERSION'])
ENV['VERSION'].to_i
end
Rake::Task['db:schema:dump'].invoke
end
desc 'Rolls the schema back to the previous version '\
'(specify steps w/ STEP=n).'
task rollback: :configure_connection do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
ActiveRecord::Migrator.rollback MIGRATIONS_DIR, step
Rake::Task['db:schema:dump'].invoke
end
desc 'Dump Schema'
namespace :schema do
task dump: :configure_connection do
ActiveRecord::SchemaDumper.send(:include,
ActiveRecord::SaneSchemaDumper::Extension)
File.open('db/schema.rb', 'w:utf-8') do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
end
desc 'Generate migration. '\
'format: rake db:generate_migration[create_foos_table]'
task :generate_migration, [:name] => :environment do |_, args|
unless args[:name]
raise 'Must pass name. rake db:generate_migration[create_foos_table]'
end
timestamp = DateTime.now.strftime('%Y%m%d%H%M%S%L')
class_name = args[:name].split('_').collect(&:capitalize).join
filename = "#{MIGRATIONS_DIR}/#{timestamp}_#{args[:name]}.rb"
File.open(filename, 'w') do |file|
file.write(<<-EOS)
class #{class_name} < ActiveRecord::Migration
def change
end
end
EOS
end
puts "Created: #{filename}"
end
end