schema_to_dbml 0.0.1 → 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: 1d1c7e55da915704ade42e031926565a591aa815626fcf0f54ef9f75a9e85875
4
- data.tar.gz: 80e1485742492b48126ab3ef28023b72c67f86d1512f97f751b9c7bd05ff1180
3
+ metadata.gz: 9032720a1b5f18280931f1c79673a0f1bd19fe11984b7648fe8a51daa4845cad
4
+ data.tar.gz: e255d62a37be7d8c1980ef5920f447993bff019119ac86cb45e2102d35a65ff1
5
5
  SHA512:
6
- metadata.gz: c4e534e1ed44995890a6d4cfa425c5157e83d9b29299a77783c4d7a1e427fd55732410c915262f71e02a274a4390315ca548c2ce73ec585f7d3c5ca4b4fcdc8a
7
- data.tar.gz: fa702214c60f1e94c587c4509d94816ce2cebd0e1ad441441dfe5a4cbb634018d56ae0ebc49ffe87e5fcbb5c89273b62effdd34021bc65e0601ab8a5d77a6e23
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/CONTRIBUTE.md ADDED
@@ -0,0 +1,68 @@
1
+ # Contributing to `schema_to_dbml`
2
+
3
+ Welcome! We're excited that you're interested in contributing to `schema_to_dbml`, an open-source project. By contributing, you can help improve the project, fix bugs, add new features, and enhance its overall quality.
4
+
5
+ The following guidelines will help you understand how you can contribute effectively. We encourage you to read them carefully before getting started.
6
+
7
+ ## Ways to Contribute
8
+
9
+ There are several ways in which you can contribute to the project:
10
+
11
+ 1. **Reporting Issues**: If you come across any bugs or have suggestions for improvements, please open an issue on the project's GitHub repository. Make sure to provide as much information as possible, including steps to reproduce the issue and any relevant error messages.
12
+
13
+ 2. **Submitting Pull Requests (PRs)**: You can help by fixing bugs, adding new features, or improving the existing codebase. If you're planning to work on a significant change, it's best to open an issue first to discuss it with the maintainers and ensure it aligns with the project's goals.
14
+
15
+ ## Getting Started
16
+
17
+ To contribute code changes to `schema_to_dbml`, follow these steps:
18
+
19
+ 1. Fork the project repository to your GitHub account.
20
+
21
+ 2. Clone the forked repository to your local machine:
22
+ ```
23
+ git clone https://github.com/your-username/schema_to_dbml.git
24
+ ```
25
+
26
+ 3. Create a new branch for your changes:
27
+ ```
28
+ git checkout -b my-branch-name
29
+ ```
30
+
31
+ 4. Make the necessary changes and improvements to the codebase.
32
+
33
+ 5. Test your changes thoroughly to ensure they work as expected.
34
+
35
+ 6. Commit your changes with a descriptive commit message:
36
+ ```
37
+ git commit -m "Add feature XYZ"
38
+ ```
39
+
40
+ 7. Push your changes to your forked repository:
41
+ ```
42
+ git push origin my-branch-name
43
+ ```
44
+
45
+ 8. Open a pull request (PR) on the original repository. Provide a detailed description of your changes, including the problem they solve or the feature they add. Also, reference any related issues in the PR description.
46
+
47
+ ## Code Guidelines
48
+
49
+ To maintain a consistent codebase, please adhere to the following guidelines:
50
+
51
+ - Follow the existing code style and naming conventions used in the project.
52
+ - Write clear and concise code and documentation.
53
+ - Add tests for any new features or modifications to existing functionality.
54
+ - Ensure that all tests pass before submitting your changes.
55
+
56
+ ## Communication
57
+
58
+ The project's GitHub repository is the primary channel for communication. Feel free to open issues for bug reports, feature requests, or general discussions related to the project.
59
+
60
+ ## Code of Conduct
61
+
62
+ Please note that this project follows a [Code of Conduct](https://github.com/ricardojcribeiro/schema_to_dbml/blob/develop/CODE_OF_CONDUCT.md). We expect all contributors to adhere to the code when participating in the project.
63
+
64
+ ## Acknowledgments
65
+
66
+ Contributors play a vital role in the success of any open-source project. Your efforts and contributions will be acknowledged in the project's documentation and release notes.
67
+
68
+ Thank you for your interest in contributing to `schema_to_dbml`. Together, we can make the project even better!
data/Gemfile.lock CHANGED
@@ -1,22 +1,34 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema_to_dbml (0.0.1)
4
+ schema_to_dbml (0.0.3)
5
+ activesupport (>= 6.1.0)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ activesupport (7.0.5)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
9
15
  ast (2.4.2)
10
16
  byebug (11.1.3)
17
+ concurrent-ruby (1.2.2)
11
18
  diff-lcs (1.5.0)
12
19
  docile (1.4.0)
20
+ i18n (1.13.0)
21
+ concurrent-ruby (~> 1.0)
13
22
  json (2.6.3)
14
- parallel (1.22.1)
15
- parser (3.2.2.0)
23
+ minitest (5.18.0)
24
+ parallel (1.23.0)
25
+ parser (3.2.2.3)
16
26
  ast (~> 2.4.1)
27
+ racc
28
+ racc (1.7.0)
17
29
  rainbow (3.1.1)
18
30
  rake (13.0.6)
19
- regexp_parser (2.7.0)
31
+ regexp_parser (2.8.1)
20
32
  rexml (3.2.5)
21
33
  rspec (3.12.0)
22
34
  rspec-core (~> 3.12.0)
@@ -31,17 +43,17 @@ GEM
31
43
  diff-lcs (>= 1.2.0, < 2.0)
32
44
  rspec-support (~> 3.12.0)
33
45
  rspec-support (3.12.0)
34
- rubocop (1.49.0)
46
+ rubocop (1.52.1)
35
47
  json (~> 2.3)
36
48
  parallel (~> 1.10)
37
- parser (>= 3.2.0.0)
49
+ parser (>= 3.2.2.3)
38
50
  rainbow (>= 2.2.2, < 4.0)
39
51
  regexp_parser (>= 1.8, < 3.0)
40
52
  rexml (>= 3.2.5, < 4.0)
41
53
  rubocop-ast (>= 1.28.0, < 2.0)
42
54
  ruby-progressbar (~> 1.7)
43
55
  unicode-display_width (>= 2.4.0, < 3.0)
44
- rubocop-ast (1.28.0)
56
+ rubocop-ast (1.29.0)
45
57
  parser (>= 3.2.1.0)
46
58
  ruby-progressbar (1.13.0)
47
59
  simplecov (0.22.0)
@@ -50,10 +62,14 @@ GEM
50
62
  simplecov_json_formatter (~> 0.1)
51
63
  simplecov-html (0.12.3)
52
64
  simplecov_json_formatter (0.1.4)
65
+ tzinfo (2.0.6)
66
+ concurrent-ruby (~> 1.0)
53
67
  unicode-display_width (2.4.2)
54
68
 
55
69
  PLATFORMS
70
+ arm64-darwin-21
56
71
  x86_64-darwin-22
72
+ x86_64-linux
57
73
 
58
74
  DEPENDENCIES
59
75
  bundler
data/README.md CHANGED
@@ -35,55 +35,80 @@ 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
 
46
- If you want to customize the DBML content, you can create a YAML configuration file with your desired properties.
47
- By default, the SchemaToDbml will try to load the configuration from `schema_to_dbml/configs/custom_config.yml`.
48
-
49
- Here's an example of the configuration file:
48
+ If you want to customize the DBML content, you can create a YAML configuration file with your desired properties.
49
+ By default, the SchemaToDbml will load the below default configuration
50
50
 
51
51
  ```yaml
52
- custom_project_name: 'My Project'
53
- custom_database_type: 'PostgreSQL'
54
- custom_project_notes: 'This is my project.'
55
- custom_primary_key: 'id [pk]'
52
+ custom_project_name: 'dbml_database_definition'
53
+ custom_database_type: "'PostgreSQL'"
54
+ custom_project_notes: |
55
+ # My Project Notes
56
+ This is a **project** that documents the database. Here are some key points:
57
+
58
+ - Utilizes the custom primary key for better indexing
59
+ - Specifies the appropriate database type (e.g., PostgreSQL)
60
+ - Provides meaningful project information and descriptions
61
+ custom_primary_key: "id integer [pk, unique, note: 'Unique identifier and primary key']"
62
+ custom_dbml_content: ''
63
+ custom_dbml_file_path: 'db/schema.dbml'
56
64
  ```
57
65
 
58
66
  You can change the properties as you want. After that, you can load the configuration by calling:
59
67
 
60
68
  ```ruby
61
- SchemaToDbml.load_configuration_from_yaml('/path/to/your/custom_config.yml')
69
+ SchemaToDbml.load_configuration_from_yaml(file_path: '/path/to/your/custom_config.yml')
62
70
  ```
63
71
 
64
- Or you can pass the configuration directly:
72
+ Example of custom yaml configurations:
65
73
 
66
74
  ```ruby
67
- config = Configuration.new
68
- config.custom_project_name = 'My Project'
69
- config.custom_database_type = 'PostgreSQL'
70
- config.custom_project_notes = 'This is my project.'
71
- config.custom_primary_key = 'id'
72
-
73
- SchemaToDbml.configure(config)
75
+ custom_database_type: 'PostgreSQL'
76
+ custom_project_name: 'my_project_database'
77
+ custom_project_notes: |
78
+ # My Project Database
79
+ This database is designed to support the operations of my project, a leading platform on my core business
80
+ custom_dbml_content: |
81
+ enum object_status {
82
+ created [note: 'Initial status']
83
+ pending
84
+ finished
85
+ cancelled
86
+ }
87
+ TableGroup my_table_group {
88
+ table_1
89
+ table_2
90
+ table_3
91
+ }
74
92
  ```
75
93
 
76
94
  After that, you can use the SchemaToDbml to generate the DBML content as usual.
77
95
 
78
96
  ## Development
79
97
 
98
+
80
99
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
100
 
82
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
101
+ To install this gem onto your local machine, run `bundle install`.
102
+
103
+ For proposing changes, create a new branch and make your changes there. Do not change the version number in `version.rb`. After you've done your changes, open a new Pull Request (PR) for your changes to be reviewed.
104
+
105
+ The maintainers will review your PR. If approved, they will update the version number in `version.rb` and run `bundle exec rake release`. This will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org/).
106
+
107
+ Remember, direct changes to the version number and releases are not allowed. All changes should go through a Pull Request and should be approved by the maintainers.
83
108
 
