dbrunner 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +74 -0
- data/lib/dbrunner/version.rb +3 -0
- data/lib/dbrunner.rb +3 -0
- data/lib/rails/commands/dbrunner/dbrunner_command.rb +137 -0
- metadata +72 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4bf5a4c41deeb515230a7178d9ae3bb1876e8fffef64932d4911005d3e1fa76c
|
|
4
|
+
data.tar.gz: '020932585333a7a27deb2a7fba7d18503ae29c2e02ef447966ac2a687ffcfc69'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 153fc5bd1f58f7faee1b36e0da8111b5a9dffb48e7244f950794fbafe3d0f0888d2f3a008588f47e6d147fe98fb8bf8c12bcde618a86ea07598453d99969ff4d
|
|
7
|
+
data.tar.gz: 2c5354a75b3e0860cc9c2f5eb8653666641f9e51b1cdea20b8dfe44a812a2efb9c261ffc46221c2d290d5179cdb927c89632e1070a919fd353eb88257a9e6ba8
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Josh Brody
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# dbrunner
|
|
2
|
+
|
|
3
|
+
`rails dbrunner` — run SQL like `rails runner` runs Ruby.
|
|
4
|
+
|
|
5
|
+
Rails has `runner`, `console`, and `dbconsole`. Now it has `dbrunner`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "dbrunner"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# inline SQL
|
|
17
|
+
bin/rails dbrunner "SELECT * FROM users LIMIT 5"
|
|
18
|
+
|
|
19
|
+
# from a file
|
|
20
|
+
bin/rails dbrunner query.sql
|
|
21
|
+
|
|
22
|
+
# from stdin
|
|
23
|
+
echo "SELECT 1" | bin/rails dbrunner -
|
|
24
|
+
|
|
25
|
+
# JSON output
|
|
26
|
+
bin/rails dbrunner -f json "SELECT * FROM users LIMIT 5"
|
|
27
|
+
|
|
28
|
+
# CSV output
|
|
29
|
+
bin/rails dbrunner -f csv "SELECT * FROM users"
|
|
30
|
+
|
|
31
|
+
# specific database (multi-db)
|
|
32
|
+
bin/rails dbrunner --db secondary "SELECT 1"
|
|
33
|
+
|
|
34
|
+
# specific environment
|
|
35
|
+
bin/rails dbrunner -e production "SELECT count(*) FROM users"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Output formats
|
|
39
|
+
|
|
40
|
+
**Table** (default):
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
id | email | created_at
|
|
44
|
+
---+--------------------+--------------------------
|
|
45
|
+
1 | alice@example.com | 2026-01-15 08:30:00 UTC
|
|
46
|
+
2 | bob@example.com | 2026-02-20 14:15:00 UTC
|
|
47
|
+
|
|
48
|
+
2 row(s)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**JSON** (`-f json`):
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
[
|
|
55
|
+
{ "id": 1, "email": "alice@example.com" },
|
|
56
|
+
{ "id": 2, "email": "bob@example.com" }
|
|
57
|
+
]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**CSV** (`-f csv`):
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
id,email
|
|
64
|
+
1,alice@example.com
|
|
65
|
+
2,bob@example.com
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## How it works
|
|
69
|
+
|
|
70
|
+
The gem registers a Rails command at `rails/commands/dbrunner/dbrunner_command.rb`. Rails discovers it automatically through its command lookup paths — no railtie or initializer needed. It uses `ActiveRecord::Base.lease_connection` to execute queries through your existing database configuration.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT
|
data/lib/dbrunner.rb
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/command/environment_argument"
|
|
4
|
+
|
|
5
|
+
module Rails
|
|
6
|
+
module Command
|
|
7
|
+
class DbrunnerCommand < Base # :nodoc:
|
|
8
|
+
include EnvironmentArgument
|
|
9
|
+
|
|
10
|
+
class_option :database, aliases: "--db", type: :string,
|
|
11
|
+
desc: "Specify the database to use."
|
|
12
|
+
|
|
13
|
+
class_option :format, aliases: "-f", type: :string, enum: %w[table csv json], default: "table",
|
|
14
|
+
desc: "Output format (table, csv, json)"
|
|
15
|
+
|
|
16
|
+
desc "dbrunner [<'SQL'> | <filename.sql> | -]",
|
|
17
|
+
"Run SQL in the context of your database"
|
|
18
|
+
def perform(sql_or_file = nil, *)
|
|
19
|
+
unless sql_or_file
|
|
20
|
+
help
|
|
21
|
+
exit 1
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if respond_to?(:boot_application!, true)
|
|
25
|
+
boot_application!
|
|
26
|
+
else
|
|
27
|
+
require_application_and_environment!
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
sql = resolve_sql(sql_or_file)
|
|
31
|
+
connection = resolve_connection
|
|
32
|
+
|
|
33
|
+
begin
|
|
34
|
+
result = connection.exec_query(sql)
|
|
35
|
+
rescue ::ActiveRecord::StatementInvalid => e
|
|
36
|
+
error e.message
|
|
37
|
+
exit 1
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
if result.columns.any?
|
|
41
|
+
render(result)
|
|
42
|
+
else
|
|
43
|
+
say "#{result.rows.length} row(s) affected"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
def resolve_sql(sql_or_file)
|
|
49
|
+
if sql_or_file == "-"
|
|
50
|
+
$stdin.read
|
|
51
|
+
elsif File.exist?(sql_or_file)
|
|
52
|
+
File.read(sql_or_file)
|
|
53
|
+
else
|
|
54
|
+
sql_or_file
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def resolve_connection
|
|
59
|
+
require APP_PATH
|
|
60
|
+
::ActiveRecord::Base.configurations = ::Rails.application.config.database_configuration
|
|
61
|
+
|
|
62
|
+
env = ::Rails::Command.environment
|
|
63
|
+
|
|
64
|
+
if options[:database]
|
|
65
|
+
configs_for_options = { env_name: env, name: options[:database] }
|
|
66
|
+
configs_for_options[:include_hidden] = true if Gem::Version.new(::ActiveRecord::VERSION::STRING) >= Gem::Version.new("7.1")
|
|
67
|
+
db_config = ::ActiveRecord::Base.configurations.configs_for(**configs_for_options)
|
|
68
|
+
|
|
69
|
+
unless db_config
|
|
70
|
+
raise ::ActiveRecord::AdapterNotSpecified,
|
|
71
|
+
"'#{options[:database]}' database is not configured for '#{env}'."
|
|
72
|
+
end
|
|
73
|
+
else
|
|
74
|
+
db_config = ::ActiveRecord::Base.configurations.find_db_config(env)
|
|
75
|
+
|
|
76
|
+
unless db_config
|
|
77
|
+
raise ::ActiveRecord::AdapterNotSpecified,
|
|
78
|
+
"No database configured for '#{env}'."
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
::ActiveRecord::Base.establish_connection(db_config)
|
|
83
|
+
|
|
84
|
+
if ::ActiveRecord::Base.respond_to?(:lease_connection)
|
|
85
|
+
::ActiveRecord::Base.lease_connection
|
|
86
|
+
else
|
|
87
|
+
::ActiveRecord::Base.connection
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def render(result)
|
|
92
|
+
case options[:format]
|
|
93
|
+
when "json"
|
|
94
|
+
render_json(result)
|
|
95
|
+
when "csv"
|
|
96
|
+
render_csv(result)
|
|
97
|
+
else
|
|
98
|
+
render_table(result)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def render_json(result)
|
|
103
|
+
require "json"
|
|
104
|
+
rows = result.rows.map { |row| result.columns.zip(row).to_h }
|
|
105
|
+
say JSON.pretty_generate(rows)
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def render_csv(result)
|
|
109
|
+
require "csv"
|
|
110
|
+
say CSV.generate { |csv|
|
|
111
|
+
csv << result.columns
|
|
112
|
+
result.rows.each { |row| csv << row }
|
|
113
|
+
}
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def render_table(result)
|
|
117
|
+
columns = result.columns
|
|
118
|
+
rows = result.rows.map { |row| row.map { |v| v.nil? ? "NULL" : v.to_s } }
|
|
119
|
+
|
|
120
|
+
widths = columns.each_with_index.map do |col, i|
|
|
121
|
+
[col.length, *rows.map { |r| r[i].length }].max
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
header = columns.each_with_index.map { |col, i| col.ljust(widths[i]) }.join(" | ")
|
|
125
|
+
separator = widths.map { |w| "-" * w }.join("-+-")
|
|
126
|
+
|
|
127
|
+
say header
|
|
128
|
+
say separator
|
|
129
|
+
rows.each do |row|
|
|
130
|
+
say row.each_with_index.map { |val, i| val.ljust(widths[i]) }.join(" | ")
|
|
131
|
+
end
|
|
132
|
+
say ""
|
|
133
|
+
say "#{rows.length} row(s)"
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dbrunner
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Josh Brody
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: railties
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '6.1'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '6.1'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: activerecord
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '6.1'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '6.1'
|
|
40
|
+
description: Adds a `rails dbrunner` command that executes SQL against your database.
|
|
41
|
+
Accepts inline SQL, .sql files, or stdin. Outputs as table, CSV, or JSON.
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- LICENSE.txt
|
|
47
|
+
- README.md
|
|
48
|
+
- lib/dbrunner.rb
|
|
49
|
+
- lib/dbrunner/version.rb
|
|
50
|
+
- lib/rails/commands/dbrunner/dbrunner_command.rb
|
|
51
|
+
homepage: https://github.com/joshmn/dbrunner
|
|
52
|
+
licenses:
|
|
53
|
+
- MIT
|
|
54
|
+
metadata: {}
|
|
55
|
+
rdoc_options: []
|
|
56
|
+
require_paths:
|
|
57
|
+
- lib
|
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
59
|
+
requirements:
|
|
60
|
+
- - ">="
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '3.0'
|
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
requirements: []
|
|
69
|
+
rubygems_version: 4.0.1
|
|
70
|
+
specification_version: 4
|
|
71
|
+
summary: rails dbrunner — run SQL like rails runner runs Ruby
|
|
72
|
+
test_files: []
|