schema_to_dbml 0.0.2 → 0.0.4
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/.rspec +1 -0
- data/.rubocop.yml +1 -4
- data/.ruby-version +1 -0
- data/Gemfile.lock +14 -9
- data/README.md +5 -2
- data/lib/configuration.rb +5 -3
- data/lib/rake_tasks/load.rb +7 -0
- data/lib/rake_tasks/schema_to_dbml.rake +9 -0
- data/lib/schema_to_dbml/build_dbml_file.rb +2 -2
- data/lib/schema_to_dbml/configs/custom_config.yml +1 -0
- data/lib/schema_to_dbml/dbml_relations_formatter.rb +9 -2
- data/lib/schema_to_dbml/dbml_tables_formatter.rb +2 -2
- data/lib/schema_to_dbml/formatters/fields_formatter.rb +12 -2
- data/lib/schema_to_dbml/helpers/constants.rb +26 -5
- data/lib/schema_to_dbml/schema_converter.rb +2 -2
- data/lib/schema_to_dbml/version.rb +1 -1
- data/lib/schema_to_dbml.rb +7 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01e5c423d835476a25d170272a5d89e762f051951176a21df6b6d62235b0a9c0
|
4
|
+
data.tar.gz: 51a2746ca61db2fae5125b0cf368c4bbca195d7f042b07b5444740e393c0a381
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aaefc5b170b0cb209df1b8ab12f80ae7293eb9859cc5470d07af3a84cc55a6546917e23c401de24ba17dc630bd08e297b901af838d6fceac5340d69ed4fe277
|
7
|
+
data.tar.gz: bd6ae1da9afff47e053d851e538a3e3e36a266dc822a98ddfd1b2b81989b814100ba0a6dece7e3c47cadb538530315e582d042fe6bc193e1a36a01dba41cad96
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
CHANGED
@@ -69,9 +69,6 @@ Style/RedundantAssignment:
|
|
69
69
|
Style/RedundantFetchBlock:
|
70
70
|
Enabled: true
|
71
71
|
|
72
|
-
Style/Documentation:
|
73
|
-
Enabled: false
|
74
|
-
|
75
72
|
Style/MethodCallWithArgsParentheses:
|
76
73
|
Enabled: false
|
77
74
|
|
@@ -79,4 +76,4 @@ Gemspec/DevelopmentDependencies:
|
|
79
76
|
Enabled: false
|
80
77
|
|
81
78
|
Metrics/ParameterLists:
|
82
|
-
Max:
|
79
|
+
Max: 8
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
schema_to_dbml (0.0.
|
4
|
+
schema_to_dbml (0.0.4)
|
5
5
|
activesupport (>= 6.1.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (7.0.
|
10
|
+
activesupport (7.0.6)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
12
|
i18n (>= 1.6, < 2)
|
13
13
|
minitest (>= 5.1)
|
@@ -17,16 +17,19 @@ GEM
|
|
17
17
|
concurrent-ruby (1.2.2)
|
18
18
|
diff-lcs (1.5.0)
|
19
19
|
docile (1.4.0)
|
20
|
-
i18n (1.
|
20
|
+
i18n (1.14.1)
|
21
21
|
concurrent-ruby (~> 1.0)
|
22
22
|
json (2.6.3)
|
23
|
-
|
23
|
+
language_server-protocol (3.17.0.3)
|
24
|
+
minitest (5.18.1)
|
24
25
|
parallel (1.23.0)
|
25
|
-
parser (3.2.2.
|
26
|
+
parser (3.2.2.3)
|
26
27
|
ast (~> 2.4.1)
|
28
|
+
racc
|
29
|
+
racc (1.7.1)
|
27
30
|
rainbow (3.1.1)
|
28
31
|
rake (13.0.6)
|
29
|
-
regexp_parser (2.8.
|
32
|
+
regexp_parser (2.8.1)
|
30
33
|
rexml (3.2.5)
|
31
34
|
rspec (3.12.0)
|
32
35
|
rspec-core (~> 3.12.0)
|
@@ -41,17 +44,18 @@ GEM
|
|
41
44
|
diff-lcs (>= 1.2.0, < 2.0)
|
42
45
|
rspec-support (~> 3.12.0)
|
43
46
|
rspec-support (3.12.0)
|
44
|
-
rubocop (1.
|
47
|
+
rubocop (1.54.0)
|
45
48
|
json (~> 2.3)
|
49
|
+
language_server-protocol (>= 3.17.0)
|
46
50
|
parallel (~> 1.10)
|
47
|
-
parser (>= 3.2.
|
51
|
+
parser (>= 3.2.2.3)
|
48
52
|
rainbow (>= 2.2.2, < 4.0)
|
49
53
|
regexp_parser (>= 1.8, < 3.0)
|
50
54
|
rexml (>= 3.2.5, < 4.0)
|
51
55
|
rubocop-ast (>= 1.28.0, < 2.0)
|
52
56
|
ruby-progressbar (~> 1.7)
|
53
57
|
unicode-display_width (>= 2.4.0, < 3.0)
|
54
|
-
rubocop-ast (1.
|
58
|
+
rubocop-ast (1.29.0)
|
55
59
|
parser (>= 3.2.1.0)
|
56
60
|
ruby-progressbar (1.13.0)
|
57
61
|
simplecov (0.22.0)
|
@@ -65,6 +69,7 @@ GEM
|
|
65
69
|
unicode-display_width (2.4.2)
|
66
70
|
|
67
71
|
PLATFORMS
|
72
|
+
arm64-darwin-21
|
68
73
|
x86_64-darwin-22
|
69
74
|
x86_64-linux
|
70
75
|
|
data/README.md
CHANGED
@@ -35,11 +35,13 @@ require 'schema_to_dbml'
|
|
35
35
|
# Load configuration from default file
|
36
36
|
SchemaToDbml.configuration
|
37
37
|
|
38
|
+
# This will output the generated DBML content
|
38
39
|
dbml_content = SchemaToDbml.new.convert(schema: 'db/schema.rb')
|
39
40
|
puts dbml_content
|
40
|
-
```
|
41
41
|
|
42
|
-
This will
|
42
|
+
# This will generate the file (db/schema.dbml) with the above content
|
43
|
+
SchemaToDbml.new.generate(schema: 'db/schema.rb')
|
44
|
+
```
|
43
45
|
|
44
46
|
## Custom Configuration
|
45
47
|
|
@@ -58,6 +60,7 @@ custom_project_notes: |
|
|
58
60
|
- Provides meaningful project information and descriptions
|
59
61
|
custom_primary_key: "id integer [pk, unique, note: 'Unique identifier and primary key']"
|
60
62
|
custom_dbml_content: ''
|
63
|
+
custom_dbml_file_path: 'db/schema.dbml'
|
61
64
|
```
|
62
65
|
|
63
66
|
You can change the properties as you want. After that, you can load the configuration by calling:
|
data/lib/configuration.rb
CHANGED
@@ -4,14 +4,16 @@ class Configuration
|
|
4
4
|
def configure_from_hash(yaml_data)
|
5
5
|
@custom_database_type = yaml_data['custom_database_type']
|
6
6
|
@custom_dbml_content = yaml_data['custom_dbml_content']
|
7
|
+
@custom_dbml_file_path = yaml_data['custom_dbml_file_path']
|
8
|
+
@custom_primary_key = yaml_data['custom_primary_key']
|
7
9
|
@custom_project_name = yaml_data['custom_project_name']
|
8
10
|
@custom_project_notes = yaml_data['custom_project_notes']
|
9
|
-
@custom_primary_key = yaml_data['custom_primary_key']
|
10
11
|
end
|
11
12
|
|
12
13
|
attr_accessor :custom_database_type,
|
13
14
|
:custom_dbml_content,
|
15
|
+
:custom_dbml_file_path,
|
16
|
+
:custom_primary_key,
|
14
17
|
:custom_project_name,
|
15
|
-
:custom_project_notes
|
16
|
-
:custom_primary_key
|
18
|
+
:custom_project_notes
|
17
19
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'helpers/constants'
|
4
|
-
require_relative '
|
5
|
-
require_relative '
|
4
|
+
require_relative 'dbml_tables_formatter'
|
5
|
+
require_relative 'dbml_relations_formatter'
|
6
6
|
|
7
7
|
class SchemaConverter
|
8
8
|
include Helpers::Constants
|
@@ -17,7 +17,7 @@ class DbmlRelationsFormatter
|
|
17
17
|
ref_name = generate_reference_name(from_table, to_table, column)
|
18
18
|
ref = build_reference_string(ref_name, from_table, column, to_table)
|
19
19
|
|
20
|
-
ref
|
20
|
+
format_on_delete(ref, on_delete)
|
21
21
|
ref
|
22
22
|
end
|
23
23
|
|
@@ -25,7 +25,7 @@ class DbmlRelationsFormatter
|
|
25
25
|
|
26
26
|
def generate_reference_name(from_table, to_table, column)
|
27
27
|
ref_name = "fk_rails_#{from_table}_#{to_table}"
|
28
|
-
ref_name
|
28
|
+
ref_name << "_#{column}" if column != default_foreign_key_column(to_table)
|
29
29
|
ref_name
|
30
30
|
end
|
31
31
|
|
@@ -36,4 +36,11 @@ class DbmlRelationsFormatter
|
|
36
36
|
def build_reference_string(ref_name, from_table, column, to_table)
|
37
37
|
"Ref #{ref_name}:#{from_table}.#{column} - #{to_table}.id"
|
38
38
|
end
|
39
|
+
|
40
|
+
def format_on_delete(ref, on_delete)
|
41
|
+
return unless on_delete
|
42
|
+
|
43
|
+
on_delete = 'set null' if on_delete == 'nullify'
|
44
|
+
ref << " [delete: #{on_delete}]"
|
45
|
+
end
|
39
46
|
end
|
@@ -22,11 +22,11 @@ class DbmlTablesFormatter
|
|
22
22
|
def build_columns(parsed_columns)
|
23
23
|
columns = []
|
24
24
|
|
25
|
-
parsed_columns.scan(COLUMNS_REGEXP).each do |type, name, default, null, comment, _precision, array|
|
25
|
+
parsed_columns.scan(COLUMNS_REGEXP).each do |type, name, default, null, comment, _precision, array, limit|
|
26
26
|
formatted_comment = format_comment(comment:)
|
27
27
|
formatted_default = format_default(default:)
|
28
28
|
formatted_null = format_null(null:)
|
29
|
-
formatted_type = format_type(type:, array:)
|
29
|
+
formatted_type = format_type(type:, array:, limit:)
|
30
30
|
|
31
31
|
final_values = [formatted_default, formatted_null, formatted_comment].compact.reject(&:empty?)
|
32
32
|
columns << build_line(name, formatted_type, final_values)
|
@@ -5,6 +5,10 @@ require_relative 'default_field_formatter_helper'
|
|
5
5
|
module Formatters
|
6
6
|
module FieldsFormatter
|
7
7
|
include DefaultFieldFormatterHelper
|
8
|
+
COMMENT_MAPPER = [
|
9
|
+
{ from: "'", to: "\\\\'" },
|
10
|
+
{ from: '\"', to: '"' }
|
11
|
+
].freeze
|
8
12
|
TYPE_MAPPER = {
|
9
13
|
string: 'varchar',
|
10
14
|
integer: 'int',
|
@@ -12,12 +16,13 @@ module Formatters
|
|
12
16
|
datetime: 'timestamp'
|
13
17
|
}.freeze
|
14
18
|
|
15
|
-
def format_type(type:, array:)
|
19
|
+
def format_type(type:, array:, limit:)
|
16
20
|
return '' if type.to_s.empty?
|
17
21
|
|
18
22
|
parsed = TYPE_MAPPER[type.to_sym] || type.to_s
|
19
23
|
|
20
24
|
return "#{parsed}[]" if array == 'true'
|
25
|
+
return "#{parsed}(#{limit})" if limit
|
21
26
|
|
22
27
|
parsed
|
23
28
|
end
|
@@ -41,7 +46,12 @@ module Formatters
|
|
41
46
|
def format_comment(comment:)
|
42
47
|
return '' if comment.to_s.empty?
|
43
48
|
|
44
|
-
|
49
|
+
note = comment.dup
|
50
|
+
COMMENT_MAPPER.each do |mapper|
|
51
|
+
note.gsub!(mapper[:from], mapper[:to])
|
52
|
+
end
|
53
|
+
|
54
|
+
"note: '#{note}'"
|
45
55
|
end
|
46
56
|
end
|
47
57
|
end
|
@@ -2,12 +2,33 @@
|
|
2
2
|
|
3
3
|
module Helpers
|
4
4
|
module Constants
|
5
|
-
|
6
|
-
|
5
|
+
TABLES_REGEXP = /
|
6
|
+
create_table\s+"(?<table_name>\w+)"
|
7
|
+
(?:,\s+comment:\s+"(?<comment>.*?)")?
|
8
|
+
(?:,\s+force:\s+:cascade)?
|
9
|
+
\s+do\s+\|t\|
|
10
|
+
\n
|
11
|
+
(?<table_content>(?:.*?)(?:".*?")*.*?)
|
12
|
+
(?<=\n)
|
13
|
+
\s+end
|
14
|
+
/xm
|
7
15
|
|
8
|
-
COLUMNS_REGEXP = /
|
16
|
+
COLUMNS_REGEXP = /
|
17
|
+
t\.(?<type>\w+)\s+"(?<name>\w+)"
|
18
|
+
(?:
|
19
|
+
(?:,\s+(?<default>default:[^,\n]+?(?=(?:,\s)|$)))?
|
20
|
+
(?:,\s+(?<null>null:\s+\w+))?
|
21
|
+
(?:,\s+comment:\s+"(?<comment>[^"\\]*(?:\\.[^"\\]*)*)")?
|
22
|
+
(?:,\s+precision:\s+(?<precision>\d+))?
|
23
|
+
(?:,\s+array:\s+(?<array>true|false))?
|
24
|
+
(?:,\s+limit:\s+(?<limit>\d+))?
|
25
|
+
)*
|
26
|
+
/x
|
9
27
|
|
10
|
-
RELATIONS_REGEXP = /
|
11
|
-
|
28
|
+
RELATIONS_REGEXP = /
|
29
|
+
add_foreign_key\s+"(?<from_table>\w+)",\s+"(?<to_table>\w+)"
|
30
|
+
(?:,\s+column:\s+"(?<column>\w+)")?
|
31
|
+
(?:,\s+on_delete:\s+:(?<on_delete>\w+))?
|
32
|
+
/x
|
12
33
|
end
|
13
34
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'helpers/constants'
|
4
|
-
require_relative '
|
5
|
-
require_relative '
|
4
|
+
require_relative 'dbml_tables_formatter'
|
5
|
+
require_relative 'dbml_relations_formatter'
|
6
6
|
|
7
7
|
class SchemaConverter
|
8
8
|
include Helpers::Constants
|
data/lib/schema_to_dbml.rb
CHANGED
@@ -8,6 +8,7 @@ require_relative 'schema_to_dbml/errors/schema_file_not_found_error'
|
|
8
8
|
require_relative 'schema_to_dbml/errors/configuration_file_not_found_error'
|
9
9
|
require_relative 'schema_to_dbml/schema_converter'
|
10
10
|
require_relative 'schema_to_dbml/version'
|
11
|
+
require_relative 'rake_tasks/load'
|
11
12
|
|
12
13
|
class SchemaToDbml
|
13
14
|
DEFAULT_CONFIG_FILE = File.join(__dir__, 'schema_to_dbml/configs/custom_config.yml')
|
@@ -43,6 +44,12 @@ class SchemaToDbml
|
|
43
44
|
@schema_converter = schema_converter
|
44
45
|
end
|
45
46
|
|
47
|
+
def generate(schema:)
|
48
|
+
content = convert(schema:)
|
49
|
+
|
50
|
+
File.write(self.class.configuration.custom_dbml_file_path, content)
|
51
|
+
end
|
52
|
+
|
46
53
|
def convert(schema:)
|
47
54
|
converted = schema_converter.convert(schema_content: schema_content(schema))
|
48
55
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_to_dbml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Ribeiro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,7 +116,9 @@ executables: []
|
|
116
116
|
extensions: []
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
|
+
- ".rspec"
|
119
120
|
- ".rubocop.yml"
|
121
|
+
- ".ruby-version"
|
120
122
|
- CHANGELOG.md
|
121
123
|
- CODE_OF_CONDUCT.md
|
122
124
|
- CONTRIBUTE.md
|
@@ -126,6 +128,8 @@ files:
|
|
126
128
|
- README.md
|
127
129
|
- Rakefile
|
128
130
|
- lib/configuration.rb
|
131
|
+
- lib/rake_tasks/load.rb
|
132
|
+
- lib/rake_tasks/schema_to_dbml.rake
|
129
133
|
- lib/schema_to_dbml.rb
|
130
134
|
- lib/schema_to_dbml/build_dbml_content.rb
|
131
135
|
- lib/schema_to_dbml/build_dbml_file.rb
|