Sha256: df9694a0ed3a013f70ba255dadc14dbdcd609c0631108db2b4b787306ce2c6f8

Contents?: true

Size: 1.07 KB

Versions: 23

Compression:

Stored size: 1.07 KB

Contents

#!/usr/bin/env ruby

require 'active_record'
require 'erb'
require 'yaml'
require 'terminal-table'

require_relative "../lib/rails_db"

debug = false

ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/rails_db/engine', __FILE__)

require "#{ENGINE_ROOT}/lib/ext/string_ext"

database_yml = ::File.expand_path(File.join('.', 'config', 'database.yml'))
app_path     = ::File.expand_path(File.join('.'))

db_conf   = YAML.load(ERB.new(File.open(database_yml).read).result) || {}
env       = ENV['RAILS_ENV'] || ENV['ENV'] || 'development'
db_env    = db_conf[env]

if db_env['adapter'] == 'sqlite3'
  db_env['database'] = "#{app_path}/#{db_env['database']}"
end

if debug
  puts "ENV: #{db_env.inspect}"
end

ActiveRecord::Base.establish_connection(db_env)

RailsDb.use_default_configuration!

sql = "#{ARGV[0]}".strip
puts "Executing: #{sql}".red

sql_query = RailsDb::SqlQuery.new(sql).execute

rows = sql_query.data.rows
cols = sql_query.data.columns

if rows.any?
  table = Terminal::Table.new rows: rows, headings: cols
  puts table
else
  puts []
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
rails_db-2.4.4 bin/runsql
rails_db-2.4.3 bin/runsql
rails_db-2.4.2 bin/runsql
rails_db-2.4.1 bin/runsql
rails_db-2.4.0 bin/runsql
rails_db-2.3.1 bin/runsql
rails_db-2.3.0 bin/runsql
rails_db-2.2.1 bin/runsql
rails_db-2.2.0 bin/runsql
rails_db-2.1.1 bin/runsql
rails_db-2.1.0 bin/runsql
rails_db-2.0.6 bin/runsql
rails_db-2.0.5 bin/runsql
rails_db-2.0.4 bin/runsql
rails_db-2.0.3 bin/runsql
rails_db-2.0.2 bin/runsql
rails_db-2.0.1 bin/runsql
rails_db-2.0.0 bin/runsql
rails_db-1.6.0 bin/runsql
rails_db-1.5.0 bin/runsql