schema_to_dbml 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d437e4d75d3d034a51e6a63d2e5f56ce5b8a95364fd785f354e5ffcad7e5956
4
- data.tar.gz: b16dd77e8f191da41fb875582873aeb0c7d53cfa9029c47b805733838585f42b
3
+ metadata.gz: 9032720a1b5f18280931f1c79673a0f1bd19fe11984b7648fe8a51daa4845cad
4
+ data.tar.gz: e255d62a37be7d8c1980ef5920f447993bff019119ac86cb45e2102d35a65ff1
5
5
  SHA512:
6
- metadata.gz: 3cb923fdc024dadc60b8d9e309895dbebd0d471c3f8fc0b50e85b5c27ea09df3b771741d8eb0c4a9ae92dc3ae174ce6043661cc95fa7adc2f2697acc51b08a36
7
- data.tar.gz: afa0177a0764031887e62fc0feaea2019c8faf2693b289fdf6794db04064abbfe61aed045b7bf40903b8463c26247a575c5a096b5aae1a351c4ffe84cdcce150
6
+ metadata.gz: 51f4a20647f1a6a050b08e45c4d6d0a7c8be8dab2ae1ffed85bcfb94b08d002dd1291247bf6487494e661f5ce993e4378e5ef540542282bee5ae9c4096fb90f9
7
+ data.tar.gz: 84e5fdc4390ed090174685f6ac18c9f29e278bc82ea99452f5d8dccae9f874c49031daac59bc135c31a2befb990f1ad3795ec024d72bd12bea91697dff0634cc
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
 
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema_to_dbml (0.0.2)
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.4.3)
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.1)
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.0)
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.51.0)
46
+ rubocop (1.52.1)
45
47
  json (~> 2.3)
46
48
  parallel (~> 1.10)
47
- parser (>= 3.2.0.0)
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.28.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 output the generated DBML content.
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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+
5
+ Dir[File.join(File.dirname(__FILE__), '**/*.rake')].each do |rake|
6
+ load rake
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :schema_to_dbml do
4
+ desc 'build dmbl.yml'
5
+ task :generate, %i[schema_path] => [:environment] do |_t, args|
6
+ schema = args[:schema_path]
7
+ SchemaToDbml.new.generate(schema:)
8
+ end
9
+ end
@@ -9,3 +9,4 @@ custom_project_notes: |
9
9
  - Provides meaningful project information and descriptions
10
10
  custom_primary_key: "id integer [pk, unique, note: 'Unique identifier and primary key']"
11
11
  custom_dbml_content: ''
12
+ custom_dbml_file_path: 'db/schema.dbml'
@@ -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 += " [delete: #{on_delete}]" if on_delete
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 += "_#{column}" if column != default_foreign_key_column(to_table)
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
@@ -41,7 +41,7 @@ module Formatters
41
41
  def format_comment(comment:)
42
42
  return '' if comment.to_s.empty?
43
43
 
44
- "note: '#{comment}'"
44
+ "note: '#{comment.gsub("'", "\\\\'")}'"
45
45
  end
46
46
  end
47
47
  end
@@ -2,12 +2,32 @@
2
2
 
3
3
  module Helpers
4
4
  module Constants
5
- # rubocop:disable Layout/LineLength
6
- TABLES_REGEXP = /create_table\s+"(?<table_name>\w+)"(?:,\s+comment:\s+"(?<comment>.*?)")?(?:,\s+force:\s+:cascade)?\s+do\s+\|t\|\n(?<table_content>(?:.*?)(?:".*?")*.*?)(?<=\n)\s+end/m
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 = /t\.(?<type>\w+)\s+"(?<name>\w+)"(?:,\s+(?<default>default:[^,\n]+?(?=(?:,\s)|$)))?(?:,\s+(?<null>null:\s+\w+))?(?:,\s+comment:\s+"(?<comment>[^"]+)")?(?:,\s+precision:\s+(?<precision>\d+))?(?:,\s+array:\s+(?<array>true|false))?.*/
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 = /add_foreign_key\s+"(?<from_table>\w+)",\s+"(?<to_table>\w+)"(?:,\s+column:\s+"(?<column>\w+)")?(?:,\s+on_delete:\s+:(?<on_delete>\w+))?/
11
- # rubocop:enable Layout/LineLength
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class SchemaToDbml
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -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.2
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-05-18 00:00:00.000000000 Z
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