-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
121 lines (102 loc) · 3.73 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
require 'bundler'
Bundler.require
require 'standalone_migrations'
require 'will_paginate/active_record' if defined? WillPaginate
# Use config/database.yml instead of db/config.yml for db connection config
module StandaloneMigrations
class Configurator
def config
'config/database.yml'
end
end
end
if File.exists?('db')
StandaloneMigrations::Tasks.load_tasks
# Load models from models directory, like Rails (for db:seed task)
Dir['./models/*.rb'].each { |file| require file }
end
desc "Run a Pry session with models from Sinatra app specified by config.ru"
task :console do
require 'rack'
require 'pry'
# load Sinatra app specified in config.ru
app, options = Rack::Builder.parse_file('config.ru')
# 'rake console' is two words; remove first so can specify pry args
ARGV.shift
# like running pry from command line
Pry::CLI.parse_options # like running pry from command line
end
task :c => :console
Rake::Task['routes'].clear
desc "List routes of the Sinatra app specified in config.ru"
task :routes do
require 'rack'
# load Sinatra app specified in config.ru
app, options = Rack::Builder.parse_file('config.ru')
# thanks to cldwalker's tux gem for this code to interpret Sinatra routes
routes = app.routes.inject([]) {|arr, (k,v)|
arr += v.map {|regex,params,*|
path = params.empty? ? regex.inspect :
params.inject(regex.inspect) {|s,e| s.sub(/\([^()]+\)/, ":#{e}") }
[k, (str = path[%r{/\^(.*)\$/}, 1]) ? str.tr('\\', '') : path]
}
}
# still need to pull the regex syntax out
routes = routes.map do |method_and_path|
method, path = method_and_path
path = path.to_s.gsub("\\/", "/").gsub(%r[^/\\A], "").gsub(%r[\\z/$], "")
"#{method} #{path}"
end
# don't show HEAD requests
routes = routes.reject! { |route| route =~ /^HEAD / }
# GET and POST paths should line up at the same column
routes.each { |route| route.gsub! /^GET /, "GET " }
routes.each { |route| puts route }
end
namespace :db do
desc "Show tables' schema and data in db specified by config/database.yml"
task :dump do
require './davinci-sinatra.rb'
ActiveRecord::Base.logger = nil # turn off SQL logging
sql = "select tablename from pg_tables where schemaname = 'public'"
rows = ActiveRecord::Base.connection.execute(sql)
rows.each do |row|
table_name = row['tablename']
puts "#{'_' * 25} #{table_name} table #{'_' * (42 - table_name.size)}"
sql = "select column_name,
case when udt_name = 'varchar'
then concat('varchar(', character_maximum_length, ')')
else data_type end as data_type,
is_nullable
from information_schema.columns
where table_name = '#{table_name}'
and table_schema = 'public'
order by ordinal_position"
rows2 = ActiveRecord::Base.connection.execute(sql)
longest_name_length =
rows2.collect { |row2| row2['column_name'].size }.max + 1
rows2.each_with_index do |row2, i|
puts sprintf("| Type of %-#{longest_name_length}s %s %s",
row2['column_name'] + ':',
row2['data_type'],
row2['is_nullable'] ? '' : 'not null')
end
puts '' # to separate schema from data
sql = "select * from #{table_name};"
rows2 = ActiveRecord::Base.connection.execute(sql)
if rows2.first
keys = rows2.first.keys
longest_key_length = keys.collect { |key| key.size }.max
rows2.each_with_index do |row2, i|
puts '' unless i == 0 # separation between rows
keys.each do |key|
value = row2[key]
value = 'NULL' if value.nil?
puts sprintf("%#{longest_key_length}s: %s", key, value)
end
end
end
end
puts '_' * 75
end
end