dba 1.1.0 → 2.1.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: 6d8a51736a883e5fa3608fd0429dbf3a819f2903a2da62d1df26d234952c899d
4
+ data.tar.gz: bcdb786558aab8e9235324eeb06e073bff126c73abd680213f030d8560cc5306
5
5
  SHA512:
6
- metadata.gz: 84c04318233dc14cacafb8b5bd065fb1d44d713cbbf39f50463dd1c46e12bc49599560e637dc0179ccbcc44b8c2cc3bde45a9244daf5d28867a0d538762fe352
7
- data.tar.gz: 0a62d78ddcd245fc5d8781f3af94dda6b463f5dac11623fe11be546339de7bdedd8827d08b68db6da85a1fb377bf2316f724bb83ac54331bf850be7f557c4d62
6
+ metadata.gz: 5fefa4be5caa1d897e9a3e330b6535351f5f503cc4b351e37cf8a7ef8b30f93555491bf5c09836c8b43f13c503d3783bb7e9173a2a4c2ca696bc8b5b1c903379
7
+ data.tar.gz: c6aea9949fe0b38718bee070cc3772b57b1ecb060b89c6fd497baa27dc30a0b2ed0edbc9b51157d8373ca057fbd85cb0a858f7f7665b3ba94bb99bcee095cc83
data/CHANGES.md CHANGED
@@ -1,3 +1,27 @@
1
+ # 2.1.0
2
+
3
+ * Fixed arity check for commands with optional arguments
4
+
5
+ * Added support for .jsonl extension to dump and load commands
6
+
7
+ * Added support for .ndjson extension to dump and load commands
8
+
9
+
10
+ # 2.0.0
11
+
12
+ * **Required ruby version is now 2.5.0**
13
+
14
+ * Fixed diff command
15
+
16
+ * Added select command
17
+
18
+ * The indexes command now shows which indexes are unique
19
+
20
+ * The schema command now shows which columns have array types
21
+
22
+ * The postgres database is now used by default; use `.env` or `config/database.yml` to specify a different database name
23
+
24
+
1
25
  # 1.1.0
2
26
 
3
27
  * Fixed sample command for postgres
@@ -6,6 +30,7 @@
6
30
 
7
31
  * Added load command for loading data from CSV, LDJSON, or YAML
8
32
 
33
+
9
34
  # 1.0.0
10
35
 
11
36
  * 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.1.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/command.rb CHANGED
@@ -1,4 +1,16 @@
1
1
  class DBA::Command
2
+ def self.arity_check(args)
3
+ parameters = instance_method(:call).parameters
4
+
5
+ required, optional = parameters.partition { |(type, name)| type == :req }
6
+
7
+ expected = optional.empty? ? required.size : (required.size .. required.size + optional.size)
8
+
9
+ unless expected === args.size
10
+ raise DBA::Error, "incorrect number of arguments (given #{args.size}, expected #{expected})"
11
+ end
12
+ end
13
+
2
14
  def initialize(database)
3
15
  self.database = database
4
16
  end
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
@@ -63,12 +64,16 @@ module DBA::Database
63
64
  File.exist?('.env')
64
65
  end
65
66
 
67
+ def database_config_path
68
+ File.join('config', 'database.yml')
69
+ end
70
+
66
71
  def database_config?
67
- File.exist?('config/database.yml')
72
+ File.exist?(database_config_path)
68
73
  end
69
74
 
70
75
  def database_config
71
- YAML.load(ERB.new(File.read('config/database.yml')).result)
76
+ YAML.load(ERB.new(File.read(database_config_path)).result)
72
77
  end
73
78
 
74
79
  def development_database_args
@@ -78,21 +83,8 @@ module DBA::Database
78
83
  args
79
84
  end
80
85
 
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
86
  def postgres_running?
96
- !`lsof -i:5432`.strip.empty?
87
+ Socket.tcp('localhost', 5432) { :connected }
88
+ rescue Errno::ECONNREFUSED
97
89
  end
98
90
  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/dump.rb CHANGED
@@ -3,7 +3,9 @@
3
3
  class DBA::Dump < DBA::Command
4
4
  ADAPTERS = {
5
5
  'csv' => :CSV,
6
+ 'jsonl' => :LDJSON,
6
7
  'ldjson' => :LDJSON,
8
+ 'ndjson' => :LDJSON,
7
9
  'yml' => :YAML,
8
10
  'yaml' => :YAML
9
11
  }
data/lib/dba/load.rb CHANGED
@@ -3,7 +3,9 @@
3
3
  class DBA::Load < DBA::Command
4
4
  ADAPTERS = {
5
5
  '.csv' => :CSV,
6
+ '.jsonl' => :LDJSON,
6
7
  '.ldjson' => :LDJSON,
8
+ '.ndjson' => :LDJSON,
7
9
  '.yml' => :YAML,
8
10
  '.yaml' => :YAML
9
11
  }
@@ -26,7 +28,7 @@ class DBA::Load < DBA::Command
26
28
 
27
29
  def file_list(path)
28
30
  if File.directory?(path)
29
- Dir.glob(File.join(path, '*.{csv,ldjson,yml,yaml}'))
31
+ Dir.glob(File.join(path, '*.{csv,jsonl,ldjson,ndjson,yml,yaml}'))
30
32
  else
31
33
  [path]
32
34
  end
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
@@ -16,11 +16,7 @@ module DBA::Shell
16
16
 
17
17
  command = DBA.const_get(command)
18
18
 
19
- arity = command.instance_method(:call).arity
20
-
21
- if arity >= 0 && args.size != arity
22
- raise DBA::Error, "incorrect number of args (given #{args.size}, expected #{arity})"
23
- end
19
+ command.arity_check(args)
24
20
 
25
21
  database = DBA::Database.connect
26
22
 
@@ -43,6 +39,7 @@ module DBA::Shell
43
39
  'pull' => :Pull,
44
40
  'sample' => :Sample,
45
41
  'schema' => :Schema,
42
+ 'select' => :Select,
46
43
  'tables' => :Tables
47
44
  }
48
45
  end
data/lib/dba.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2019-2020 TIMCRAFT
1
+ # Copyright (c) 2019-2022 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
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.1.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: 2022-10-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,14 +107,14 @@ 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
  - - ">="
113
114
  - !ruby/object:Gem::Version
114
115
  version: '0'
115
116
  requirements: []
116
- rubygems_version: 3.1.4
117
+ rubygems_version: 3.3.7
117
118
  signing_key:
118
119
  specification_version: 4
119
120
  summary: See description