db_admin 0.1.5 → 0.1.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f8ca91c5487bf88b9be3339e6535a869a292f32b6dd99f762b40a53b7497524
4
- data.tar.gz: d9a7108368da315d9af88b852167895d6548988e72291398a1e77067067fb7f9
3
+ metadata.gz: b9334ebb91f433dfa6036c5126cfd0dcb94eb2b85dfba0398206a27b4c8cb808
4
+ data.tar.gz: c5c8f15ebff96a75d13df95ac6c802ffc67209f54a2f787288a7c855ec5c1a31
5
5
  SHA512:
6
- metadata.gz: 41a830abc163b9cec042f70b65a1e4af57ad3a690448d04c57b1dad9576286675f84701dbdc38d66baed0e452f75e4bfa01b9e8a6af6cf6c67be9d34908bef36
7
- data.tar.gz: 7ce005a8a1def70323f57f456fc2282b71716851149b521e05822f78842df7a590da7561e21b3716bd3713eab9cd86e481b78789dc990bc02449f4003ddad678
6
+ metadata.gz: 4fa406fe61284e9e171aab51f69630ae2acbff65a2acc5ff0b6b140c2cbd59b3feea9da28e86cc4e9c87d8d39de671acff450690f47ba0992421f3af3adee206
7
+ data.tar.gz: 342f9253078c69c90913b393afd26b5422dc843485ea272d2791c6eda914e583c511bf609a6ecef111d5130313c9f79f486d102552ab83703dce4c4b1df48dbc
data/README.md CHANGED
@@ -2,7 +2,9 @@
2
2
  A gem based on [Sinatra](https://github.com/sinatra/sinatra) and [Sequel](https://github.com/jeremyevans/sequel).
3
3
  You can use it to manage multiple databases at the same time.
4
4
 
5
- Rails user can also use it because it is just a gem. Here are some demo pictures.
5
+ For Rails user, this gem will treat `./config/database.yml` as database configuration file.
6
+
7
+ Here are some demo pictures.
6
8
 
7
9
  ![Demo home](./lib/public/demo_home.png)
8
10
 
@@ -11,14 +13,15 @@ Rails user can also use it because it is just a gem. Here are some demo pictures
11
13
  ![Demo hover](./lib/public/demo_hover.png)
12
14
 
13
15
  ## Installation
14
- **The best way** to use this gem is **`gem install db_admin`**.
15
-
16
+ ~~~ruby
17
+ gem install db_admin
18
+ ~~~
16
19
  If you prefer to use it in `Gemfile`:
17
20
  ~~~ruby
18
21
  group :development do
19
22
  # A Web UI for database admin. Run `$ db_admin` and then
20
23
  # visit http://localhost:4567
21
- gem 'db_admin', require: false # Don't require the code because you needn't it.
24
+ gem 'db_admin', require: false # You needn't require the code.
22
25
  end
23
26
  ~~~
24
27
 
@@ -31,6 +34,49 @@ $ db_admin # Run this command to start a Sinatra Web.
31
34
  ~~~
32
35
  Visit [http://localhost:4567](http://localhost:4567/)
33
36
 
37
+ ### Connect to database
38
+ There are two ways for you to connect to a database.
39
+
40
+ 1) Visit [http://localhost:4567](http://localhost:4567/) . The homepage is asking you to connect to a database. Please look at the demo picture above.
41
+
42
+ 2) `touch ./config/database.yml` or `touch ./database.yml`
43
+
44
+ You can use Rails's `database.yml` file directly.
45
+
46
+ In yaml, 'Omit the key' or 'keep value blank' means use the default value.
47
+ ~~~yaml
48
+ # 'database.yml' for Rails style example 1. We only connect to the 'development' database!
49
+ development:
50
+ adapter: postgresql
51
+ encoding: unicode
52
+ host: localhost
53
+ database: somthing_development
54
+ username: username
55
+ password:
56
+ port: 5432
57
+ ~~~
58
+ ~~~yaml
59
+ # 'database.yml' for Rails style example 2. Inherited style is also supported.
60
+ default: &default
61
+ adapter: postgresql
62
+ # Below omitted ...
63
+
64
+ development:
65
+ <<: *default
66
+ database: somthing_development
67
+ # Below omitted ...
68
+ ~~~
69
+ ~~~yaml
70
+ # 'database.yml' for none-Rails style example.
71
+ adapter: postgres
72
+ encoding: unicode
73
+ host: localhost
74
+ database: your_database_name
75
+ user: username
76
+ password:
77
+ port: 5432
78
+ ~~~
79
+
34
80
  ## Customizing
35
81
  If you want to change some code for your own purpose, please
36
82
  ~~~bash
data/bin/db_admin CHANGED
@@ -2,4 +2,29 @@
2
2
 
3
3
  require 'db_admin'
4
4
 
5
+ module DBAdmin
6
+ class Web
7
+ yml_hash = database_config_yml_hash
8
+
9
+ unless yml_hash.empty?
10
+ begin
11
+ db = Sequel.connect(yml_hash)
12
+
13
+ if db.test_connection
14
+ DB = db
15
+ DBs << DB
16
+ puts "==== Connect to the database defined in your config file './config/database.yml' or './database.yml' succeed."
17
+ end
18
+ rescue Sequel::AdapterNotFound => e
19
+ puts "==== Warning: An exception occurred when trying to connect to the database defined in your config file './config/database.yml' or './database.yml' \n" +
20
+ "==== #{e.message} \n" +
21
+ "==== You need to install the database driver gem first"
22
+ rescue Exception => e
23
+ puts "==== Warning: An exception occurred when trying to connect to the database defined in your config file './config/database.yml' or './database.yml' \n" +
24
+ "==== #{e.message}"
25
+ end
26
+ end
27
+ end
28
+ end
29
+
5
30
  DBAdmin::Web.run!
data/lib/db_admin.rb CHANGED
@@ -10,19 +10,54 @@ module DBAdmin
10
10
 
11
11
  DBs = []
12
12
 
13
- # Direct connect. 'adapter' can be 'postgres', 'mysql2', 'sqlite', 'oracle', etc.
14
- # DB = Sequel.connect({ adapter: 'postgres',
15
- # host: 'hostname_or_ip',
16
- # database: 'database_name',
17
- # user: 'user',
18
- # password: '' }); DBs << DB
13
+ # Load Rails './config/database.yml' or './database.yml'
14
+ def self.database_config_yml_hash
15
+ db_hash = load_database_yml
16
+
17
+ return {} if db_hash.empty?
18
+
19
+ db_hash = db_hash['development'] if db_hash['development'].is_a?(Hash)
20
+
21
+ return {} if ['adapter', 'host', 'database'].any? { |item| db_hash[item].to_s.empty? }
22
+
23
+ return db_hash
24
+ .slice('adapter', 'host', 'database', 'username', 'password', 'encoding', 'port')
25
+ .transform_keys { |key| key == 'username' ? :user : key.to_sym }
26
+ .transform_values { |value| convert_rails_adapter(value) }
27
+ end
28
+
29
+ def self.load_database_yml
30
+ require 'yaml'
31
+
32
+ begin
33
+ YAML.load_file('config/database.yml')
34
+ rescue Errno::ENOENT
35
+ begin
36
+ YAML.load_file('database.yml')
37
+ rescue Errno::ENOENT
38
+ {}
39
+ end
40
+ end
41
+ end
42
+
43
+ def self.convert_rails_adapter(adapter)
44
+ hash = { 'postgresql' => 'postgres', 'sqlite3' => 'sqlite', 'sqlserver' => 'tinytds' }
45
+
46
+ return hash[adapter] if hash.keys.include?(adapter)
47
+
48
+ return adapter
49
+ end
19
50
 
20
51
  set :bind, '0.0.0.0'
21
52
 
22
53
  enable :sessions
23
54
 
24
55
  get '/' do
25
- erb :index, layout: :layout_index
56
+ if DBs.size > 0
57
+ erb :home
58
+ else
59
+ erb :index, layout: :layout_index
60
+ end
26
61
  end
27
62
 
28
63
  get '/home' do
@@ -55,7 +90,7 @@ module DBAdmin
55
90
  DBs << DB
56
91
  end
57
92
  rescue Sequel::AdapterNotFound => e
58
- session[:error] = "#{e.message} \n You need to install the database driver gem first.\n Please uncomment the driver gem in 'Gemfile' and run `$ bundle install`."
93
+ session[:error] = "#{e.message} \n You need to install the database driver gem first."
59
94
  rescue Exception => e
60
95
  session[:error] = e.message
61
96
  end
@@ -1,3 +1,14 @@
1
1
  require_relative '../db_admin'
2
2
 
3
+ module DBAdmin
4
+ class Web
5
+ # Uncomment the line below to direct connect to a database.
6
+ # DB = Sequel.connect({ adapter: 'postgres', # 'adapter' can be 'postgres', 'mysql2', 'sqlite', 'oracle', etc.
7
+ # host: 'hostname_or_ip',
8
+ # database: 'your_database_name',
9
+ # user: 'user',
10
+ # password: '' }); DBs << DB
11
+ end
12
+ end
13
+
3
14
  DBAdmin::Web.run!
@@ -1,43 +1,43 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
- <head>
4
- <meta charset='utf-8'>
5
- <meta name='viewport' content='width=device-width, initial-scale=1'>
6
- <title>Database</title>
7
- <link href='/css/bootstrap.min.css' rel='stylesheet'>
3
+ <head>
4
+ <meta charset='utf-8'>
5
+ <meta name='viewport' content='width=device-width, initial-scale=1'>
6
+ <title>Database</title>
7
+ <link href='/css/bootstrap.min.css' rel='stylesheet'>
8
8
 
9
- <script src='/js/jquery.min.js'></script>
10
- <script src='/js/bootstrap.min.js'></script>
9
+ <script src='/js/jquery.min.js'></script>
10
+ <script src='/js/bootstrap.min.js'></script>
11
11
 
12
- <style>
13
- select, button {
14
- margin-top: 5px;
15
- margin-bottom: 5px;
16
- }
12
+ <style>
13
+ select, button {
14
+ margin-top: 5px;
15
+ margin-bottom: 5px;
16
+ }
17
17
 
18
- h1 a:link { color: #000000; }
19
- h1 a:visited { color: #000000; }
20
- h1 a:hover { color: #4666FF; }
21
- </style>
22
- </head>
18
+ h1 a:link { color: #000000; }
19
+ h1 a:visited { color: #000000; }
20
+ h1 a:hover { color: #4666FF; }
21
+ </style>
22
+ </head>
23
23
 
24
- <body>
25
- <div class='container'>
26
- <div>
27
- <h1>
28
- <a href='/'>Ruby Database Admin</a>
29
- </h1>
30
- </div>
24
+ <body>
25
+ <div class='container'>
26
+ <div>
27
+ <h1>
28
+ <a href='/'>Ruby Database Admin</a>
29
+ </h1>
30
+ </div>
31
31
 
32
- <div class='row'>
33
- <div class='col-sm-2'>
34
- <%= erb :_recent_databases %>
35
- </div>
32
+ <div class='row'>
33
+ <div class='col-sm-2'>
34
+ <%= erb :_recent_databases %>
35
+ </div>
36
36
 
37
- <div class='col-sm-10 blog-main'>
38
- <%= yield %>
37
+ <div class='col-sm-10 blog-main'>
38
+ <%= yield %>
39
+ </div>
40
+ </div>
39
41
  </div>
40
- </div>
41
- </div>
42
- </body>
42
+ </body>
43
43
  </html>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lane Zhang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-03 00:00:00.000000000 Z
11
+ date: 2019-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra