database_schema_ownership 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b5e756e987e34d6afc6217586393241faf93bf362adf8dac06ddcda94ac20b85
4
+ data.tar.gz: 57a76af1486dcb2e317451be4f7ba9be6d940ab0ec1a23d471181041ce0698ad
5
+ SHA512:
6
+ metadata.gz: 1939d8886af6269f2ee40d1ceb10c5c0e10652fe44a308a62eb8b55be227727fdb45701251b77918eb554a13d8c23319bc557e61d37db152341c821ba4131b7c
7
+ data.tar.gz: 39a9a193151dd8f4213af72870aa133058ee75dcb960850a8adf7826d9f71332da4362a73693d6cccb8e453887dcfbc84eb593631f75ad16fc68dd1c176872a6
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :database_schema_ownership do
4
+ desc "Generate ownership files for database schema"
5
+ task dump: "db:schema:dump" do
6
+ DatabaseSchemaOwnership::Runner.new.run
7
+ end
8
+ end
9
+
10
+ Rake::Task["db:schema:dump"].enhance do
11
+ Rake::Task["database_schema_ownership:dump"].invoke
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseSchemaOwnership
4
+ class Entity # :nodoc:
5
+ attr_reader :name, :metadata
6
+
7
+ def initialize(name, metadata)
8
+ @name = name
9
+ @metadata = metadata
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseSchemaOwnership
4
+ class Parser
5
+ class RubyParser < Parser # :nodoc:
6
+ def parse
7
+ parse_tables + parse_foreign_keys
8
+ end
9
+
10
+ def parse_tables
11
+ schema.scan(/\n*(\s+create_table "([^"]*)".*?end)/m).map do |s|
12
+ DatabaseSchemaOwnership::Entity.new(s[1], s[0])
13
+ end
14
+ end
15
+
16
+ def parse_foreign_keys
17
+ schema.scan(/\n*(\s+add_foreign_key "([^"]*)", "([^"]*)")/).map do |s|
18
+ DatabaseSchemaOwnership::Entity.new(s[1], s[0])
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseSchemaOwnership
4
+ class Parser
5
+ class SqlParser < Parser # :nodoc:
6
+ def parse
7
+ parse_tables + parse_indexes
8
+ end
9
+
10
+ def parse_tables
11
+ schema.scan(/(CREATE TABLE IF NOT EXISTS "([^"]*)"[^;]*;)/m).map do |s|
12
+ DatabaseSchemaOwnership::Entity.new(s[1], s[0])
13
+ end
14
+ end
15
+
16
+ def parse_indexes
17
+ schema.scan(/(CREATE INDEX .* ON "([^"]*)"[^;]*;)/).map do |s|
18
+ DatabaseSchemaOwnership::Entity.new(s[1], s[0])
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseSchemaOwnership
4
+ class Parser # :nodoc:
5
+ def self.for(extension)
6
+ case extension
7
+ when ".rb" then RubyParser
8
+ when ".sql" then SqlParser
9
+ else
10
+ raise ArgumentError, "Unsupported schema extension: #{extension}"
11
+ end
12
+ end
13
+
14
+ attr_accessor :schema_path
15
+
16
+ def initialize(schema_path)
17
+ @schema_path = schema_path
18
+ end
19
+
20
+ def schema
21
+ File.read(schema_path)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../database_schema_ownership"
4
+
5
+ module DatabaseSchemaOwnership
6
+ class Railtie < Rails::Railtie # :nodoc:
7
+ rake_tasks do
8
+ load "#{__dir__}/database_schema_ownership.rake"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseSchemaOwnership
4
+ # :nodoc
5
+ class Runner
6
+ RUBY_SCHEMA_PATH = "db/schema.rb"
7
+ SQL_SCHEMA_PATH = "db/structure.sql"
8
+ OWNERSHIP_PATH = "db/database_schema_ownership"
9
+
10
+ attr_reader :schema_path, :folder_path, :schema_extension
11
+
12
+ def initialize(schema_path = nil, folder_path = OWNERSHIP_PATH)
13
+ @schema_path = schema_path || find_schema_path
14
+ @folder_path = folder_path
15
+ @schema_extension = File.extname(@schema_path)
16
+ end
17
+
18
+ def run
19
+ entities = parser.parse
20
+
21
+ FileUtils.mkdir_p(folder_path) if entities.any?
22
+
23
+ grouped = group_entities(entities)
24
+
25
+ grouped.each do |name, metadata|
26
+ File.write(file_name(name), metadata)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def group_entities(entities)
33
+ entities.each_with_object({}) do |entity, hash|
34
+ if hash[entity.name]
35
+ hash[entity.name] += "\n#{entity.metadata}"
36
+ else
37
+ hash[entity.name] = entity.metadata
38
+ end
39
+ end
40
+ end
41
+
42
+ def parser
43
+ DatabaseSchemaOwnership::Parser.for(schema_extension).new(schema_path)
44
+ end
45
+
46
+ def file_name(name)
47
+ "#{folder_path}/#{name}#{schema_extension}"
48
+ end
49
+
50
+ def find_schema_path
51
+ if File.exist?(RUBY_SCHEMA_PATH)
52
+ RUBY_SCHEMA_PATH
53
+ elsif File.exist?(SQL_SCHEMA_PATH)
54
+ SQL_SCHEMA_PATH
55
+ else
56
+ raise "Schema file not found"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DatabaseSchemaOwnership
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "database_schema_ownership/version"
4
+ require_relative "database_schema_ownership/entity"
5
+ require_relative "database_schema_ownership/parser/ruby_parser"
6
+ require_relative "database_schema_ownership/parser/sql_parser"
7
+ require_relative "database_schema_ownership/parser"
8
+ require_relative "database_schema_ownership/runner"
9
+
10
+ module DatabaseSchemaOwnership # :nodoc:
11
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: database_schema_ownership
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeniy Demin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-11-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - lawliet.djez@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/database_schema_ownership.rb
35
+ - lib/database_schema_ownership/database_schema_ownership.rake
36
+ - lib/database_schema_ownership/entity.rb
37
+ - lib/database_schema_ownership/parser.rb
38
+ - lib/database_schema_ownership/parser/ruby_parser.rb
39
+ - lib/database_schema_ownership/parser/sql_parser.rb
40
+ - lib/database_schema_ownership/railtie.rb
41
+ - lib/database_schema_ownership/runner.rb
42
+ - lib/database_schema_ownership/version.rb
43
+ homepage: https://github.com/djezzzl/database_schema_ownership
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/djezzzl/database_schema_ownership
48
+ source_code_uri: https://github.com/djezzzl/database_schema_ownership
49
+ changelog_uri: https://github.com/djezzzl/database_schema_ownership/blob/master/CHANGELOG.md
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 2.5.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.5.23
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Provide an easy way to manage code ownership over a database schema.
69
+ test_files: []