84
109
  ## Contributing
85
110
 
86
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/schema_to_dbml. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/schema_to_dbml/blob/main/CODE_OF_CONDUCT.md).
111
+ Please read the [contribution guideline](https://github.com/ricardojcribeiro/schema_to_dbml/blob/develop/CONTRIBUTE.md)
87
112
 
88
113
  ## License
89
114
 
@@ -91,4 +116,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
91
116
 
92
117
  ## Code of Conduct
93
118
 
94
- Everyone interacting in the SchemaToDbml project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/schema_to_dbml/blob/main/CODE_OF_CONDUCT.md).
119
+ Everyone interacting in the SchemaToDbml project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ricardojcribeiro/schema_to_dbml/blob/develop/CODE_OF_CONDUCT.md).
data/lib/configuration.rb CHANGED
@@ -1,8 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Configuration
4
- attr_accessor :custom_primary_key,
5
- :custom_database_type,
4
+ def configure_from_hash(yaml_data)
5
+ @custom_database_type = yaml_data['custom_database_type']
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']
9
+ @custom_project_name = yaml_data['custom_project_name']
10
+ @custom_project_notes = yaml_data['custom_project_notes']
11
+ end
12
+
13
+ attr_accessor :custom_database_type,
14
+ :custom_dbml_content,
15
+ :custom_dbml_file_path,
16
+ :custom_primary_key,
6
17
  :custom_project_name,
