-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
97 lines (81 loc) · 2.18 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/testtask"
import "tasks/legacy.rake"
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/**/*_test.rb']
end
task :default => :test
namespace :db do
desc "Migrate the database (you can specify the version with `db:migrate[N]`)"
task :migrate, [:version] do |task, args|
version = args[:version] ? Integer(args[:version]) : nil
migrate(version)
dump_schema
end
desc "Undo all migrations"
task :demigrate do
migrate(0)
dump_schema
end
desc "Undo all migrations and migrate again"
task :remigrate do
migrate(0)
migrate
dump_schema
end
def migrate(version = nil)
databases.each do |db|
Sequel.extension :migration
Sequel::Migrator.apply(db, "db/migrations", version)
end
end
def dump_schema
unless ENV["RACK_ENV"] == "production"
db = databases.first
system "pg_dump #{db.opts[:database]} > db/schema.sql"
db.extension :schema_dumper
File.write("db/schema.rb", db.dump_schema_migration(same_db: true))
end
end
def databases
@databases ||= ["development", "test", "production"]
.map { |env| get_database(env) }
.compact
end
def get_database(env)
ENV["RACK_ENV"] = env
require "kvizovi/configuration/sequel"
DB
rescue Sequel::DatabaseConnectionError
ensure
Object.send(:remove_const, :DB) if Object.const_defined?(:DB)
$LOADED_FEATURES.reject! { |path| path.include?("kvizovi/configuration/sequel") }
ENV.delete("RACK_ENV")
end
end
namespace :elastic do
desc "Create Elaticsearch index and import the records"
task :setup => [:create, :import]
desc "Create the Elasticsearch index"
task :create do
require "kvizovi"
Kvizovi::ElasticsearchIndex.create!
end
desc "Import all existing quizzes to Elasticsearch"
task :import do
require "kvizovi"
Kvizovi::Models::Quiz.dataset.each_page(1000) do |quizzes|
Kvizovi::ElasticsearchIndex[:quiz].index(quizzes)
end
end
end
desc "Start the console with app loaded, in sandbox mode"
task :console do
ARGV.clear
require "kvizovi"
require "pry"
require "logger"
include Kvizovi::Models
DB.logger = Logger.new(STDOUT)
Pry.start
end