the_schema_is 0.0.4 → 0.0.5
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/Changelog.md +5 -0
- data/README.md +3 -2
- data/config/defaults.yml +1 -0
- data/lib/the_schema_is/cops/node_util.rb +1 -1
- data/lib/the_schema_is/cops/parser.rb +27 -2
- data/lib/the_schema_is/cops.rb +9 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8735d476459a14d53240641bec87d03d7dbcd7bd5d3c8508135bde7739a04e80
|
4
|
+
data.tar.gz: 5cb9c6f68830db4c556aabe538611f448c581fd2c94efb15bf6eda0c0ce04511
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4ad68960b63c360ec9becc3f733e157a6547d55e318b7de37ac9d9ff3f872c436a07729759eeba126613fb812a291f7b76a1b782fa83a82c85977e91f066493
|
7
|
+
data.tar.gz: c37d853e5dfbb790264e146cd07849e959880b46559a92e5b578036b0cb906de4a4db74d4d744c4b8013763782c67f5bb0592f8bd2bb22ed702348ab42fdb1b3
|
data/Changelog.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# the-schema-is changes
|
2
2
|
|
3
|
+
## 2021-11-04 - 0.0.5
|
4
|
+
|
5
|
+
* Support `enum` column type;
|
6
|
+
* Add `RemoveDefinitions` config key for leaner `the_schema_is` definitions (avoiding huge index descriptions in models).
|
7
|
+
|
3
8
|
## 2021-09-15 - 0.0.4
|
4
9
|
|
5
10
|
* Get rid of [Fast](https://jonatas.github.io/fast/) dependency. It is cool, but we switched to use Rubocop's own `NodePattern` to lessen the dependency burden (Fast was depending on [astrolabe](https://github.com/yujinakayama/astrolabe) which wasn't updated in 6 years, locking parser dependency to old version and making Fast incompatible with newer Rubocop);
|
data/README.md
CHANGED
@@ -134,8 +134,9 @@ TheSchemaIs:
|
|
134
134
|
Currently available settings are:
|
135
135
|
|
136
136
|
* `TablePrefix` to help `the_schema_is` deduce table name from class name;
|
137
|
-
* `Schema` to set path to schema (by default `db/schema.rb`)
|
138
|
-
* `BaseClass` to help `the_schema_is` guess what is a model class (by default `ApplicationRecord` and `ActiveRecord::Base`)
|
137
|
+
* `Schema` to set path to schema (by default `db/schema.rb`);
|
138
|
+
* `BaseClass` to help `the_schema_is` guess what is a model class (by default `ApplicationRecord` and `ActiveRecord::Base`);
|
139
|
+
* `RemoveDefinitions`: list of definition keys to remove (for example, `[index, foreign_key, limit]`) when copying definitions into models; this might be desirable for leaner `the_schema_is` statements, displaying only field types/names.
|
139
140
|
|
140
141
|
So, if you have your custom-named base class, you should do:
|
141
142
|
|
data/config/defaults.yml
CHANGED
@@ -15,7 +15,7 @@ module TheSchemaIs
|
|
15
15
|
float integer json string text time timestamp virtual].freeze
|
16
16
|
POSTGRES_COLUMN_TYPES = %i[jsonb inet cidr macaddr hstore uuid].freeze
|
17
17
|
|
18
|
-
COLUMN_DEFS = (STANDARD_COLUMN_TYPES + POSTGRES_COLUMN_TYPES + %i[column]).freeze
|
18
|
+
COLUMN_DEFS = (STANDARD_COLUMN_TYPES + POSTGRES_COLUMN_TYPES + %i[enum column]).freeze
|
19
19
|
|
20
20
|
Model = Struct.new(:class_name, :table_name, :source, :schema, :table_name_node,
|
21
21
|
keyword_init: true)
|
@@ -33,9 +33,11 @@ module TheSchemaIs
|
|
33
33
|
RuboCop::AST::ProcessedSource.new(code, 2.7).ast
|
34
34
|
end
|
35
35
|
|
36
|
-
def self.schema(path)
|
36
|
+
def self.schema(path, remove_definition_attrs: [])
|
37
37
|
ast = parse(File.read(path))
|
38
38
|
|
39
|
+
ast = remove_attributes(ast, remove_definition_attrs) unless remove_definition_attrs.empty?
|
40
|
+
|
39
41
|
ast.ast_search('(block (send nil? :create_table (str $_) _) _ $_)').to_h
|
40
42
|
end
|
41
43
|
|
@@ -95,6 +97,29 @@ module TheSchemaIs
|
|
95
97
|
end
|
96
98
|
}.compact
|
97
99
|
end
|
100
|
+
|
101
|
+
# Removes unnecessary column definitions from further comparison, using schema source tree editing
|
102
|
+
def self.remove_attributes(ast, attrs_to_remove)
|
103
|
+
buf = ast.loc.expression.source_buffer
|
104
|
+
src = buf.source
|
105
|
+
rewriter = ::Parser::Source::TreeRewriter.new(buf)
|
106
|
+
|
107
|
+
# FIXME: Two nested cycles can be simplifid to just look for column definition, probably
|
108
|
+
ast.ast_search('(block (send nil? :create_table (str _) _) _ $_)').each do |table_def|
|
109
|
+
table_def.children.each do |col|
|
110
|
+
dfn = col.children[3] or next
|
111
|
+
dfn.children
|
112
|
+
.select { |c| attrs_to_remove.include?(c.children[0].children[0]) }
|
113
|
+
.each do |c|
|
114
|
+
prev_comma = c.source_range.begin_pos.step(by: -1).find { |pos| src[pos] == ',' }
|
115
|
+
range = ::Parser::Source::Range.new(buf, prev_comma, c.source_range.end_pos)
|
116
|
+
rewriter.remove(range)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
RuboCop::AST::ProcessedSource.new(rewriter.process, 2.7).ast
|
122
|
+
end
|
98
123
|
end
|
99
124
|
end
|
100
125
|
end
|
data/lib/the_schema_is/cops.rb
CHANGED
@@ -15,8 +15,12 @@ module TheSchemaIs
|
|
15
15
|
using Cops::NodeRefinements
|
16
16
|
|
17
17
|
module Cops
|
18
|
-
|
19
|
-
|
18
|
+
class << self
|
19
|
+
extend Memoist
|
20
|
+
|
21
|
+
memoize def fetch_schema(path, remove_definition_attrs: [])
|
22
|
+
Cops::Parser.schema(path, remove_definition_attrs: remove_definition_attrs)
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
22
26
|
|
@@ -59,7 +63,9 @@ module TheSchemaIs
|
|
59
63
|
end
|
60
64
|
|
61
65
|
memoize def schema
|
62
|
-
|
66
|
+
attrs_to_remove = cop_config['RemoveDefinitions']&.map(&:to_sym) || []
|
67
|
+
# It is OK if it returns Nil, just will be handled by "schema is absent" cop
|
68
|
+
Cops.fetch_schema(schema_path, remove_definition_attrs: attrs_to_remove)[model.table_name]
|
63
69
|
end
|
64
70
|
|
65
71
|
memoize def model_columns
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_schema_is
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Shepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: backports
|