kolla 0.0.3 → 0.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/README.md +64 -0
- data/bin/kolla +32 -2
- data/lib/kolla/errors.rb +7 -0
- data/lib/kolla/parser.rb +39 -23
- data/lib/kolla/version.rb +5 -0
- data/lib/kolla.rb +11 -14
- metadata +14 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: aa7af9c82815bce009d2214337606318aaf9af44fa9874fbbd70793926ab9aca
|
|
4
|
+
data.tar.gz: 58d03e71d4ebb50a0ff1bdef9d33dfebb0cd9f3199d2a7f2e5212f90d3df4742
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eaf1ca3aaf552e86d5812f2a4f11be50a9766c5cc2e1fa862a77c74f3930381aa01e932cbf660d51bac053fe96b12d819da6c07f5ef0c03af599c547f2206745
|
|
7
|
+
data.tar.gz: 46903fc9c6446b1f51c8896ae88140e88ad9483e62336aa2dc1e9e3a95f3d994e61de5bfa09f15e8bce88a42d0faec91506cc20499f953733871c26edbd78ee5
|
data/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Kolla
|
|
2
|
+
|
|
3
|
+
Zero-dependency CLI that parses `db/schema.rb` and answers "what's in this table?" in under a second — without booting Rails.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
gem install kolla
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
List all tables:
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
$ kolla
|
|
17
|
+
users
|
|
18
|
+
posts
|
|
19
|
+
comments
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Inspect a table:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
$ kolla users
|
|
26
|
+
name (string, null: false)
|
|
27
|
+
email (string)
|
|
28
|
+
life_in_seconds (bigint, default: 0)
|
|
29
|
+
admin (boolean, default: false)
|
|
30
|
+
created_at (datetime, null: false)
|
|
31
|
+
index: [email] (unique)
|
|
32
|
+
index: [name]
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Use a custom schema path:
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
$ kolla -f path/to/schema.rb users
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Options
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
-h, --help Show help
|
|
45
|
+
-v, --version Show version
|
|
46
|
+
-f, --file PATH Path to schema.rb (default: db/schema.rb)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## What gets filtered
|
|
50
|
+
|
|
51
|
+
Rails internal tables (`action_*`, `active_*`) are excluded from the table list.
|
|
52
|
+
|
|
53
|
+
## Development
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
git clone https://github.com/antonysastre/kolla.git
|
|
57
|
+
cd kolla
|
|
58
|
+
bundle install
|
|
59
|
+
rake test
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
data/bin/kolla
CHANGED
|
@@ -1,4 +1,34 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
2
3
|
|
|
3
|
-
require
|
|
4
|
-
|
|
4
|
+
require "kolla"
|
|
5
|
+
|
|
6
|
+
file_path = Kolla::Parser::DEFAULT_FILE
|
|
7
|
+
|
|
8
|
+
args = ARGV.dup
|
|
9
|
+
while args.first&.start_with?("-")
|
|
10
|
+
case args.shift
|
|
11
|
+
when "--help", "-h"
|
|
12
|
+
puts <<~USAGE
|
|
13
|
+
Usage: kolla [options] [TABLE_NAME]
|
|
14
|
+
|
|
15
|
+
Options:
|
|
16
|
+
-h, --help Show this help
|
|
17
|
+
-v, --version Show version
|
|
18
|
+
-f, --file PATH Path to schema.rb (default: db/schema.rb)
|
|
19
|
+
|
|
20
|
+
Examples:
|
|
21
|
+
kolla List all tables
|
|
22
|
+
kolla users Show fields for 'users' table
|
|
23
|
+
kolla -f path/schema.rb users
|
|
24
|
+
USAGE
|
|
25
|
+
exit 0
|
|
26
|
+
when "--version", "-v"
|
|
27
|
+
puts "kolla #{Kolla::VERSION}"
|
|
28
|
+
exit 0
|
|
29
|
+
when "--file", "-f"
|
|
30
|
+
file_path = args.shift
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
Kolla.run(args, file_path: file_path)
|
data/lib/kolla/errors.rb
ADDED
data/lib/kolla/parser.rb
CHANGED
|
@@ -2,40 +2,56 @@
|
|
|
2
2
|
|
|
3
3
|
module Kolla
|
|
4
4
|
class Parser
|
|
5
|
-
DEFAULT_FILE = File.join(
|
|
5
|
+
DEFAULT_FILE = File.join("db", "schema.rb").freeze
|
|
6
|
+
IGNORED_PREFIXES = %w[action_ active_].freeze
|
|
6
7
|
|
|
7
|
-
def initialize(file_path: DEFAULT_FILE
|
|
8
|
+
def initialize(file_path: DEFAULT_FILE)
|
|
8
9
|
@file_path = File.expand_path(file_path)
|
|
9
|
-
@
|
|
10
|
+
@content = File.read(@file_path)
|
|
11
|
+
rescue Errno::ENOENT
|
|
12
|
+
raise SchemaNotFound, "Schema file not found at #{@file_path}"
|
|
10
13
|
end
|
|
11
14
|
|
|
12
|
-
def
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
extract_fields(content, options[:model_name])
|
|
16
|
-
else
|
|
17
|
-
extract_tables(content)
|
|
18
|
-
end
|
|
19
|
-
rescue Errno::ENOENT
|
|
20
|
-
Kolla.logger.warn "Schema file_path not found at #{file_path}"
|
|
15
|
+
def tables
|
|
16
|
+
matches = @content.scan(/create_table\s+"(\w+)"/)
|
|
17
|
+
matches.map(&:first).reject { |table| IGNORED_PREFIXES.any? { |prefix| table.start_with?(prefix) } }
|
|
21
18
|
end
|
|
22
19
|
|
|
23
|
-
|
|
20
|
+
def fields_for(name)
|
|
21
|
+
escaped = Regexp.escape(name)
|
|
22
|
+
table_regex = /create_table "#{escaped}".*?do \|t\|(.*?)end/m
|
|
23
|
+
match = @content.match(table_regex)
|
|
24
|
+
raise TableNotFound, "Table '#{name}' not found in schema" unless match
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
block = match[1]
|
|
27
|
+
fields = parse_fields(block)
|
|
28
|
+
indexes = parse_indexes(block)
|
|
29
|
+
fields + indexes
|
|
28
30
|
end
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
table_regex = /create_table "#{table_name}".*?do \|t\|(.*?)end/m
|
|
32
|
-
match = schema.match(table_regex)
|
|
33
|
-
return [] unless match
|
|
32
|
+
private
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
def parse_fields(block)
|
|
35
|
+
field_regex = /t\.(\w+)\s+"(\w+)"(.*)/
|
|
36
|
+
block.scan(field_regex).map do |type, name, rest|
|
|
37
|
+
next if type == "index"
|
|
38
|
+
|
|
39
|
+
parts = [type]
|
|
40
|
+
parts << "null: false" if rest.include?("null: false")
|
|
41
|
+
if (default_match = rest.match(/default:\s*("(?:[^"\\]|\\.)*"|[\w.]+)/))
|
|
42
|
+
parts << "default: #{default_match[1]}"
|
|
43
|
+
end
|
|
44
|
+
"#{name} (#{parts.join(', ')})"
|
|
45
|
+
end.compact
|
|
37
46
|
end
|
|
38
47
|
|
|
39
|
-
|
|
48
|
+
def parse_indexes(block)
|
|
49
|
+
index_regex = /t\.index\s+\[([^\]]+)\](.*)$/
|
|
50
|
+
block.scan(index_regex).map do |columns, rest|
|
|
51
|
+
cols = columns.scan(/"(\w+)"/).flatten
|
|
52
|
+
unique = rest.include?("unique: true") ? " (unique)" : ""
|
|
53
|
+
"index: [#{cols.join(', ')}]#{unique}"
|
|
54
|
+
end
|
|
55
|
+
end
|
|
40
56
|
end
|
|
41
57
|
end
|
data/lib/kolla.rb
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require_relative "kolla/version"
|
|
4
|
+
require_relative "kolla/errors"
|
|
5
|
+
require_relative "kolla/parser"
|
|
4
6
|
|
|
5
|
-
# Kolla parses a file and reports matching results.
|
|
6
7
|
module Kolla
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
self.logger = Logger.new($stdout)
|
|
8
|
+
def self.run(args = [], file_path: Parser::DEFAULT_FILE)
|
|
9
|
+
parser = Parser.new(file_path: file_path)
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
model_name = args[0]
|
|
16
|
-
puts Parser.new(options:{model_name:}).run!
|
|
11
|
+
if args.empty?
|
|
12
|
+
puts parser.tables
|
|
17
13
|
else
|
|
18
|
-
puts
|
|
14
|
+
puts parser.fields_for(args[0])
|
|
19
15
|
end
|
|
16
|
+
rescue Kolla::Error => e
|
|
17
|
+
warn e.message
|
|
18
|
+
exit 1
|
|
20
19
|
end
|
|
21
20
|
end
|
|
22
|
-
|
|
23
|
-
require 'kolla/parser'
|
metadata
CHANGED
|
@@ -1,27 +1,35 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: kolla
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Antony Sastre
|
|
8
|
+
autorequire:
|
|
8
9
|
bindir: bin
|
|
9
10
|
cert_chain: []
|
|
10
|
-
date:
|
|
11
|
+
date: 2026-03-20 00:00:00.000000000 Z
|
|
11
12
|
dependencies: []
|
|
13
|
+
description: Zero-dependency CLI that parses db/schema.rb and answers 'what's in this
|
|
14
|
+
table?' in under a second — without booting Rails.
|
|
12
15
|
email: antony.sastre@gmail.com
|
|
13
16
|
executables:
|
|
14
17
|
- kolla
|
|
15
18
|
extensions: []
|
|
16
19
|
extra_rdoc_files: []
|
|
17
20
|
files:
|
|
21
|
+
- README.md
|
|
18
22
|
- bin/kolla
|
|
19
23
|
- lib/kolla.rb
|
|
24
|
+
- lib/kolla/errors.rb
|
|
20
25
|
- lib/kolla/parser.rb
|
|
26
|
+
- lib/kolla/version.rb
|
|
21
27
|
homepage: https://rubygems.org/gems/kolla
|
|
22
28
|
licenses:
|
|
23
29
|
- MIT
|
|
24
|
-
metadata:
|
|
30
|
+
metadata:
|
|
31
|
+
source_code_uri: https://github.com/antonysastre/kolla
|
|
32
|
+
post_install_message:
|
|
25
33
|
rdoc_options: []
|
|
26
34
|
require_paths:
|
|
27
35
|
- lib
|
|
@@ -36,7 +44,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
36
44
|
- !ruby/object:Gem::Version
|
|
37
45
|
version: '0'
|
|
38
46
|
requirements: []
|
|
39
|
-
rubygems_version: 3.
|
|
47
|
+
rubygems_version: 3.0.3.1
|
|
48
|
+
signing_key:
|
|
40
49
|
specification_version: 4
|
|
41
|
-
summary: Simple CLI
|
|
50
|
+
summary: Simple CLI tool to parse Rails schema file and list its details.
|
|
42
51
|
test_files: []
|