dba 1.0.0 → 1.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: b66a9e45cd9054a65b9453a5c981d6637d1060f2d0862d45874276d630bd8859
4
- data.tar.gz: d1ddad863691a445c275cdbb580e6f463a8bc6a7bf4e6856b2ff3b110ac7c9f7
3
+ metadata.gz: 4212cdf60ed43e28c00a0bb815eabf6ff0912535713a27a425f077e71ef27f4d
4
+ data.tar.gz: a3da3be24d5e75207eb239fd814307c02457d22b286f1de548b5429c830dec60
5
5
  SHA512:
6
- metadata.gz: 6592ef3d6089bb864d72c3eb1606b8a3907d1bcdbe114a29b426c253e7d8864ad90c6f7a23663184339c1040692d68b82ce73ee151fd18fba13c5d618a1b4c0b
7
- data.tar.gz: 683c320b294abcb74b42d74de889bf0205f79075b1c51f541c1e64eabfe418e662c5fc88a33b0ef22cdcf558d07299a76352cfe21a870d009b36aa4b0102b356
6
+ metadata.gz: 84c04318233dc14cacafb8b5bd065fb1d44d713cbbf39f50463dd1c46e12bc49599560e637dc0179ccbcc44b8c2cc3bde45a9244daf5d28867a0d538762fe352
7
+ data.tar.gz: 0a62d78ddcd245fc5d8781f3af94dda6b463f5dac11623fe11be546339de7bdedd8827d08b68db6da85a1fb377bf2316f724bb83ac54331bf850be7f557c4d62
@@ -0,0 +1,11 @@
1
+ # 1.1.0
2
+
3
+ * Fixed sample command for postgres
4
+
5
+ * Added dump command for dumping data to CSV, LDJSON, or YAML
6
+
7
+ * Added load command for loading data from CSV, LDJSON, or YAML
8
+
9
+ # 1.0.0
10
+
11
+ * First version!
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # dba
2
2
 
3
- A developer tool for working with development databases.
3
+ Ruby command line tool for working with development databases.
4
4
 
5
5
 
6
6
  ## Installation
