rails-clean-schema 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 32e48270779ae3a725ba4e6cea2bb91a5af941fdb7bf903a79e09f62cd8121f8
4
+ data.tar.gz: 29a9de45c946508eb944a1c4818aa10ff31f50b28fb4f46c57402b86db5190f5
5
+ SHA512:
6
+ metadata.gz: 7f2132fe0dda94e9aee6ac6347db7953776d0899f939aa1d5026021c54ce79dd7ff7ae75b67ef40a2077b16bd01bcdc169f2693f6fbbf4d97af97990a36aef54
7
+ data.tar.gz: 829a379dafe0200eb273644d66accbc502e5565a2e1e1e71626b7139674a2bbe442ee55727b9488e3b268abbce57afc5b0dd34e0b2932ff08893bf1ab68eba97
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Rails::Clean::Schema
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails/clean/schema`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails-clean-schema. 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]/rails-clean-schema/blob/master/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Rails::Clean::Schema project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rails-clean-schema/blob/master/CODE_OF_CONDUCT.md).
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rails/clean/schema"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "../lib/rails-clean-schema"
3
+ require File.expand_path("config/environment", Dir.pwd)
4
+
5
+ results = RailsCleanSchema.run
6
+
7
+ puts "\n== 🗃️ Unused Tables =="
8
+ puts results[:unused_tables].any? ? results[:unused_tables].join("\n") : "None 🎉"
9
+
10
+ puts "\n== 🧱 Unused Columns =="
11
+ if results[:unused_columns].any?
12
+ results[:unused_columns].each do |table, columns|
13
+ puts "#{table}: #{columns.join(', ')}"
14
+ end
15
+ else
16
+ puts "None 🎉"
17
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,46 @@
1
+ # lib/rails/clean/schema/analyzer.rb
2
+ module Rails
3
+ module Clean
4
+ module Schema
5
+ class Analyzer
6
+ def initialize
7
+ @schema_info = ActiveRecord::Base.connection.tables.index_with do |table|
8
+ {
9
+ columns: ActiveRecord::Base.connection.columns(table).map(&:name),
10
+ indexes: ActiveRecord::Base.connection.indexes(table).map(&:columns).flatten
11
+ }
12
+ end
13
+
14
+ @model_info = ActiveRecord::Base.descendants.select do |model|
15
+ model.table_exists? && !model.abstract_class?
16
+ end.index_with do |model|
17
+ {
18
+ table: model.table_name,
19
+ columns: model.column_names
20
+ }
21
+ end
22
+ end
23
+
24
+ def run
25
+ unused_tables = @schema_info.keys - @model_info.values.map { |info| info[:table] }
26
+
27
+ unused_columns = {}
28
+ @model_info.each do |model, info|
29
+ table = info[:table]
30
+ next unless @schema_info[table]
31
+
32
+ schema_columns = @schema_info[table][:columns]
33
+ model_columns = info[:columns]
34
+ unused = schema_columns - model_columns
35
+ unused_columns[table] = unused if unused.any?
36
+ end
37
+
38
+ {
39
+ unused_tables: unused_tables,
40
+ unused_columns: unused_columns
41
+ }
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Clean
5
+ module Schema
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "schema/version"
4
+
5
+ module Rails
6
+ module Clean
7
+ module Schema
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ # lib/rails-clean-schema.rb
2
+ require "rails/clean/schema/analyzer"
3
+
4
+ module RailsCleanSchema
5
+ def self.run
6
+ Rails.application.eager_load!
7
+ analyzer = ::Rails::Clean::Schema::Analyzer.new
8
+ analyzer.run
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-clean-schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Awais Manzoor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ description: Rails gem to scan db/schema.rb and compare with models to detect unused
28
+ database elements
29
+ email:
30
+ - awaismanzoor232@gmail.com
31
+ executables:
32
+ - rails-clean-schema
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - README.md
37
+ - bin/console
38
+ - bin/rails-clean-schema
39
+ - bin/setup
40
+ - lib/rails-clean-schema.rb
41
+ - lib/rails/clean/schema.rb
42
+ - lib/rails/clean/schema/analyzer.rb
43
+ - lib/rails/clean/schema/version.rb
44
+ homepage:
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '2.7'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.2.3
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Find unused tables and columns in your Rails app
67
+ test_files: []