rackdb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4d345d254f1c2ad27b886c988a50d462a4c9270c
4
+ data.tar.gz: 18fbfef83c48dc7f0a7324222487a04a4050c1dd
5
+ SHA512:
6
+ metadata.gz: 475f10e4c91265d9a42c9002160e71cb840a59593ad48fa42657b3c64fe3d1964778574bdef5a2ea3282533faebeec9612e17861c095be54c3a244f85d750d80
7
+ data.tar.gz: 1eaec56cd7652b9ae32f55c7d4648089ad05093ab8b84018e8d1fb180802a40847a717ace15b7a06aade5d424c3aa9305e92c8d525049433f05a4ef29eac3333
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 / 2016-04-14
2
+
3
+ * Initial release, based upon [`racksh`](https://github.com/sickill/racksh) and the Ruby on Rails `dbconsole` [code](https://github.com/rails/rails/blob/master/railties/lib/rails/commands/dbconsole.rb) (as of Railties v4.2.6).
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2016 Andrew Hodgkinson (Hipposoft)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # `rackdb`
2
+
3
+ ## About
4
+
5
+ **rackdb** is a database console for Ruby applications that run on Rack, which follow the Rails-like convention of a `config/database.yml` file describing the database connection parameters. This includes [Rails applications](http://rubyonrails.org) and [Hoodoo services](http://hoodoo.cloud/).
6
+
7
+ It is based upon [`racksh`](https://github.com/sickill/racksh) and the Ruby on Rails `dbconsole` [code](https://github.com/rails/rails/blob/master/railties/lib/rails/commands/dbconsole.rb). For more information, see the Rails documentation for `dbconsole`.
8
+
9
+ ## Installation
10
+
11
+ gem install rackdb
12
+
13
+ ## Usage
14
+
15
+ To open a default shell:
16
+
17
+ cd some_application_or_service
18
+ bundle exec rackdb
19
+
20
+ For help:
21
+
22
+ bundle exec rackdb --help
23
+
24
+ Quick access to specific environment database consoles:
25
+
26
+ bundle exec rackdb <environment_name>
27
+
28
+ ## Bugs & feature requests
29
+
30
+ Please use the [GitHub issue tracker](https://github.com/pond/rackdb/issues).
31
+
32
+ ## Authors
33
+
34
+ * Created by Andrew Hodgkinson - [http://pond.org.uk/](http://pond.org.uk/)
35
+ * Derives from:
36
+ * https://github.com/sickill/racksh
37
+ * https://github.com/rails/rails/blob/master/railties/lib/rails/commands/dbconsole.rb
data/bin/rackdb ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(
4
+ File.join(
5
+ File.dirname( __FILE__),
6
+ '..', 'lib', 'rackdb', 'init.rb'
7
+ )
8
+ )
9
+
10
+ # Prevent STDOUT and STDERR from being reopened (some applications do this
11
+ # to be able to log under Passenger).
12
+ #
13
+ def STDOUT.reopen(*args); end
14
+ def STDERR.reopen(*args); end
15
+
16
+ RackDB::Console.start()
17
+ p eval( ARGV.join( ' ' ) ) unless ARGV.empty?
@@ -0,0 +1,204 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+ require 'optparse'
4
+ require 'rbconfig'
5
+ require 'active_record'
6
+
7
+ module Service
8
+ include ActiveSupport::Configurable
9
+ end
10
+
11
+ module RackDB
12
+ class Console
13
+ attr_reader :arguments
14
+
15
+ def self.start
16
+ new.start
17
+ end
18
+
19
+ def initialize(arguments = ARGV)
20
+ @arguments = arguments
21
+ end
22
+
23
+ def start
24
+ options = parse_arguments(arguments)
25
+ exit if options.nil?
26
+
27
+ ENV['RACK_ENV'] = options[:environment] || environment()
28
+ Service.config.env = environment()
29
+
30
+ case config["adapter"]
31
+ when /^(jdbc)?mysql/
32
+ args = {
33
+ 'host' => '--host',
34
+ 'port' => '--port',
35
+ 'socket' => '--socket',
36
+ 'username' => '--user',
37
+ 'encoding' => '--default-character-set',
38
+ 'sslca' => '--ssl-ca',
39
+ 'sslcert' => '--ssl-cert',
40
+ 'sslcapath' => '--ssl-capath',
41
+ 'sslcipher' => '--ssl-cipher',
42
+ 'sslkey' => '--ssl-key'
43
+ }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
44
+
45
+ if config['password'] && options['include_password']
46
+ args << "--password=#{config['password']}"
47
+ elsif config['password'] && !config['password'].to_s.empty?
48
+ args << "-p"
49
+ end
50
+
51
+ args << config['database']
52
+
53
+ find_cmd_and_exec(['mysql', 'mysql5'], *args)
54
+
55
+ when /^postgres|^postgis/
56
+ ENV['PGUSER'] = config["username"] if config["username"]
57
+ ENV['PGHOST'] = config["host"] if config["host"]
58
+ ENV['PGPORT'] = config["port"].to_s if config["port"]
59
+ ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && options['include_password']
60
+ find_cmd_and_exec('psql', config["database"])
61
+
62
+ when "sqlite"
63
+ find_cmd_and_exec('sqlite', config["database"])
64
+
65
+ when "sqlite3"
66
+ args = []
67
+
68
+ args << "-#{options['mode']}" if options['mode']
69
+ args << "-header" if options['header']
70
+ args << File.expand_path(config['database'], root())
71
+
72
+ find_cmd_and_exec('sqlite3', *args)
73
+
74
+ when "oracle", "oracle_enhanced"
75
+ logon = ""
76
+
77
+ if config['username']
78
+ logon = config['username']
79
+ logon << "/#{config['password']}" if config['password'] && options['include_password']
80
+ logon << "@#{config['database']}" if config['database']
81
+ end
82
+
83
+ find_cmd_and_exec('sqlplus', logon)
84
+
85
+ when "sqlserver"
86
+ args = []
87
+
88
+ args += ["-D", "#{config['database']}"] if config['database']
89
+ args += ["-U", "#{config['username']}"] if config['username']
90
+ args += ["-P", "#{config['password']}"] if config['password']
91
+
92
+ if config['host']
93
+ host_arg = "#{config['host']}"
94
+ host_arg << ":#{config['port']}" if config['port']
95
+ args += ["-S", host_arg]
96
+ end
97
+
98
+ find_cmd_and_exec("sqsh", *args)
99
+
100
+ else
101
+ abort "Unknown command-line client for #{config['database']}."
102
+ end
103
+ end
104
+
105
+ def config
106
+ @config ||= begin
107
+ if configurations()[environment].blank?
108
+ raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations().inspect}"
109
+ else
110
+ configurations()[environment]
111
+ end
112
+ end
113
+ end
114
+
115
+ def environment
116
+ ENV["RACK_ENV"] || "development"
117
+ end
118
+
119
+ protected
120
+
121
+ def root
122
+ if defined?( Application ) && Application.respond_to?( :root )
123
+ Application.root # Rails-like
124
+ elsif Rack.respond_to?( :root )
125
+ Rack.root # Hypothetical
126
+ elsif defined?( Service ) && Service.respond_to?( :config ) && Service.config.respond_to?( :root ) && Service.config.root != nil
127
+ Service.config.root # Hoodoo service
128
+ else
129
+ '.'
130
+ end
131
+ end
132
+
133
+ def configurations
134
+ ActiveRecord::Base.default_timezone = :utc
135
+
136
+ path = File.join( root(), 'config', 'database.yml' )
137
+ erb_yaml_file = File.read( path )
138
+ pure_yaml_file = ERB.new( erb_yaml_file ).result
139
+
140
+ ActiveRecord::Base.configurations = YAML.load( pure_yaml_file )
141
+ return ActiveRecord::Base.configurations
142
+ end
143
+
144
+ def parse_arguments(arguments)
145
+ options = {}
146
+
147
+ OptionParser.new do |opt|
148
+ opt.banner = "Usage: rackdb [environment] [options]"
149
+ opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
150
+ options['include_password'] = true
151
+ end
152
+
153
+ opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
154
+ "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
155
+ options['mode'] = mode
156
+ end
157
+
158
+ opt.on("--header", "Turn on sqlite3 database command line header display.") do |h|
159
+ options['header'] = h
160
+ end
161
+
162
+ opt.on("-h", "--help", "Show this help message.") do
163
+ puts opt
164
+ return nil
165
+ end
166
+
167
+ opt.on("-e", "--environment=name", String,
168
+ "Specifies the environment to run this console under (test/development/production).",
169
+ "Default: development"
170
+ ) { |v| options[:environment] = v.strip }
171
+
172
+ opt.parse!(arguments)
173
+ abort opt.to_s unless (0..1).include?(arguments.size)
174
+ end
175
+
176
+ if arguments.first && arguments.first[0] != '-'
177
+ options[:environment] = arguments.first
178
+ end
179
+
180
+ options
181
+ end
182
+
183
+ def find_cmd_and_exec(commands, *args)
184
+ commands = Array(commands)
185
+
186
+ dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
187
+ commands += commands.map{|cmd| "#{cmd}.exe"} if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
188
+
189
+ full_path_command = nil
190
+ found = commands.detect do |cmd|
191
+ dirs_on_path.detect do |path|
192
+ full_path_command = File.join(path, cmd)
193
+ File.executable? full_path_command
194
+ end
195
+ end
196
+
197
+ if found
198
+ exec full_path_command, *args
199
+ else
200
+ abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
201
+ end
202
+ end
203
+ end
204
+ end
@@ -0,0 +1,3 @@
1
+ module RackDB
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rackdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Hodgkinson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-mocks
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ description: Database console for Ruby applications running on Rack with ActiveRecord
84
+ and following the Rails "config/database.yml" database configuration pattern
85
+ email: ahodgkin@rowing.org.uk
86
+ executables:
87
+ - rackdb
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - CHANGELOG.md
92
+ - LICENSE
93
+ - README.md
94
+ - bin/rackdb
95
+ - lib/rackdb/console.rb
96
+ - lib/rackdb/version.rb
97
+ homepage: http://github.com/pond/rackdb
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '2.1'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.5.1
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Database console for Rack applications
121
+ test_files: []