-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
82 lines (73 loc) · 2.12 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
# rubocop:disable Airbnb/RiskyActiverecordInvocation
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new :specs do |task|
task.pattern = Dir['spec/**/*_spec.rb']
end
desc "start trasina server"
task :server do |args|
system %(puma -C config/puma.rb)
end
task :s => :server
desc "start trasina console"
task :console do |args|
system %(./bin/console)
end
task :c => :console
namespace :db do
desc "create database"
task :create do |task, args|
require 'sequel'
target_database_name = "#{ENV['POSTGRES_DATABASE_BASE_NAME']}_#{ENV["RACK_ENV"]}"
begin
Sequel.connect(
adapter: :postgres,
user: ENV["POSTGRES_USER"],
password: ENV["POSTGRES_PASSWORD"],
host: ENV["POSTGRES_HOST"],
port: ENV["POSTGRES_PORT"]
) do |db|
puts "creating #{target_database_name}"
db.execute "CREATE DATABASE #{target_database_name}"
end
rescue Sequel::DatabaseError => e
if e.wrapped_exception.class != PG::DuplicateDatabase
raise e
end
puts "already exists #{target_database_name}"
end
end
desc "drop database"
task :drop do |task, args|
require 'sequel'
Sequel.connect(
adapter: :postgres,
user: ENV["POSTGRES_USER"],
password: ENV["POSTGRES_PASSWORD"],
host: 'db',
port: ENV["POSTGRES_PORT"]
) do |db|
database = "#{ENV['POSTGRES_DATABASE_BASE_NAME']}_#{ENV["RACK_ENV"]}"
puts "droping #{database}"
db.execute "DROP DATABASE IF EXISTS #{database}"
end
end
desc "Run migrations"
task :migrate, [:version] do |t, args|
require "sequel/core"
Sequel.extension :migration
version = args[:version].to_i if args[:version]
database = "#{ENV['POSTGRES_DATABASE_BASE_NAME']}_#{ENV["RACK_ENV"]}"
Sequel.connect(
adapter: :postgres,
user: ENV["POSTGRES_USER"],
password: ENV["POSTGRES_PASSWORD"],
host: 'db',
port: ENV["POSTGRES_PORT"],
database: database
) do |db|
Sequel::Migrator.run(db, "db/migrations", target: version)
end
end
end
task :default => ['specs']
# rubocop:enable Airbnb/RiskyActiverecordInvocation