dba 1.1.0 → 2.1.0
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 +4 -4
- data/CHANGES.md +25 -0
- data/README.md +1 -0
- data/dba.gemspec +2 -2
- data/lib/dba/command.rb +12 -0
- data/lib/dba/database.rb +10 -18
- data/lib/dba/diff.rb +3 -3
- data/lib/dba/dump.rb +2 -0
- data/lib/dba/load.rb +3 -1
- data/lib/dba/printer.rb +13 -4
- data/lib/dba/select.rb +14 -0
- data/lib/dba/shell.rb +2 -5
- data/lib/dba.rb +1 -1
- metadata +5 -4
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 6d8a51736a883e5fa3608fd0429dbf3a819f2903a2da62d1df26d234952c899d
         | 
| 4 | 
            +
              data.tar.gz: bcdb786558aab8e9235324eeb06e073bff126c73abd680213f030d8560cc5306
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 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
    
    
    
        data/dba.gemspec
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            Gem::Specification.new do |s|
         | 
| 2 2 | 
             
              s.name = 'dba'
         | 
| 3 | 
            -
              s.version = ' | 
| 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. | 
| 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  | 
| 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?( | 
| 72 | 
            +
                File.exist?(database_config_path)
         | 
| 68 73 | 
             
              end
         | 
| 69 74 |  | 
| 70 75 | 
             
              def database_config
         | 
| 71 | 
            -
                YAML.load(ERB.new(File.read( | 
| 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 | 
            -
                 | 
| 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 | 
            -
                 | 
| 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
    
    
    
        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 | 
| 61 | 
            -
                  fields << muted(' | 
| 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 | 
            -
                   | 
| 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  | 
| 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 | 
            -
                 | 
| 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
    
    
    
        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:  | 
| 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:  | 
| 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. | 
| 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. | 
| 117 | 
            +
            rubygems_version: 3.3.7
         | 
| 117 118 | 
             
            signing_key:
         | 
| 118 119 | 
             
            specification_version: 4
         | 
| 119 120 | 
             
            summary: See description
         |