7
18
  :custom_project_notes
8
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
@@ -10,6 +10,7 @@ class BuildDbmlContent
10
10
  dbml << project_header
11
11
  dbml << tables_section(converted[:tables])
12
12
  dbml << relations_section(converted[:relations])
13
+ dbml << custom_dbml_content
13
14
 
14
15
  dbml.join("\n\n")
15
16
  end
@@ -18,7 +19,7 @@ class BuildDbmlContent
18
19
 
19
20
  def project_header
20
21
  header = "Project #{project_name} {\n"
21
- header += " database_type: #{custom_database_type}\n"
22
+ header += " database_type: '#{custom_database_type}'\n"
22
23
  header += " Note: '#{custom_project_notes}'\n"
23
24
  header += '}'
24
25
  header
@@ -44,5 +45,9 @@ class BuildDbmlContent
44
45
  configuration.custom_project_notes
45
46
  end
46
47
 
48
+ def custom_dbml_content
49
+ configuration.custom_dbml_content
50
+ end
51
+
47
52
  attr_reader :configuration
48
53
  end
@@ -1,5 +1,5 @@
1
1
  custom_project_name: 'dbml_database_definition'
2
- custom_database_type: "'PostgreSQL'"
2
+ custom_database_type: 'PostgreSQL'
3
3
  custom_project_notes: |
4
4
  # My Project Notes
5
5
  This is a **project** that documents the database. Here are some key points:
@@ -8,3 +8,5 @@ custom_project_notes: |
8
8
  - Specifies the appropriate database type (e.g., PostgreSQL)
9
9
  - Provides meaningful project information and descriptions
