dba 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4212cdf60ed43e28c00a0bb815eabf6ff0912535713a27a425f077e71ef27f4d
4
- data.tar.gz: a3da3be24d5e75207eb239fd814307c02457d22b286f1de548b5429c830dec60
3
+ metadata.gz: 7a165d5ff98a815002bea9e1d54744639aba99bb1e678d66afe8743ffcb2fe30
4
+ data.tar.gz: dadb161b89dde586047a0a8b6aded72be9499db6c48a27c2e9c57c7172a9c1a9
5
5
  SHA512:
6
- metadata.gz: 84c04318233dc14cacafb8b5bd065fb1d44d713cbbf39f50463dd1c46e12bc49599560e637dc0179ccbcc44b8c2cc3bde45a9244daf5d28867a0d538762fe352
7
- data.tar.gz: 0a62d78ddcd245fc5d8781f3af94dda6b463f5dac11623fe11be546339de7bdedd8827d08b68db6da85a1fb377bf2316f724bb83ac54331bf850be7f557c4d62
6
+ metadata.gz: 5f8033d0911e384525fb846228840d4ff5c7527df5c5e0d64fedc6fc158e6e809b5d88ab57483592954e8313961cd23696ae030efe97b7f06a342bfbd40c8c7e
7
+ data.tar.gz: 461b1d4e5eb81151aa5350c2f09362da3d6a31a42c959edb48a5dbd97206a239c3f6b4ad8e166e4185df2e6569f63445443af30a41d1064f9679c8dae7aa2a0f
data/CHANGES.md CHANGED
@@ -1,3 +1,18 @@
1
+ # 2.0.0
2
+
3
+ * **Required ruby version is now 2.5.0**
4
+
5
+ * Fixed diff command
6
+
7
+ * Added select command
8
+
9
+ * The indexes command now shows which indexes are unique
10
+
11
+ * The schema command now shows which columns have array types
12
+
13
+ * The postgres database is now used by default; use `.env` or `config/database.yml` to specify a different database name
14
+
15
+
1
16
  # 1.1.0
2
17
 
3
18
  * Fixed sample command for postgres
@@ -6,6 +21,7 @@
6
21
 
7
22
  * Added load command for loading data from CSV, LDJSON, or YAML
8
23
 
24
+
9
25
  # 1.0.0
10
26
 
11
27
  * First version!
