-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
109 lines (93 loc) · 2.74 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
require "bundler/setup"
require "pry-byebug" unless ENV["RACK_ENV"] == "production"
require "rom/sql/rake_task"
require "shellwords"
require_relative "system/todo/container"
begin
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new :spec
task default: [:spec]
rescue LoadError
end
def db
Todo::Container["persistence.db"]
end
def settings
Todo::Container["settings"]
end
def database_uri
require "uri"
URI.parse(settings.database_url)
end
def postgres_env_vars(uri)
{}.tap do |vars|
vars["PGHOST"] = uri.host
vars["PGPORT"] = uri.port.to_s if uri.port
vars["PGUSER"] = uri.user if uri.user
vars["PGPASSWORD"] = uri.password if uri.password
end
end
namespace :db do
task :setup do
Todo::Container.init :persistence
end
desc "Print current database schema version"
task version: :setup do
version =
if db.tables.include?(:schema_migrations)
db[:schema_migrations].order(:filename).last[:filename]
else
"not available"
end
puts "Current schema version: #{version}"
end
desc "Create database"
task :create do
if system("which createdb", out: File::NULL)
uri = database_uri
system(postgres_env_vars(uri), "createdb #{Shellwords.escape(uri.path[1..-1])}")
else
puts "You must have Postgres installed to create a database"
exit 1
end
end
desc "Drop database"
task :drop do
if system("which dropdb", out: File::NULL)
uri = database_uri
system(postgres_env_vars(uri), "dropdb #{Shellwords.escape(uri.path[1..-1])}")
else
puts "You must have Postgres installed to drop a database"
exit 1
end
end
desc "Migrate database up to latest migration available"
task :migrate do
# Enhance the migration task provided by ROM
# Once it finishes, dump the db structure
Rake::Task["db:structure:dump"].execute
# And print the current migration version
Rake::Task["db:version"].execute
end
namespace :structure do
desc "Dump database structure to db/structure.sql"
task :dump do
if system("which pg_dump", out: File::NULL)
uri = database_uri
system(postgres_env_vars(uri), "pg_dump -s -x -O #{Shellwords.escape(uri.path[1..-1])}", out: "db/structure.sql")
else
puts "You must have pg_dump installed to dump the database structure"
end
end
end
desc "Load seed data into the database"
task :seed do
seed_data = File.join("db", "seed.rb")
load(seed_data) if File.exist?(seed_data)
end
desc "Load a small, representative set of data so that the application can start in a useful state (for development)."
task :sample_data do
sample_data = File.join("db", "sample_data.rb")
load(sample_data) if File.exist?(sample_data)
end
end