@@ -19,9 +19,11 @@ You can connect to any database supported by [sequel](https://rubygems.org/gems/
19
19
  Usage: dba COMMAND
20
20
 
21
21
  dba diff URL
22
+ dba dump TABLE EXTENSION
22
23
  dba edit TABLE IDENTIFIER
23
24
  dba find TABLE IDENTIFIER
24
25
  dba indexes [TABLE]
26
+ dba load PATH
25
27
  dba pull TABLE URL
26
28
  dba sample TABLE [COLUMN]
27
29
  dba schema [TABLE]
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'dba'
3
- s.version = '1.0.0'
3
+ s.version = '1.1.0'
4
4
  s.license = 'GPL-3.0'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.authors = ['Tim Craft']
7
7
  s.email = ['mail@timcraft.com']
8
8
  s.homepage = 'https://github.com/readysteady/dba'
9
- s.description = 'A developer tool for working with development databases'
9
+ s.description = 'Ruby command line tool for working with development databases'
10
10
  s.summary = 'See description'
11
- s.files = Dir.glob('lib/**/*.rb') + %w[LICENSE.txt README.md dba.gemspec]
11
+ s.files = Dir.glob('lib/**/*.rb') + %w[CHANGES.md LICENSE.txt README.md dba.gemspec]
12
12
  s.required_ruby_version = '>= 2.3.0'
13
13
  s.add_dependency('zeitwerk', '~> 2', '>= 2.2')
14
14
  s.add_dependency('sequel', '~> 5')
data/lib/dba.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2019 TIMCRAFT
1
+ # Copyright (c) 2019-2020 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
@@ -17,7 +17,12 @@ module DBA
17
17
 
18
18
  loader = Zeitwerk::Loader.new
19
19
  loader.tag = File.basename(__FILE__, '.rb')
20
- loader.inflector.inflect('dba' => 'DBA')
20
+ loader.inflector.inflect({
21
+ 'dba' => 'DBA',
22
+ 'csv' => 'CSV',
23
+ 'ldjson' => 'LDJSON',
24
+ 'yaml' => 'YAML'
25
+ })
21
26
  loader.push_dir(__dir__)
22
27
  loader.setup
23
28
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ require 'csv'
3
+
4
+ module DBA::CSV
5
+ def self.dump(database, table_name, path)
6
+ columns = nil
7
+
8
+ ::CSV.open(path, 'wb') do |csv|
9
+ database[table_name].each_with_index do |row, index|
10
+ if index.zero?
11
+ columns = row.keys
12
+
13
+ csv << columns
14
+ end
15
+
16
+ csv << row.values_at(*columns)
17
+ end
18
+ end
19
+ end
20
+
21
+ def self.load(path, database, table_name)
22
+ dataset = database[table_name]
23
+
24
+ rows = ::CSV.read(path)
25
+
26
+ headers = rows.shift
27
+
28
+ dataset.import(headers, rows)
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DBA::Dump < DBA::Command
4
+ ADAPTERS = {
5
+ 'csv' => :CSV,
6
+ 'ldjson' => :LDJSON,
7
+ 'yml' => :YAML,
8
+ 'yaml' => :YAML
9
+ }
10
+
11
+ def call(table, extension)
12
+ self.table_name = table
13
+
14
+ output_path = "#{table_name}.#{extension}"
15
+
16
+ adapter = ADAPTERS.fetch(extension) { raise DBA::Error, 'unsupported file extension' }
17
+
18
+ adapter = DBA.const_get(adapter)
19
+
20
+ rows = database[table_name].count
21
+
22
+ return if rows.zero?
23
+
24
+ adapter.dump(database, table_name, output_path)
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require 'json'
3
+
4
+ module DBA::LDJSON
5
+ def self.dump(database, table_name, path)
6
+ File.open(path, 'w+') do |io|
7
+ database[table_name].each do |row|
8
+ io.puts ::JSON.generate(row.compact)
9
+ end
10
+ end
11
+ end
12
+
13
+ def self.load(path, database, table_name)
14
+ dataset = database[table_name]
15
+
16
+ database.transaction do
17
+ File.readlines(path).each do |line|
18
+ dataset.insert(::JSON.parse(line))
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DBA::Load < DBA::Command
4
+ ADAPTERS = {
5
+ '.csv' => :CSV,
6
+ '.ldjson' => :LDJSON,
7
+ '.yml' => :YAML,
8
+ '.yaml' => :YAML
9
+ }
10
+
11
+ def call(path)
12
+ file_list(path).each do |file|
13
+ extension = File.extname(file)
14
+
15
+ adapter = ADAPTERS.fetch(extension) { raise DBA::Error, 'unsupported file extension' }
16
+
17
+ adapter = DBA.const_get(adapter)
18
+
19
+ table_name = File.basename(file, extension).to_sym
20
+
21
+ adapter.load(file, database, table_name)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def file_list(path)
28
+ if File.directory?(path)
29
+ Dir.glob(File.join(path, '*.{csv,ldjson,yml,yaml}'))
30
+ else
31
+ [path]
32
+ end
33
+ end
34
+ end
@@ -4,14 +4,13 @@ class DBA::Sample < DBA::Command
4
4
 
5
5
  column_name = column.to_sym if column
6
6
 
7
- random_rows = database[table_name].order(random_function)
8
-
9
7
  if column_name
10
- random_rows.distinct.select(column_name).limit(20).each do |row|
8
+ dataset = database[table_name].distinct.select(column_name)
9
+ dataset.from_self.order(random_function).limit(20).each do |row|
11
10
  puts row[column_name]
12
11
  end
13
12
  else
14
- random_rows.first(3).each do |row|
13
+ database[table_name].order(random_function).first(3).each do |row|
15
14
  printer.print(row)
16
15
  printer.print_line
17
16
  end
@@ -35,9 +35,11 @@ module DBA::Shell
35
35
  def commands
36
36
  {
37
37
  'diff' => :Diff,
38
+ 'dump' => :Dump,
38
39
  'edit' => :Edit,
39
40
  'find' => :Find,
40
41
  'indexes' => :Indexes,
42
+ 'load' => :Load,
41
43
  'pull' => :Pull,
42
44
  'sample' => :Sample,
43
45
  'schema' => :Schema,
@@ -52,7 +54,7 @@ module DBA::Shell
52
54
  end
53
55
 
54
56
  def program_name
55
- File.basename($0)
57
+ 'dba'
56
58
  end
57
59
 
58
60
  def print_usage
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ require 'yaml'
3
+
4
+ module DBA::YAML
5
+ def self.dump(database, table_name, path)
6
+ File.open(path, 'w+') do |io|
7
+ io.write ::YAML.dump(database[table_name].all.map(&:compact))
8
+ end
9
+ end
10
+
11
+ def self.load(path, database, table_name)
12
+ dataset = database[table_name]
13
+
14
+ database.transaction do
15
+ ::YAML.load_file(path).each do |row|
16
+ dataset.insert(row)
17
+ end
18
+ end
19
+ end
20
+ 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.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Craft
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-22 00:00:00.000000000 Z
11
+ date: 2020-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -58,7 +58,7 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
- description: A developer tool for working with development databases
61
+ description: Ruby command line tool for working with development databases
62
62
  email:
63
63
  - mail@timcraft.com
64
64
  executables:
@@ -66,17 +66,22 @@ executables:
66
66
  extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
+ - CHANGES.md
69
70
  - LICENSE.txt
70
71
  - README.md
71
72
  - bin/dba
72
73
  - dba.gemspec
73
74
  - lib/dba.rb
74
75
  - lib/dba/command.rb
76
+ - lib/dba/csv.rb
75
77
  - lib/dba/database.rb
76
78
  - lib/dba/diff.rb
79
+ - lib/dba/dump.rb
77
80
  - lib/dba/edit.rb
78
81
  - lib/dba/find.rb
79
82
  - lib/dba/indexes.rb
83
+ - lib/dba/ldjson.rb
84
+ - lib/dba/load.rb
80
85
  - lib/dba/printer.rb
81
86
  - lib/dba/pull.rb
82
87
  - lib/dba/row_command.rb
@@ -88,11 +93,12 @@ files:
88
93
  - lib/dba/table_command.rb
89
94
  - lib/dba/table_schema.rb
90
95
  - lib/dba/tables.rb
96
+ - lib/dba/yaml.rb
91
97
  homepage: https://github.com/readysteady/dba
92
98
  licenses:
93
99
  - GPL-3.0
94
100
  metadata: {}
95
- post_install_message:
101
+ post_install_message:
96
102
  rdoc_options: []
97
103
  require_paths:
98
104
  - lib
@@ -107,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
113
  - !ruby/object:Gem::Version
108
114
  version: '0'
109
115
  requirements: []
110
- rubygems_version: 3.0.3
111
- signing_key:
116
+ rubygems_version: 3.1.4
117
+ signing_key:
112
118
  specification_version: 4
113
119
  summary: See description
114
120
  test_files: []