10
10
  custom_primary_key: "id integer [pk, unique, note: 'Unique identifier and primary key']"
11
+ custom_dbml_content: ''
12
+ custom_dbml_file_path: 'db/schema.dbml'
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support/inflector'
4
+
3
5
  class DbmlRelationsFormatter
4
6
  # Formats a database relationship for a DBML file
5
7
  #
@@ -15,32 +17,30 @@ class DbmlRelationsFormatter
15
17
  ref_name = generate_reference_name(from_table, to_table, column)
16
18
  ref = build_reference_string(ref_name, from_table, column, to_table)
17
19
 
18
- ref += " [delete: #{on_delete}]" if on_delete
20
+ format_on_delete(ref, on_delete)
19
21
  ref
20
22
  end
21
23
 
22
24
  private
23
25
 
24
- def default_foreign_key_column(to_table)
25
- "#{singularize(to_table)}_id"
26
- end
27
-
28
26
  def generate_reference_name(from_table, to_table, column)
29
27
  ref_name = "fk_rails_#{from_table}_#{to_table}"
30
- ref_name += "_#{column}" if column != default_foreign_key_column(to_table)
28
+ ref_name << "_#{column}" if column != default_foreign_key_column(to_table)
31
29
  ref_name
32
30
  end
33
31
 
32
+ def default_foreign_key_column(to_table)
33
+ "#{to_table.singularize}_id"
34
+ end
35
+
34
36
  def build_reference_string(ref_name, from_table, column, to_table)
35
37
  "Ref #{ref_name}:#{from_table}.#{column} - #{to_table}.id"
36
38
  end
37
39
 
38
- # Singularizes a word
39
- #
40
- # @param word [String] the word to singularize
41
- #
42
- # @return [String] the singularized word
43
- def singularize(word)
44
- word.sub(/(?:([^aeiouy])y|s)$/, '\1').sub(/ies$/, 'y')
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
45
  end
46
46
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Errors
4
+ class ConfigurationFileNotFoundError < StandardError
5
+ def initialize(message = 'Configuration file was not found')
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DefaultFieldFormatterHelper
4
+ DEFAULT_ARRAY_REGEX = [/default: (\[.*?\])/, 'default: \'\1\''].freeze
5
+ DEFAULT_BOOLEAN_REGEX = [/default: (true|false)/, 'default: \1'].freeze
6
+ DEFAULT_HASH_REGEX = [/default: (\{(?:[^}]*?)\})/, 'default: \'\1\''].freeze
7
+ DEFAULT_NUMBER_REGEX = [/default: (\d+(?:\.\d+)?)/, 'default: \1'].freeze
8
+ DEFAULT_STRING_REGEX = [/default: "(.*?)"/, 'default: \'\1\''].freeze
9
+ DEFAULT_LAMBDA_REGEX = [/default: -> \{ "(.*?)" \}/, 'default: `\1`'].freeze
10
+
11
+ DEFAULT_PATTERNS = [
12
+ DEFAULT_ARRAY_REGEX,
13
+ DEFAULT_BOOLEAN_REGEX,
14
+ DEFAULT_HASH_REGEX,
15
+ DEFAULT_NUMBER_REGEX,
16
+ DEFAULT_STRING_REGEX,
17
+ DEFAULT_LAMBDA_REGEX
18
+ ].freeze
19
+ end
@@ -1,7 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'default_field_formatter_helper'
4
+
3
5
  module Formatters
4
6
  module FieldsFormatter
