active-dbml 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +47 -0
- data/Rakefile +8 -0
- data/app/assets/config/active_dbml_manifest.js +1 -0
- data/app/assets/stylesheets/active/dbml/application.css +15 -0
- data/app/controllers/active/dbml/application_controller.rb +6 -0
- data/app/helpers/active/dbml/application_helper.rb +6 -0
- data/app/jobs/active/dbml/application_job.rb +6 -0
- data/app/mailers/active/dbml/application_mailer.rb +8 -0
- data/app/models/application_record.rb +3 -0
- data/app/views/layouts/active/dbml/application.html.erb +15 -0
- data/config/routes.rb +2 -0
- data/lib/active/dbml/engine.rb +7 -0
- data/lib/active/dbml/version.rb +5 -0
- data/lib/active/dbml.rb +146 -0
- data/lib/tasks/active_dbml.rake +10 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8221dbee838a7444be8ba17b7e186639411170beed4acd2f6096bc4ce5d822f2
|
4
|
+
data.tar.gz: 2ef8374f20e734b3f0c34180cce1cb542d2e875998ff0693676728b356acbfbc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 062d762e36de8e9e80996ef58f289930fb28955c5dfb67a069bb6b28030ad3188b69660b6629283b1b9e203304410b70e41b9f806ff954828cdff2ad2ae90de0
|
7
|
+
data.tar.gz: bd8626a3d86dfa3004c5495084a81164cb108d93707e1ebae45e04b8adf4bab35d3428a9d08fe60ce3f2eb9071b5d90bf8ab6466e322da93cf4875a314205a73
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Active::Dbml
|
2
|
+
|
3
|
+
ActiveDBML: A Gem for Bridging Database Design and Development
|
4
|
+
|
5
|
+
ActiveDBML is a new, useful Ruby gem designed to streamline database development. At its core, this gem provides a Rake Task that outputs Database Markup Language (DBML) from ActiveRecord. Its primary aim is to alleviate discrepancies between database definitions and actual development, ensuring consistency across all stages of development.
|
6
|
+
|
7
|
+
The purpose of developing this gem originated from the need to bridge the gap between DB definitions and development, providing a robust solution to a common problem. We hope that by making this gem publicly available, it will not only make the life of developers easier but also contribute to the broader software development community.
|
8
|
+
|
9
|
+
ActiveDBML is open for improvements. We eagerly welcome and encourage pull requests and issue submissions. We envision this gem evolving, becoming more comprehensive, and better with collective input and efforts. Our ultimate goal is to enhance this gem further, based on your valuable feedback and contributions. Join us in this journey to make ActiveDBML an essential tool in every developer's toolkit.
|
10
|
+
|
11
|
+
## Other language docs
|
12
|
+
|
13
|
+
- [English](/)
|
14
|
+
- [日本語](/docs/ja.md)
|
15
|
+
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
``` sh
|
20
|
+
$ rake active_dbml:export
|
21
|
+
|
22
|
+
# or (In the case you want to specify the export destination)
|
23
|
+
$ rake active_dbml:export["export/path.dbml"]
|
24
|
+
```
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
Add this line to your application's Gemfile:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem "active-dbml"
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
```bash
|
35
|
+
$ bundle
|
36
|
+
```
|
37
|
+
|
38
|
+
Or install it yourself as:
|
39
|
+
```bash
|
40
|
+
$ gem install active-dbml
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
Contribution directions go here.
|
45
|
+
|
46
|
+
## License
|
47
|
+
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 @@
|
|
1
|
+
//= link_directory ../stylesheets/active/dbml .css
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
data/config/routes.rb
ADDED
data/lib/active/dbml.rb
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
require "active/dbml/version"
|
2
|
+
require "active/dbml/engine"
|
3
|
+
|
4
|
+
module Active
|
5
|
+
module Dbml
|
6
|
+
def self.export_dbml(export_path)
|
7
|
+
dbml_file = File.new(export_path, 'w+')
|
8
|
+
models = get_model_list
|
9
|
+
dbml = generate_dbml(models)
|
10
|
+
dbml_file.puts(dbml)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.generate_dbml(models)
|
14
|
+
dbml_output = []
|
15
|
+
# 出力済みのEnumを追跡
|
16
|
+
enums_generated = Set.new
|
17
|
+
|
18
|
+
models.each do |model|
|
19
|
+
if ActiveRecord::Base.connection.table_exists?(model.table_name)
|
20
|
+
primary_key = get_primary_key(model)
|
21
|
+
foreign_keys = get_foreign_keys(model)
|
22
|
+
|
23
|
+
dbml_output << "Table #{model.table_name} {"
|
24
|
+
|
25
|
+
# Enumを取得
|
26
|
+
defined_enums = model.defined_enums.keys
|
27
|
+
|
28
|
+
# カラム情報を生成
|
29
|
+
generate_columns(dbml_output, model, primary_key, foreign_keys, defined_enums)
|
30
|
+
|
31
|
+
# Index情報の取得
|
32
|
+
generate_indexes(dbml_output, model)
|
33
|
+
|
34
|
+
# テーブルコメントの取得
|
35
|
+
generate_table_comment(dbml_output, model)
|
36
|
+
|
37
|
+
# テーブルを閉じる
|
38
|
+
dbml_output << "}\n"
|
39
|
+
|
40
|
+
# Enumの詳細を取得
|
41
|
+
generate_enums(dbml_output, model, enums_generated)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
dbml_output.join("\n")
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def self.get_primary_key(model)
|
50
|
+
model.primary_key
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.get_foreign_keys(model)
|
54
|
+
foreign_keys = {}
|
55
|
+
|
56
|
+
model.reflect_on_all_associations.each do |association|
|
57
|
+
case association.macro
|
58
|
+
when :belongs_to
|
59
|
+
foreign_key = association.options[:foreign_key]
|
60
|
+
foreign_key_destination = "#{association.plural_name}.#{association.class_name.constantize.primary_key}"
|
61
|
+
|
62
|
+
association.class_name.constantize.reflect_on_all_associations.each do |relation_association|
|
63
|
+
if model.table_name.eql?(relation_association.plural_name)
|
64
|
+
case relation_association.macro
|
65
|
+
when :has_one
|
66
|
+
foreign_keys[foreign_key] = { destination: foreign_key_destination, relation_type: '-' }
|
67
|
+
when :has_many
|
68
|
+
foreign_keys[foreign_key] = { destination: foreign_key_destination, relation_type: '>' }
|
69
|
+
else
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
foreign_keys
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.generate_columns(dbml_output, model, primary_key, foreign_keys, defined_enums)
|
80
|
+
model.columns.each do |column|
|
81
|
+
options = []
|
82
|
+
options.push 'pk' if primary_key.eql?(column.name)
|
83
|
+
options.push 'not null' if !column.null
|
84
|
+
options.push "note: '#{column.comment}'" if column.comment.present?
|
85
|
+
options.push "ref: #{foreign_keys[column.name][:relation_type]} #{foreign_keys[column.name][:destination]}" if foreign_keys.key?(column.name)
|
86
|
+
|
87
|
+
unless defined_enums.include?(column.name)
|
88
|
+
dbml_output << " #{column.name} #{check_sql_type(column.sql_type)} #{"[#{options.join(', ')}]" if options.present?}"
|
89
|
+
else
|
90
|
+
dbml_output << " #{column.name} #{column.name} #{"[#{options.join(', ')}]" if options.present?}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.generate_indexes(dbml_output, model)
|
96
|
+
indexes = model.connection.indexes(model.table_name)
|
97
|
+
if indexes.present?
|
98
|
+
dbml_output << "\n Indexes {"
|
99
|
+
indexes.each do |index|
|
100
|
+
dbml_output << " #{index.columns[0]} [name: '#{index.name}']"
|
101
|
+
end
|
102
|
+
dbml_output << " }"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.generate_table_comment(dbml_output, model)
|
107
|
+
table_comment = model.connection.table_comment(model.table_name)
|
108
|
+
if table_comment.present?
|
109
|
+
dbml_output << "\n Note: '# #{table_comment}'"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.generate_enums(dbml_output, model, enums_generated)
|
114
|
+
model.defined_enums.each do |key, value|
|
115
|
+
# Enumがまだ出力されていない場合のみ出力
|
116
|
+
if key.present? && !enums_generated.include?(key)
|
117
|
+
dbml_output << "Enum #{key} {"
|
118
|
+
value.keys.each do |value_key|
|
119
|
+
dbml_output << " #{value_key}"
|
120
|
+
end
|
121
|
+
|
122
|
+
dbml_output << "}\n"
|
123
|
+
# Enumを出力済みとして記録
|
124
|
+
enums_generated.add(key)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.check_sql_type(sql_type)
|
130
|
+
case sql_type
|
131
|
+
when 'tinyint(1)'
|
132
|
+
return 'boolean'
|
133
|
+
when 'datetime(6)'
|
134
|
+
return 'datetime'
|
135
|
+
else
|
136
|
+
return sql_type
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def self.get_model_list
|
141
|
+
Rails.application.eager_load! unless Rails.configuration.cache_classes
|
142
|
+
# ApplicationRecord.descendants
|
143
|
+
ActiveRecord::Base.descendants
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active/dbml'
|
2
|
+
|
3
|
+
namespace :active_dbml do
|
4
|
+
desc 'Export DBML'
|
5
|
+
task :export, [:export_path] => :environment do |t, args|
|
6
|
+
args.with_defaults(export_path: 'db/export.dbml')
|
7
|
+
Active::Dbml.export_dbml(args['export_path'])
|
8
|
+
puts "📣 Successfully exported DBML to #{args['export_path']}!"
|
9
|
+
end
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active-dbml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ukmshi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-05-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '7'
|
27
|
+
description: Active::DBML is a new, useful Ruby gem designed to streamline database
|
28
|
+
development.
|
29
|
+
email:
|
30
|
+
- ukyo.mashiko@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- app/assets/config/active_dbml_manifest.js
|
39
|
+
- app/assets/stylesheets/active/dbml/application.css
|
40
|
+
- app/controllers/active/dbml/application_controller.rb
|
41
|
+
- app/helpers/active/dbml/application_helper.rb
|
42
|
+
- app/jobs/active/dbml/application_job.rb
|
43
|
+
- app/mailers/active/dbml/application_mailer.rb
|
44
|
+
- app/models/application_record.rb
|
45
|
+
- app/views/layouts/active/dbml/application.html.erb
|
46
|
+
- config/routes.rb
|
47
|
+
- lib/active/dbml.rb
|
48
|
+
- lib/active/dbml/engine.rb
|
49
|
+
- lib/active/dbml/version.rb
|
50
|
+
- lib/tasks/active_dbml.rake
|
51
|
+
homepage: https://github.com/ukmshi/ActiveDBML
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://github.com/ukmshi/ActiveDBML
|
56
|
+
source_code_uri: https://github.com/ukmshi/ActiveDBML
|
57
|
+
changelog_uri: https://github.com/ukmshi/ActiveDBML/CHANGELOG.md
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.3.20
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: 'Active::DBML: A Gem for Bridging Database Design and Development'
|
77
|
+
test_files: []
|