-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
77 lines (62 loc) · 1.91 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 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
require_relative 'config/application'
desc 'Look for style guide offenses in your code'
task :rubocop do
sh 'rubocop --format simple || true'
end
task default: [:rubocop, :spec]
desc 'Open an irb session preloaded with the environment'
task :console do
require 'rubygems'
require 'pry'
Pry.start
end
## Active Record related rake tasks
db_namespace = namespace :db do
desc 'create the database'
task :create do
puts "Creating #{db_path}..."
touch db_path
end
desc 'drop the database'
task :drop do
puts "Deleting #{db_path}..."
rm_f db_path
end
desc 'migrate the database (options: VERSION=x).'
task :migrate do
ActiveRecord::Migrator.migrations_paths = [File.join(__dir__, 'db/migrate')]
ActiveRecord::Migration.verbose = true
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
args = [ActiveRecord::Migrator.migrations_paths, ActiveRecord::SchemaMigration]
ActiveRecord::MigrationContext.new(*args).migrate(version)
db_namespace["schema:dump"].invoke
end
desc 'Retrieves the current schema version number'
task :version do
puts "Current version: #{ActiveRecord::Migrator.current_version}"
end
desc 'populate the database with sample data'
task :seed do
require "#{__dir__}/db/seeds.rb"
end
desc 'Gives you a timestamp for your migration file name'
task :timestamp do
puts DateTime.now.strftime('%Y%m%d%H%M%S')
end
namespace :schema do
desc 'Create a db/schema.rb file that can be portably used against any DB supported by AR'
task :dump do
require 'active_record/schema_dumper'
filename = 'db/schema.rb'
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
end
private
def db_path
ActiveRecord::Base.configurations['development']['database']
end
end