|
| 1 | +require 'bundler' |
| 2 | +Bundler.require |
| 3 | + |
| 4 | +require 'standalone_migrations' |
| 5 | +require 'will_paginate/active_record' if defined? WillPaginate |
| 6 | + |
| 7 | +# Use config/database.yml instead of db/config.yml for db connection config |
| 8 | +module StandaloneMigrations |
| 9 | + class Configurator |
| 10 | + def config |
| 11 | + 'config/database.yml' |
| 12 | + end |
| 13 | + end |
| 14 | +end |
| 15 | + |
| 16 | +if File.exists?('db') |
| 17 | + StandaloneMigrations::Tasks.load_tasks |
| 18 | + |
| 19 | + # Load models from models directory, like Rails (for db:seed task) |
| 20 | + Dir['./models/*.rb'].each { |file| require file } |
| 21 | +end |
| 22 | + |
| 23 | +desc "Run a Pry session with models from Sinatra app specified by config.ru" |
| 24 | +task :console do |
| 25 | + require 'rack' |
| 26 | + require 'pry' |
| 27 | + |
| 28 | + # load Sinatra app specified in config.ru |
| 29 | + app, options = Rack::Builder.parse_file('config.ru') |
| 30 | + |
| 31 | + # 'rake console' is two words; remove first so can specify pry args |
| 32 | + ARGV.shift |
| 33 | + |
| 34 | + # like running pry from command line |
| 35 | + Pry::CLI.parse_options # like running pry from command line |
| 36 | +end |
| 37 | + |
| 38 | +desc "List routes of the Sinatra app specified in config.ru" |
| 39 | +task :routes do |
| 40 | + require 'rack' |
| 41 | + |
| 42 | + # load Sinatra app specified in config.ru |
| 43 | + app, options = Rack::Builder.parse_file('config.ru') |
| 44 | + |
| 45 | + # thanks to cldwalker's tux gem for this code to interpret Sinatra routes |
| 46 | + routes = app.routes.inject([]) {|arr, (k,v)| |
| 47 | + arr += v.map {|regex,params,*| |
| 48 | + path = params.empty? ? regex.inspect : |
| 49 | + params.inject(regex.inspect) {|s,e| s.sub(/\([^()]+\)/, ":#{e}") } |
| 50 | + [k, (str = path[%r{/\^(.*)\$/}, 1]) ? str.tr('\\', '') : path] |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + # still need to pull the regex syntax out |
| 55 | + routes = routes.map do |method_and_path| |
| 56 | + method, path = method_and_path |
| 57 | + path = path.to_s.gsub("\\/", "/").gsub(%r[^/\\A], "").gsub(%r[\\z/$], "") |
| 58 | + "#{method} #{path}" |
| 59 | + end |
| 60 | + |
| 61 | + # don't show HEAD requests |
| 62 | + routes = routes.reject! { |route| route =~ /^HEAD / } |
| 63 | + |
| 64 | + # GET and POST paths should line up at the same column |
| 65 | + routes.each { |route| route.gsub! /^GET /, "GET " } |
| 66 | + |
| 67 | + routes.each { |route| puts route } |
| 68 | +end |
| 69 | + |
| 70 | +namespace :db do |
| 71 | + desc "Show tables' schema and data in db specified by config/database.yml" |
| 72 | + task :dump do |
| 73 | + require './davinci-sinatra.rb' |
| 74 | + ActiveRecord::Base.logger = nil # turn off SQL logging |
| 75 | + |
| 76 | + sql = "select tablename from pg_tables where schemaname = 'public'" |
| 77 | + rows = ActiveRecord::Base.connection.execute(sql) |
| 78 | + rows.each do |row| |
| 79 | + table_name = row['tablename'] |
| 80 | + puts "#{'_' * 25} #{table_name} table #{'_' * (42 - table_name.size)}" |
| 81 | + |
| 82 | + sql = "select column_name, |
| 83 | + case when udt_name = 'varchar' |
| 84 | + then concat('varchar(', character_maximum_length, ')') |
| 85 | + else data_type end as data_type, |
| 86 | + is_nullable |
| 87 | + from information_schema.columns |
| 88 | + where table_name = '#{table_name}' |
| 89 | + and table_schema = 'public' |
| 90 | + order by ordinal_position" |
| 91 | + rows2 = ActiveRecord::Base.connection.execute(sql) |
| 92 | + longest_name_length = |
| 93 | + rows2.collect { |row2| row2['column_name'].size }.max + 1 |
| 94 | + rows2.each_with_index do |row2, i| |
| 95 | + puts sprintf("| Type of %-#{longest_name_length}s %s %s", |
| 96 | + row2['column_name'] + ':', |
| 97 | + row2['data_type'], |
| 98 | + row2['is_nullable'] ? '' : 'not null') |
| 99 | + end |
| 100 | + puts '' # to separate schema from data |
| 101 | + |
| 102 | + sql = "select * from #{table_name};" |
| 103 | + rows2 = ActiveRecord::Base.connection.execute(sql) |
| 104 | + if rows2.first |
| 105 | + keys = rows2.first.keys |
| 106 | + longest_key_length = keys.collect { |key| key.size }.max |
| 107 | + rows2.each_with_index do |row2, i| |
| 108 | + puts '' unless i == 0 # separation between rows |
| 109 | + keys.each do |key| |
| 110 | + value = row2[key] |
| 111 | + value = 'NULL' if value.nil? |
| 112 | + puts sprintf("%#{longest_key_length}s: %s", key, value) |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + puts '_' * 75 |
| 118 | + end |
| 119 | +end |
0 commit comments