7
+ include DefaultFieldFormatterHelper
5
8
  TYPE_MAPPER = {
6
9
  string: 'varchar',
7
10
  integer: 'int',
@@ -22,7 +25,11 @@ module Formatters
22
25
  def format_default(default:)
23
26
  return '' if default.to_s.empty?
24
27
 
25
- "default: #{default}"
28
+ DEFAULT_PATTERNS.each do |pattern, replacement|
29
+ default = default.gsub(pattern, replacement)
30
+ end
31
+
32
+ default
26
33
  end
27
34
 
28
35
  def format_null(null:)
@@ -34,7 +41,7 @@ module Formatters
34
41
  def format_comment(comment:)
35
42
  return '' if comment.to_s.empty?
36
43
 
37
- "note: '#{comment}'"
44
+ "note: '#{comment.gsub("'", "\\\\'")}'"
38
45
  end
39
46
  end
40
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>.*?)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:\s+(?<default>[^,\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.1'
4
+ VERSION = '0.0.3'
5
5
  end
@@ -1,38 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
+ require 'active_support/core_ext/hash'
4
5
  require_relative 'configuration'
5
6
  require_relative 'schema_to_dbml/build_dbml_content'
6
7
  require_relative 'schema_to_dbml/errors/schema_file_not_found_error'
8
+ require_relative 'schema_to_dbml/errors/configuration_file_not_found_error'
7
9
  require_relative 'schema_to_dbml/schema_converter'
8
10
  require_relative 'schema_to_dbml/version'
11
+ require_relative 'rake_tasks/load'
9
12
 
10
13
  class SchemaToDbml
11
14
  DEFAULT_CONFIG_FILE = File.join(__dir__, 'schema_to_dbml/configs/custom_config.yml')
12
15
 
13
16
  class << self
14
17
  def configuration
15
- @configuration ||= begin
16
- config = Configuration.new
17
- load_configuration_from_yaml(DEFAULT_CONFIG_FILE, config)
18
- config
19
- end
18
+ @configuration ||= Configuration.new
20
19
  end
21
20
 
22
- def load_configuration_from_yaml(file_path, config = configuration)
23
- raise 'Configuration file not found Error' unless File.exist?(file_path)
21
+ def load_configuration_from_yaml(file_path: DEFAULT_CONFIG_FILE)
22
+ raise Errors::ConfigurationFileNotFoundError unless File.exist?(file_path)
24
23
 
25
24
  yaml_data = YAML.load_file(file_path)
26
- configure(config) do |c|
27
- c.custom_primary_key = yaml_data['custom_primary_key']
28
- c.custom_database_type = yaml_data['custom_database_type']
29
- c.custom_project_name = yaml_data['custom_project_name']
30
- c.custom_project_notes = yaml_data['custom_project_notes']
31
- end
25
+ yaml_data = merge_with_defaults(file_path, yaml_data)
26
+
27
+ configuration.configure_from_hash(yaml_data)
32
28
  end
33
29
 
34
- def configure(config = configuration)
35
- yield(config)
30
+ def merge_with_defaults(file_path, yaml_data)
31
+ return yaml_data unless file_path != DEFAULT_CONFIG_FILE
32
+
33
+ default_settings = YAML.load_file(DEFAULT_CONFIG_FILE)
34
+
35
+ default_settings.deep_merge!(yaml_data)
36
36
  end
37
37
  end
38
38
 
@@ -44,6 +44,12 @@ class SchemaToDbml
44
44
  @schema_converter = schema_converter
45
45
  end
46
46
 
47
+ def generate(schema:)
48
+ content = convert(schema:)
49
+
50
+ File.write(self.class.configuration.custom_dbml_file_path, content)
51
+ end
52
+
47
53
  def convert(schema:)
48
54
  converted = schema_converter.convert(schema_content: schema_content(schema))
49
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.1
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-06 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
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 6.1.0
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 6.1.0
97
111
  description: This gem provides functionality for parsing a Rails schema.rb file and
98
112
  generating a corresponding DBML file.
99
113
  email:
@@ -102,23 +116,28 @@ executables: []
102
116
  extensions: []
103
117
  extra_rdoc_files: []
104
118
  files:
119
+ - ".rspec"
105
120
  - ".rubocop.yml"
106
121
  - CHANGELOG.md
107
122
  - CODE_OF_CONDUCT.md
123
+ - CONTRIBUTE.md
108
124
  - Gemfile
109
125
  - Gemfile.lock
110
126
  - LICENSE
111
- - LICENSE.txt
112
127
  - README.md
113
128
  - Rakefile
114
129
  - lib/configuration.rb
130
+ - lib/rake_tasks/load.rb
131
+ - lib/rake_tasks/schema_to_dbml.rake
115
132
  - lib/schema_to_dbml.rb
116
133
  - lib/schema_to_dbml/build_dbml_content.rb
117
134
  - lib/schema_to_dbml/build_dbml_file.rb
118
135
  - lib/schema_to_dbml/configs/custom_config.yml
119
136
  - lib/schema_to_dbml/dbml_relations_formatter.rb
120
137
  - lib/schema_to_dbml/dbml_tables_formatter.rb
138
+ - lib/schema_to_dbml/errors/configuration_file_not_found_error.rb
121
139
  - lib/schema_to_dbml/errors/schema_file_not_found_error.rb
140
+ - lib/schema_to_dbml/formatters/default_field_formatter_helper.rb
122
141
  - lib/schema_to_dbml/formatters/fields_formatter.rb
123
142
  - lib/schema_to_dbml/helpers/constants.rb
124
143
  - lib/schema_to_dbml/schema_converter.rb
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2023 Ricardo Ribeiro
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.