data/README.md CHANGED
@@ -27,6 +27,7 @@ You can connect to any database supported by [sequel](https://rubygems.org/gems/
27
27
  dba pull TABLE URL
28
28
  dba sample TABLE [COLUMN]
29
29
  dba schema [TABLE]
30
+ dba select TABLE COLUMN VALUE
30
31
  dba tables
31
32
 
32
33
 
data/dba.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'dba'
3
- s.version = '1.1.0'
3
+ s.version = '2.0.0'
4
4
  s.license = 'GPL-3.0'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ['Tim Craft']
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.description = 'Ruby command line tool for working with development databases'
10
10
  s.summary = 'See description'
11
11
  s.files = Dir.glob('lib/**/*.rb') + %w[CHANGES.md LICENSE.txt README.md dba.gemspec]
12
- s.required_ruby_version = '>= 2.3.0'
12
+ s.required_ruby_version = '>= 2.5.0'
13
13
  s.add_dependency('zeitwerk', '~> 2', '>= 2.2')
14
14
  s.add_dependency('sequel', '~> 5')
15
15
  s.add_dependency('pastel', '~> 0')
data/lib/dba.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2019-2020 TIMCRAFT
1
+ # Copyright (c) 2019-2021 TIMCRAFT
2
2
  #
3
3
  # This program is free software: you can redistribute it and/or modify
4
4
  # it under the terms of the GNU General Public License as published by
data/lib/dba/database.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'sequel'
4
4
  require 'yaml'
5
5
  require 'erb'
6
+ require 'socket'
6
7
 
7
8
  module DBA::Database
8
9
  extend self
@@ -31,7 +32,7 @@ module DBA::Database
31
32
  end
32
33
 
33
34
  if postgres_running?
34
- return "postgres://localhost:5432/#{postgres_database_name}"
35
+ return 'postgres://localhost:5432/postgres'
35
36
  end
36
37
 
37
38
  if path = Dir['*.sqlite3'].first
@@ -78,21 +79,8 @@ module DBA::Database
78
79
  args
79
80
  end
80
81
 
81
- def postgres_database_name
82
- heroku_app_name || working_directory_name
83
- end
84
-
85
- def heroku_app_name
86
- if `git config --get remote.heroku.url`.chomp =~ %r{(?<=[:/])([^:/]+)\.git\z}
87
- $1
88
- end
89
- end
90
-
91
- def working_directory_name
92
- File.basename(Dir.pwd)
93
- end
94
-
95
82
  def postgres_running?
96
- !`lsof -i:5432`.strip.empty?
83
+ Socket.tcp('localhost', 5432) { :connected }
84
+ rescue Errno::ECONNREFUSED
97
85
  end
98
86
  end
data/lib/dba/diff.rb CHANGED
@@ -8,7 +8,7 @@ class DBA::Diff < DBA::Command
8
8
 
9
9
  other_tables = other_database.tables
10
10
 
11
- diff tables, other_tables
11
+ printer.print_diff tables, other_tables
12
12
 
13
13
  tables &= other_tables # only diff columns/indexes for tables that exist in both databases
14
14
 
@@ -21,7 +21,7 @@ class DBA::Diff < DBA::Command
21
21
 
22
22
  def list_columns(database, tables)
23
23
  tables.inject([]) do |columns, table_name|
24
- columns + database.schema(table_name).map { |name, info| format_column(name, info) }
24
+ columns + database.schema(table_name).map { |name, info| format_column(name, info, table_name) }
25
25
  end
26
26
  end
27
27
 
@@ -31,7 +31,7 @@ class DBA::Diff < DBA::Command
31
31
  end
32
32
  end
33
33
 
34
- def format_column(name, info_hash)
34
+ def format_column(name, info_hash, table_name)
35
35
  "#{table_name}.#{name} (#{info_hash.fetch(:type)})"
36
36
  end
37
37
 
data/lib/dba/printer.rb CHANGED
@@ -57,8 +57,8 @@ class DBA::Printer
57
57
  schema_hash.each do |column_name, info_hash|
58
58
  fields = []
59
59
  fields << "#{table_name}.#{column_name}"
60
- fields << muted(info_hash[:type] || info_hash[:db_type])
61
- fields << muted('(primary key)') if info_hash[:primary_key]
60
+ fields << muted(format_column_type(info_hash))
61
+ fields << muted('{primary}') if info_hash[:primary_key]
62
62
 
63
63
  io.puts fields.join(' ')
64
64
  end
@@ -68,9 +68,12 @@ class DBA::Printer
68
68
 
69
69
  def print_indexes(indexes)
70
70
  indexes.each do |index_name, info_hash|
71
- columns = info_hash.fetch(:columns).map(&:to_s).join(', ')
71
+ fields = []
72
+ fields << index_name
73
+ fields << muted('(' + info_hash.fetch(:columns).map(&:to_s).join(', ') + ')')
74
+ fields << muted('{unique}') if info_hash[:unique]
72
75
 
73
- io.puts "#{index_name} (#{columns})"
76
+ io.puts fields.join(' ')
74
77
  end
75
78
  end
76
79
 
@@ -96,6 +99,12 @@ class DBA::Printer
96
99
  end
97
100
  end
98
101
 
102
+ def format_column_type(info_hash)
103
+ return info_hash[:db_type] unless info_hash[:type]
104
+ return info_hash[:type] unless info_hash[:db_type]&.end_with?('[]')
105
+ info_hash[:type].to_s + '[]'
106
+ end
107
+
99
108
  def null
100
109
  @null ||= muted('NULL')
101
110
  end
data/lib/dba/select.rb ADDED
@@ -0,0 +1,14 @@
1
+ class DBA::Select < DBA::Command
2
+ def call(table, column, value)
3
+ self.table_name = table
4
+
5
+ column_name = column.to_sym
6
+
7
+ rows = database[table_name].where(column_name => value)
8
+
9
+ rows.each do |row|
10
+ printer.print(row)
11
+ printer.print_line
12
+ end
13
+ end
14
+ end
data/lib/dba/shell.rb CHANGED
@@ -43,6 +43,7 @@ module DBA::Shell
43
43
  'pull' => :Pull,
44
44
  'sample' => :Sample,
45
45
  'schema' => :Schema,
46
+ 'select' => :Select,
46
47
  'tables' => :Tables
47
48
  }
48
49
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dba
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Craft
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2021-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -89,6 +89,7 @@ files:
89
89
  - lib/dba/row_parser.rb
90
90
  - lib/dba/sample.rb
91
91
  - lib/dba/schema.rb
92
+ - lib/dba/select.rb
92
93
  - lib/dba/shell.rb
93
94
  - lib/dba/table_command.rb
94
95
  - lib/dba/table_schema.rb
@@ -106,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
107
  requirements:
107
108
  - - ">="
108
109
  - !ruby/object:Gem::Version
109
- version: 2.3.0
110
+ version: 2.5.0
110
111
  required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  requirements:
112
113
  - - ">="