-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
63 lines (58 loc) · 1.83 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
#!/usr/bin/env ruby
# -*- ruby -*-
require_relative 'lib/environment'
require 'active_record'
require 'rake/testtask'
Rake::TestTask.new(test: "db:test:prepare") do |t|
t.pattern = "test/test_*.rb"
end
desc "Run tests"
task :default => :test
# desc 'import data from the given file'
# task :import_data do
# Environment.environment = "production"
# require_relative 'lib/importer'
# Importer.import("data/purchase_import.csv")
# end
db_namespace = namespace :db do
desc "Migrate the db"
task :migrate do
Environment.environment = 'production'
Environment.connect_to_database
ActiveRecord::Migrator.migrate("db/migrate/")
db_namespace["schema:dump"].invoke
end
namespace :test do
desc "Prepare the test database"
task :prepare do
Environment.environment = 'test'
Environment.connect_to_database
file = ENV['SCHEMA'] || "db/schema.rb"
if File.exists?(file)
load(file)
else
abort %{#{file} doesn't exist yet. Run `rake db:migrate` to create it.}
end
end
end
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
task :rollback do
Environment.environment = 'production'
Environment.connect_to_database
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
db_namespace["schema:dump"].invoke
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'
Environment.environment = 'production'
Environment.connect_to_database
filename = ENV['SCHEMA'] || "db/schema.rb"
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
end
end