schema_to_dbml 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.rubocop.yml +0 -3
- data/Gemfile.lock +10 -7
- 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/configs/custom_config.yml +1 -0
- data/lib/schema_to_dbml/dbml_relations_formatter.rb +9 -2
- data/lib/schema_to_dbml/formatters/fields_formatter.rb +1 -1
- data/lib/schema_to_dbml/helpers/constants.rb +25 -5
- data/lib/schema_to_dbml/version.rb +1 -1
- data/lib/schema_to_dbml.rb +7 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9032720a1b5f18280931f1c79673a0f1bd19fe11984b7648fe8a51daa4845cad
|
4
|
+
data.tar.gz: e255d62a37be7d8c1980ef5920f447993bff019119ac86cb45e2102d35a65ff1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51f4a20647f1a6a050b08e45c4d6d0a7c8be8dab2ae1ffed85bcfb94b08d002dd1291247bf6487494e661f5ce993e4378e5ef540542282bee5ae9c4096fb90f9
|
7
|
+
data.tar.gz: 84e5fdc4390ed090174685f6ac18c9f29e278bc82ea99452f5d8dccae9f874c49031daac59bc135c31a2befb990f1ad3795ec024d72bd12bea91697dff0634cc
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
CHANGED
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.3)
|
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.5)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
12
|
i18n (>= 1.6, < 2)
|
13
13
|
minitest (>= 5.1)
|
@@ -22,11 +22,13 @@ GEM
|
|
22
22
|
json (2.6.3)
|
23
23
|
minitest (5.18.0)
|
24
24
|
parallel (1.23.0)
|
25
|
-
parser (3.2.2.
|
25
|
+
parser (3.2.2.3)
|
26
26
|
ast (~> 2.4.1)
|
27
|
+
racc
|
28
|
+
racc (1.7.0)
|
27
29
|
rainbow (3.1.1)
|
28
30
|
rake (13.0.6)
|
29
|
-
regexp_parser (2.8.
|
31
|
+
regexp_parser (2.8.1)
|
30
32
|
rexml (3.2.5)
|
31
33
|
rspec (3.12.0)
|
32
34
|
rspec-core (~> 3.12.0)
|
@@ -41,17 +43,17 @@ GEM
|
|
41
43
|
diff-lcs (>= 1.2.0, < 2.0)
|
42
44
|
rspec-support (~> 3.12.0)
|
43
45
|
rspec-support (3.12.0)
|
44
|
-
rubocop (1.
|
46
|
+
rubocop (1.52.1)
|
45
47
|
json (~> 2.3)
|
46
48
|
parallel (~> 1.10)
|
47
|
-
parser (>= 3.2.
|
49
|
+
parser (>= 3.2.2.3)
|
48
50
|
rainbow (>= 2.2.2, < 4.0)
|
49
51
|
regexp_parser (>= 1.8, < 3.0)
|
50
52
|
rexml (>= 3.2.5, < 4.0)
|
51
53
|
rubocop-ast (>= 1.28.0, < 2.0)
|
52
54
|
ruby-progressbar (~> 1.7)
|
53
55
|
unicode-display_width (>= 2.4.0, < 3.0)
|
54
|
-
rubocop-ast (1.
|
56
|
+
rubocop-ast (1.29.0)
|
55
57
|
parser (>= 3.2.1.0)
|
56
58
|
ruby-progressbar (1.13.0)
|
57
59
|
simplecov (0.22.0)
|
@@ -65,6 +67,7 @@ GEM
|
|
65
67
|
unicode-display_width (2.4.2)
|
66
68
|
|
67
69
|
PLATFORMS
|
70
|
+
arm64-darwin-21
|
68
71
|
x86_64-darwin-22
|
69
72
|
x86_64-linux
|
70
73
|
|
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
|
@@ -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
|
@@ -2,12 +2,32 @@
|
|
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
|
+
)*
|
25
|
+
/x
|
9
26
|
|
10
|
-
RELATIONS_REGEXP = /
|
11
|
-
|
27
|
+
RELATIONS_REGEXP = /
|
28
|
+
add_foreign_key\s+"(?<from_table>\w+)",\s+"(?<to_table>\w+)"
|
29
|
+
(?:,\s+column:\s+"(?<column>\w+)")?
|
30
|
+
(?:,\s+on_delete:\s+:(?<on_delete>\w+))?
|
31
|
+
/x
|
12
32
|
end
|
13
33
|
end
|
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.3
|
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-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,6 +116,7 @@ executables: []
|
|
116
116
|
extensions: []
|
117
117
|
extra_rdoc_files: []
|
118
118
|
files:
|
119
|
+
- ".rspec"
|
119
120
|
- ".rubocop.yml"
|
120
121
|
- CHANGELOG.md
|
121
122
|
- CODE_OF_CONDUCT.md
|
@@ -126,6 +127,8 @@ files:
|
|
126
127
|
- README.md
|
127
128
|
- Rakefile
|
128
129
|
- lib/configuration.rb
|
130
|
+
- lib/rake_tasks/load.rb
|
131
|
+
- lib/rake_tasks/schema_to_dbml.rake
|
129
132
|
- lib/schema_to_dbml.rb
|
130
133
|
- lib/schema_to_dbml/build_dbml_content.rb
|
131
134
|
- lib/schema_to_dbml/build_dbml_file.rb
|