schema_doctor 0.0.1 → 0.0.3

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: bb30a32516182031828f9be6b2f30a5496d5322ba77f7a4e17aa8655e2ef8596
4
- data.tar.gz: 40a59308e3e55209061d7995c393ff45071352ee168af12ea09387edf07fcff2
3
+ metadata.gz: 36282d15058adc20e4b9c2013bb9ddaa1d95f1e9457a10278be99a7b40c900e4
4
+ data.tar.gz: baf1eae580dedd1d8cbe4c5ca5bfef36e08849589aec89856731a9680cfc268a
5
5
  SHA512:
6
- metadata.gz: f7cd15084b68dd5b4c7993f5346344f01c2de301f2061ee7230267f2bf738a31b93611924a8532807ae25df8524e44f087d305116c988a6ed78d37ca0ebf2fb3
7
- data.tar.gz: de186a77d0f1afec160804205a521784919a75f92452558baa8453eebfceebd34cef33fa190519872d4c2a0fdfed0a54f5ebe8bf79c025c0b44a85da45cd9fde
6
+ metadata.gz: 917c1f0b805be6028f319f00698fdc153aedc38ffbf3b7ff6b780fc1d9eb4fd3fb7c0a106d4185cd32c54547279b509c869645a24ac3ca8849d2d5af6e4061a8
7
+ data.tar.gz: b309be6513ad10e6bf0563c9da4c87d1c9b17507f87d1a670527cc6c73f0f1b41ce0dab22a5b5b395fa3a196df991f96415078c33aaad3677e1163c87a6bb5ec
data/README.md CHANGED
@@ -1,11 +1,29 @@
1
1
  # SchemaDoctor
2
2
 
3
- TBD
3
+ Automatic database documentation tool, for Ruby on Rails project.
4
+
5
+ ## Demo
6
+
7
+ - **[Sample Output](https://lnit.github.io/schema_doctor/)**
8
+ - Generated from [Mastodon](https://github.com/mastodon/mastodon)'s [schema](https://github.com/mastodon/mastodon/blob/9be77fc0dbb01c1a8a54cd3da97e16c7941df367/db/schema.rb).
4
9
 
5
10
  ## Installation
6
11
 
7
- TBD
12
+ Add the gem to your project
13
+
14
+ ```rb
15
+ gem "rails"
16
+
17
+ group :development do
18
+ gem "schema_doctor"
19
+ end
20
+ ```
21
+
22
+ Then `bundle install` and you are ready to go.
8
23
 
9
24
  ## Usage
10
25
 
11
- TBD
26
+ ```sh
27
+ rails schema:analyze # Analyze Database Schema.
28
+ rails schema:export # Export HTML.
29
+ ```
@@ -32,10 +32,15 @@ module SchemaDoctor
32
32
  table_comment: connection.table_comment(model.table_name),
33
33
  extra_comment: schema.dig(model.name, :extra_comment),
34
34
  columns: columns(model, schema.dig(model.name, :columns) || {}),
35
- indexes: indexes(model)
35
+ indexes: indexes(model),
36
+ associations: associations(model)
36
37
  }
37
38
  rescue ActiveRecord::TableNotSpecified
38
39
  nil
40
+ rescue => e
41
+ # Skip analyzing if an error occurs
42
+ puts "Failed to process #{model.name}: #{e.inspect}"
43
+ puts "\e[31mWe're sorry, Failed to process \e[33m#{model.name}\e[31m:\n #{e.inspect}\e[0m"
39
44
  end
40
45
 
41
46
  schema
@@ -83,5 +88,38 @@ module SchemaDoctor
83
88
  }
84
89
  end
85
90
  end
91
+
92
+ def associations(model)
93
+ result = {
94
+ belongs: {},
95
+ has: {}
96
+ }
97
+
98
+ model.reflect_on_all_associations.each do |association|
99
+ case association.macro.to_s
100
+ when /\Abelongs_to/
101
+ result[:belongs][association.name] = {
102
+ macro: association.macro.to_s,
103
+ name: association.name,
104
+ class_name: association.class_name,
105
+ foreign_key: association.foreign_key,
106
+ options: association.options,
107
+ polymorphic: association.polymorphic?
108
+ }
109
+ when /\Ahas/
110
+ result[:has][association.name] = {
111
+ macro: association.macro.to_s,
112
+ name: association.name,
113
+ class_name: association.class_name,
114
+ options: association.options
115
+ }
116
+ else
117
+ puts "Unknown association type: #{association.macro}: :#{association.name}"
118
+ next
119
+ end
120
+ end
121
+
122
+ result
123
+ end
86
124
  end
87
125
  end
@@ -23,6 +23,8 @@ module SchemaDoctor
23
23
  :toclevels: 1
24
24
  :toc-title: Table of Contents
25
25
  :linkattrs:
26
+ :sectlinks:
27
+ :sectanchors:
26
28
 
27
29
  TEXT
28
30
 
@@ -85,6 +87,71 @@ module SchemaDoctor
85
87
 
86
88
  TEXT
87
89
  end
90
+
91
+ # Output associations schema
92
+ f.puts <<~TEXT
93
+
94
+ === Associations
95
+ TEXT
96
+ belongs = model[:associations][:belongs]
97
+ if belongs.present?
98
+ f.puts <<~TEXT
99
+ [cols="1,1,1,1"]
100
+ |===
101
+ |macro|name|foreign_key|options
102
+
103
+ TEXT
104
+
105
+ belongs.each_value do |association|
106
+ class_name = association.delete(:class_name).to_s
107
+ polymorphic = association.delete(:polymorphic)
108
+ # TODO: polymorphicのリンク先を特定できるようにする。今のところリンクを生成しないように。
109
+ association[:name] = "link:#_#{class_name.downcase.gsub("::", "")}[#{association[:name]}]" unless polymorphic
110
+
111
+ str = association.values.map do |v|
112
+ escaped = v.to_s.gsub("|", "{vbar}")
113
+ "|#{escaped}\n"
114
+ end.join
115
+
116
+ f.puts <<~TEXT
117
+ #{str}
118
+ TEXT
119
+ end
120
+
121
+ f.puts "|==="
122
+ end
123
+
124
+ has = model[:associations][:has]
125
+ if has.present?
126
+ f.puts <<~TEXT
127
+ [cols="1,1,1"]
128
+ |===
129
+ |macro|name|options
130
+
131
+ TEXT
132
+
133
+ has.each_value do |association|
134
+ class_name = association.delete(:class_name).to_s
135
+ association[:name] = "link:#_#{class_name.gsub("::", "").downcase}[#{association[:name]}]"
136
+ str = association.values.map do |v|
137
+ escaped = v.to_s.gsub("|", "{vbar}")
138
+ "|#{escaped}\n"
139
+ end.join
140
+
141
+ f.puts <<~TEXT
142
+ #{str}
143
+ TEXT
144
+ end
145
+
146
+ f.puts "|==="
147
+ end
148
+
149
+ if belongs.empty? & has.empty?
150
+ f.puts <<~TEXT
151
+ None
152
+
153
+ TEXT
154
+ end
88
155
  end
89
156
  end
90
157
  puts "Done! => #{adoc_path}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SchemaDoctor
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_doctor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - lni_T
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-02 00:00:00.000000000 Z
11
+ date: 2024-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord