rails_schema_excel 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 +7 -0
- data/README.md +65 -0
- data/Rakefile +4 -0
- data/exe/rails_schema_excel +35 -0
- data/lib/rails_schema_excel/exporter.rb +156 -0
- data/lib/rails_schema_excel/parser.rb +57 -0
- data/lib/rails_schema_excel/version.rb +5 -0
- data/lib/rails_schema_excel.rb +14 -0
- data/sig/rails_schema_excel.rbs +4 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 198d77326308bbe83fe771b6aea1197cd03449c4cefc0eb2e16bf0bff37b1deb
|
4
|
+
data.tar.gz: 0eac34f563ce3083eb1ea004de83851f13f4f8dac9eb487c1665b6a9fd63b023
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 82fbd81d9ad7ca3666bca685bbf93e76968886a36725db6e26d363dd6452a6926119e91861f8ab993c3585617dc5ecf4ecea5a71a3240bca9933ed7b7da02a8d
|
7
|
+
data.tar.gz: 4af3392cddac63eefded135acaeefe8c3661e6daf5e681eeef8c9e5696ac17fc08fc4c5502edb7b4c82e952cfdf803767c9d4bf34a060d89bb5703f956b34f93
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# RailsSchemaExcel
|
2
|
+
|
3
|
+
Export Rails database schema to Excel format with A5:SQL Mk-2 style layout.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rails_schema_excel'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rails_schema_excel
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Command Line
|
24
|
+
|
25
|
+
```bash
|
26
|
+
# From Rails root directory (uses db/schema.rb by default)
|
27
|
+
rails_schema_excel
|
28
|
+
|
29
|
+
# Specify schema file and output
|
30
|
+
rails_schema_excel -s db/schema.rb -o output.xlsx
|
31
|
+
|
32
|
+
# Show help
|
33
|
+
rails_schema_excel -h
|
34
|
+
```
|
35
|
+
|
36
|
+
### In Ruby Code
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'rails_schema_excel'
|
40
|
+
|
41
|
+
RailsSchemaExcel.export('db/schema.rb', 'schema.xlsx')
|
42
|
+
```
|
43
|
+
|
44
|
+
## Features
|
45
|
+
|
46
|
+
Each table is exported as a separate Excel sheet with A5:SQL Mk-2 format including:
|
47
|
+
|
48
|
+
- **テーブル情報** (Table Information)
|
49
|
+
- **カラム情報** (Column Information) - name, type, not null, default, comments
|
50
|
+
- **インデックス情報** (Index Information)
|
51
|
+
- **制約情報** (Constraint Information) - primary keys
|
52
|
+
- **外部キー情報** (Foreign Key Information)
|
53
|
+
- **外部キー情報(PK側)** (Foreign Key Information - PK side)
|
54
|
+
- **トリガー情報** (Trigger Information)
|
55
|
+
- **RDBMS固有の情報** (RDBMS Specific Information)
|
56
|
+
|
57
|
+
## Development
|
58
|
+
|
59
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'rails_schema_excel'
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
options = {}
|
8
|
+
OptionParser.new do |opts|
|
9
|
+
opts.banner = "Usage: rails_schema_excel [options]"
|
10
|
+
|
11
|
+
opts.on("-s", "--schema FILE", "Path to schema.rb file (default: db/schema.rb)") do |file|
|
12
|
+
options[:schema] = file
|
13
|
+
end
|
14
|
+
|
15
|
+
opts.on("-o", "--output FILE", "Output Excel file (default: schema.xlsx)") do |file|
|
16
|
+
options[:output] = file
|
17
|
+
end
|
18
|
+
|
19
|
+
opts.on("-h", "--help", "Prints this help") do
|
20
|
+
puts opts
|
21
|
+
exit
|
22
|
+
end
|
23
|
+
end.parse!
|
24
|
+
|
25
|
+
schema_file = options[:schema] || 'db/schema.rb'
|
26
|
+
output_file = options[:output] || 'schema.xlsx'
|
27
|
+
|
28
|
+
unless File.exist?(schema_file)
|
29
|
+
puts "Error: #{schema_file} not found"
|
30
|
+
exit 1
|
31
|
+
end
|
32
|
+
|
33
|
+
puts "Exporting #{schema_file} to #{output_file}..."
|
34
|
+
RailsSchemaExcel.export(schema_file, output_file)
|
35
|
+
puts "Done! Exported to #{output_file}"
|
@@ -0,0 +1,156 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'caxlsx'
|
4
|
+
|
5
|
+
module RailsSchemaExcel
|
6
|
+
class Exporter
|
7
|
+
GREEN = 'FF92D050'
|
8
|
+
|
9
|
+
def self.export(tables, output_file)
|
10
|
+
Axlsx::Package.new do |p|
|
11
|
+
tables.each do |table_name, table_data|
|
12
|
+
next if table_data[:columns].empty?
|
13
|
+
|
14
|
+
sheet_name = table_name[0..30]
|
15
|
+
p.workbook.add_worksheet(name: sheet_name) do |sheet|
|
16
|
+
create_a5_format(sheet, table_name, table_data)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
p.serialize(output_file)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.create_a5_format(sheet, table_name, table_data)
|
25
|
+
green_fill = sheet.styles.add_style(bg_color: GREEN, fg_color: '00', b: true, border: Axlsx::STYLE_THIN_BORDER)
|
26
|
+
header_style = sheet.styles.add_style(bg_color: GREEN, fg_color: '00', b: true, border: Axlsx::STYLE_THIN_BORDER, alignment: { horizontal: :center })
|
27
|
+
border_style = sheet.styles.add_style(border: Axlsx::STYLE_THIN_BORDER)
|
28
|
+
bold_style = sheet.styles.add_style(b: true)
|
29
|
+
|
30
|
+
# テーブル情報
|
31
|
+
sheet.add_row ['テーブル情報'], style: bold_style
|
32
|
+
sheet.merge_cells("A1:F1")
|
33
|
+
|
34
|
+
[
|
35
|
+
['システム名', ''],
|
36
|
+
['サブシステム名', ''],
|
37
|
+
['スキーマ名', table_name],
|
38
|
+
['論理テーブル名', ''],
|
39
|
+
['物理テーブル名', table_name],
|
40
|
+
['備考', '']
|
41
|
+
].each do |label, value|
|
42
|
+
sheet.add_row [label, value], style: [green_fill, border_style]
|
43
|
+
sheet.merge_cells("B#{sheet.rows.length}:C#{sheet.rows.length}")
|
44
|
+
end
|
45
|
+
|
46
|
+
sheet.add_row []
|
47
|
+
|
48
|
+
# カラム情報
|
49
|
+
sheet.add_row ['カラム情報'], style: bold_style
|
50
|
+
sheet.merge_cells("A#{sheet.rows.length}:F#{sheet.rows.length}")
|
51
|
+
|
52
|
+
sheet.add_row ['No', '物理名', 'データ型', 'Not Null', 'デフォルト', '備考'], style: header_style
|
53
|
+
|
54
|
+
table_data[:columns].each_with_index do |col, idx|
|
55
|
+
sheet.add_row [
|
56
|
+
idx + 1,
|
57
|
+
col[:name],
|
58
|
+
col[:type],
|
59
|
+
col[:not_null] ? 'Yes' : '',
|
60
|
+
col[:default],
|
61
|
+
''
|
62
|
+
], style: border_style
|
63
|
+
end
|
64
|
+
|
65
|
+
sheet.add_row []
|
66
|
+
|
67
|
+
# インデックス情報
|
68
|
+
sheet.add_row ['インデックス情報'], style: bold_style
|
69
|
+
sheet.merge_cells("A#{sheet.rows.length}:F#{sheet.rows.length}")
|
70
|
+
|
71
|
+
sheet.add_row ['No', 'インデックス名', 'カラムリスト', 'キー', 'ユニーク', '備考'], style: header_style
|
72
|
+
|
73
|
+
table_data[:indexes].each_with_index do |idx_data, idx|
|
74
|
+
sheet.add_row [
|
75
|
+
idx + 1,
|
76
|
+
"idx_#{idx_data[:columns]}",
|
77
|
+
idx_data[:columns],
|
78
|
+
'',
|
79
|
+
idx_data[:unique] ? 'Yes' : '',
|
80
|
+
''
|
81
|
+
], style: border_style
|
82
|
+
end
|
83
|
+
|
84
|
+
sheet.add_row []
|
85
|
+
|
86
|
+
# 制約情報
|
87
|
+
sheet.add_row ['制約情報'], style: bold_style
|
88
|
+
sheet.merge_cells("A#{sheet.rows.length}:D#{sheet.rows.length}")
|
89
|
+
|
90
|
+
sheet.add_row ['No', '制約名', '種類', '制約定義'], style: header_style
|
91
|
+
|
92
|
+
if table_data[:columns].any? { |col| col[:name] == 'id' }
|
93
|
+
sheet.add_row [1, 'PRIMARY', 'PRIMARY KEY', 'id'], style: border_style
|
94
|
+
end
|
95
|
+
|
96
|
+
sheet.add_row []
|
97
|
+
|
98
|
+
# 外部キー情報
|
99
|
+
sheet.add_row ['外部キー情報'], style: bold_style
|
100
|
+
sheet.merge_cells("A#{sheet.rows.length}:E#{sheet.rows.length}")
|
101
|
+
|
102
|
+
sheet.add_row ['No', '外部キー名', 'カラムリスト', '参照先テーブル名', '参照先カラムリスト'], style: header_style
|
103
|
+
|
104
|
+
table_data[:foreign_keys].each_with_index do |fk, idx|
|
105
|
+
sheet.add_row [
|
106
|
+
idx + 1,
|
107
|
+
"fk_#{fk[:ref_table]}",
|
108
|
+
"#{fk[:ref_table]}_id",
|
109
|
+
fk[:ref_table],
|
110
|
+
'id'
|
111
|
+
], style: border_style
|
112
|
+
end
|
113
|
+
|
114
|
+
sheet.add_row []
|
115
|
+
|
116
|
+
# 外部キー情報(PK側)
|
117
|
+
sheet.add_row ['外部キー情報(PK側)'], style: bold_style
|
118
|
+
sheet.merge_cells("A#{sheet.rows.length}:E#{sheet.rows.length}")
|
119
|
+
|
120
|
+
sheet.add_row ['No', '外部キー名', 'カラムリスト', '参照元テーブル名', '参照元カラムリスト'], style: header_style
|
121
|
+
sheet.add_row []
|
122
|
+
|
123
|
+
# トリガー情報
|
124
|
+
sheet.add_row ['トリガー情報'], style: bold_style
|
125
|
+
sheet.merge_cells("A#{sheet.rows.length}:E#{sheet.rows.length}")
|
126
|
+
|
127
|
+
sheet.add_row ['No', 'トリガー名', 'イベント', 'タイミング', '条件'], style: header_style
|
128
|
+
sheet.add_row []
|
129
|
+
|
130
|
+
# RDBMS固有の情報
|
131
|
+
sheet.add_row ['RDBMS固有の情報'], style: bold_style
|
132
|
+
sheet.merge_cells("A#{sheet.rows.length}:C#{sheet.rows.length}")
|
133
|
+
|
134
|
+
sheet.add_row ['No', 'プロパティ名', 'プロパティ値'], style: header_style
|
135
|
+
|
136
|
+
[
|
137
|
+
['TABLE_CATALOG', 'def'],
|
138
|
+
['TABLE_SCHEMA', ''],
|
139
|
+
['TABLE_NAME', table_name],
|
140
|
+
['TABLE_TYPE', 'BASE TABLE'],
|
141
|
+
['ENGINE', 'InnoDB'],
|
142
|
+
['VERSION', '10'],
|
143
|
+
['ROW_FORMAT', 'Compact'],
|
144
|
+
['TABLE_ROWS', ''],
|
145
|
+
['AVG_ROW_LENGTH', ''],
|
146
|
+
['DATA_LENGTH', ''],
|
147
|
+
['MAX_DATA_LENGTH', '']
|
148
|
+
].each_with_index do |(prop_name, prop_value), idx|
|
149
|
+
sheet.add_row [idx + 1, prop_name, prop_value], style: border_style
|
150
|
+
end
|
151
|
+
|
152
|
+
# Column widths
|
153
|
+
sheet.column_widths 5, 25, 20, 20, 20, 30
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsSchemaExcel
|
4
|
+
class Parser
|
5
|
+
def self.parse(schema_file)
|
6
|
+
content = File.read(schema_file)
|
7
|
+
tables = {}
|
8
|
+
current_table = nil
|
9
|
+
|
10
|
+
content.each_line do |line|
|
11
|
+
line.strip!
|
12
|
+
|
13
|
+
if line =~ /create_table\s+"([^"]+)"/
|
14
|
+
current_table = $1
|
15
|
+
tables[current_table] = { columns: [], indexes: [], foreign_keys: [] }
|
16
|
+
next
|
17
|
+
end
|
18
|
+
|
19
|
+
if current_table && line.start_with?('t.')
|
20
|
+
if line =~ /t\.(\w+)\s+"([^"]+)"(?:,\s*(.+))?/
|
21
|
+
col_type, col_name, options = $1, $2, $3
|
22
|
+
options ||= ''
|
23
|
+
|
24
|
+
not_null = options.include?('null: false')
|
25
|
+
default = options[/default:\s*([^,]+)/, 1]&.strip || ''
|
26
|
+
|
27
|
+
tables[current_table][:columns] << {
|
28
|
+
name: col_name,
|
29
|
+
type: col_type,
|
30
|
+
not_null: not_null,
|
31
|
+
default: default
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
if line.include?('t.index')
|
36
|
+
columns = line[/\["([^"]+)"\]/, 1]
|
37
|
+
unique = line.include?('unique: true')
|
38
|
+
|
39
|
+
tables[current_table][:indexes] << {
|
40
|
+
columns: columns,
|
41
|
+
unique: unique
|
42
|
+
} if columns
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
if current_table && line.include?('foreign_key')
|
47
|
+
ref_table = line[/to_table:\s*:(\w+)/, 1]
|
48
|
+
tables[current_table][:foreign_keys] << { ref_table: ref_table } if ref_table
|
49
|
+
end
|
50
|
+
|
51
|
+
current_table = nil if line == 'end' && current_table
|
52
|
+
end
|
53
|
+
|
54
|
+
tables
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rails_schema_excel/version"
|
4
|
+
require_relative "rails_schema_excel/parser"
|
5
|
+
require_relative "rails_schema_excel/exporter"
|
6
|
+
|
7
|
+
module RailsSchemaExcel
|
8
|
+
class Error < StandardError; end
|
9
|
+
|
10
|
+
def self.export(schema_file, output_file)
|
11
|
+
tables = Parser.parse(schema_file)
|
12
|
+
Exporter.export(tables, output_file)
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_schema_excel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dang Quang Minh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: caxlsx
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: caxlsx_rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.6'
|
41
|
+
description: Convert Rails database schema to Excel format with each table as a separate
|
42
|
+
sheet in A5:SQL Mk-2 style
|
43
|
+
email:
|
44
|
+
- d-minh@ruby-dev.vn
|
45
|
+
executables:
|
46
|
+
- rails_schema_excel
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- exe/rails_schema_excel
|
53
|
+
- lib/rails_schema_excel.rb
|
54
|
+
- lib/rails_schema_excel/exporter.rb
|
55
|
+
- lib/rails_schema_excel/parser.rb
|
56
|
+
- lib/rails_schema_excel/version.rb
|
57
|
+
- sig/rails_schema_excel.rbs
|
58
|
+
homepage: https://github.com/ojisanchamchi/rails_schema_excel
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/ojisanchamchi/rails_schema_excel
|
63
|
+
source_code_uri: https://github.com/ojisanchamchi/rails_schema_excel
|
64
|
+
changelog_uri: https://github.com/ojisanchamchi/rails_schema_excel/blob/main/CHANGELOG.md
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 2.7.0
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.3.3
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Export Rails schema to Excel with A5:SQL Mk-2 format
|
84
|
+
test_files: []
|