-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
191 lines (164 loc) · 4.95 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require "rake/testtask"
unless ENV["RACK_ENV"] == "production"
require "bcrypt"
require "standard/rake"
require "dotenv"
Dotenv.load
end
MIGRATIONS_PATH = File.expand_path("db/migrations", __dir__)
SCHEMA_PATH = File.expand_path("db/schema", __dir__)
Rake::TestTask.new do |t|
t.pattern = "test/**/*_test.rb"
t.warning = false
end
def db_connection(&block)
require "sequel/core"
Sequel.connect(ENV['DATABASE_URL']) do |db|
block.call(db)
end
if ENV['TEST_DATABASE_URL']
Sequel.connect(ENV['TEST_DATABASE_URL']) do |db|
block.call(db)
end
end
end
desc "Run tests"
task default: :test
desc "Generate random base64 string of 64 bytes length"
task :random do
require "securerandom"
random_str = SecureRandom.base64(48)
puts "Random string of 64 bytes length: #{random_str}"
end
desc "Start interactive console"
task :c do |t|
system "irb -r ./db/db.rb"
end
desc "Start development server"
task :ds do |t|
exec "RACK_ENV=development bundle exec rerun --ignore 'test/*' rackup config.ru"
end
desc "Start classic development server"
task :s do |t|
system "RACK_ENV=development bundle exec rackup config.ru"
end
desc "start classic server in production mode"
task :ps do
system "RACK_ENV=production bundle exec rackup config.ru"
end
namespace :db do
desc "Run migrations"
task :migrate, [:version] do |t, args|
require "sequel/core"
Sequel.extension :migration
version = args[:version].to_i if args[:version]
Sequel.connect(ENV["DATABASE_URL"]) do |db|
Sequel::Migrator.run(db, MIGRATIONS_PATH, target: version)
end
puts "All migrations applied"
if ENV["TEST_DATABASE_URL"]
Sequel.connect(ENV["TEST_DATABASE_URL"]) do |db|
Sequel::Migrator.run(db, MIGRATIONS_PATH, target: version)
puts "Migrations also applied to test database"
end
end
end
desc "Revert all app migrations"
task :reset do |t|
require "sequel/core"
Sequel.extension :migration
Sequel.connect(ENV["DATABASE_URL"]) do |db|
Sequel::Migrator.run(db, MIGRATIONS_PATH, target: 0)
end
puts "All migrations have been reverted"
if ENV["TEST_DATABASE_URL"]
Sequel.connect(ENV["TEST_DATABASE_URL"]) do |db|
Sequel::Migrator.run(db, MIGRATIONS_PATH, target: 0)
puts "Also on test database"
end
end
end
desc "Delete all data from database"
task :clean do
require "sequel/core"
tables = [:account_sms_codes,
:account_recovery_codes,
:account_otp_keys,
:account_webauthn_keys,
:account_webauthn_user_ids,
:account_session_keys,
:account_active_session_keys,
:account_email_auth_keys,
:account_lockouts,
:account_login_failures,
:account_remember_keys,
:account_login_change_keys,
:account_verification_keys,
:account_password_reset_keys,
:account_authentication_audit_logs,
:admins,
:entries,
:batches,
:accounts,
:account_statuses]
Sequel.connect(ENV["DATABASE_URL"]) do |db|
tables.each do |table|
if db.table_exists?(table)
db[table].delete
puts "Table #{table} deleted"
end
end
end
end
desc "Reset database, run all migrations and seed the database"
task fresh: [:clean, :reset, :migrate, :seed]
desc "Check pending migrations"
task :pending do |t|
require "sequel/core"
Sequel.extension :migration
Sequel.connect(ENV["DATABASE_URL"]) do |db|
if Sequel::Migrator.is_current?(db, MIGRATIONS_PATH)
puts "No pending migration."
else
puts "There are pending migrations. Run 'rake db:migrate' to apply them"
end
end
if ENV["TEST_DATABASE_URL"]
Sequel.connect(ENV["TEST_DATABASE_URL"]) do |db|
if Sequel::Migrator.is_current?(db, MIGRATIONS_PATH)
puts "No pending migration on test database."
else
puts "There are pending migrations on the test database. Run 'rake db:migrate' to apply them"
end
end
end
end
desc "Inject seed data"
task :seed do |t|
system "ruby db/seeds/seeds.rb"
puts "Seed data have been inserted"
end
desc "Dump database schema with timestamp"
task :schema do |t|
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
destination = File.join(SCHEMA_PATH, "001_schema_#{timestamp}.rb")
system "rm #{SCHEMA_PATH}/*"
system("sequel -D #{ENV["DATABASE_URL"]} > #{destination}")
puts "Schema dumped to #{destination}"
end
end
desc "Cleanup the temp files"
task :clean_tmp do
temp_directory = File.expand_path("tmp", __dir__)
number_tmp_files = Dir.entries(temp_directory).length
if number_tmp_files == 2 # "." and ".."
puts "Temp directory was empty"
else
Dir[File.join(temp_directory, "*")].each { |f| FileUtils.remove_entry_secure(f) }
puts "Temporary Directory cleaned - #{number_tmp_files - 2} removed"
end
end
desc "Generate a password hash"
task :gen_ph, [:pw] do |pw|
puts BCrypt::Password.create(pw, cost: 2)
end