rails_schema_cleaner 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 +7 -0
- data/.rspec +3 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +10 -0
- data/bin/rails_schema_cleaner +34 -0
- data/lib/rails_schema_cleaner/version.rb +5 -0
- data/lib/rails_schema_cleaner.rb +50 -0
- data/sig/rails_schema_cleaner.rbs +4 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dcc1e8a0e8351e3fab096c5ed0e38758cd998cb7472f31e3b7c03b7adcb9d3af
|
4
|
+
data.tar.gz: cd21d7ec9586ce7345ce3fad4885755e544a43fdd0ebeea0b9bf36cb2e936724
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0f327bc26e101aa549cac433c8cca3971920bcaaf8bd9f4f7ae459b64791ae8122b09562a90a0ef0ea30f163fe03fa332c62803fc636ac3986fcd07b0f07a489
|
7
|
+
data.tar.gz: aeca7ea631cae4a193479803099ee38adab348c7f7235fda3946bf82722ec1f36d088953d0fb0e2d47043cf6a7f2370a45a9152a36395816008a7ca6d0635dea
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Hassan Murtaza
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# RailsSchemaCleaner
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
`rails_schema_cleaner` is a Ruby gem that helps keep your Rails database schema clean by identifying and removing orphaned tables (tables that do not have corresponding ActiveRecord models). It generates a migration file to drop these unused tables safely.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
Add this gem to your `Gemfile`:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "rails_schema_cleaner"
|
11
|
+
```
|
12
|
+
|
13
|
+
Then run:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Alternatively, install it manually:
|
20
|
+
|
21
|
+
```sh
|
22
|
+
gem install rails_schema_cleaner
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
### **1. Detect Orphaned Tables**
|
27
|
+
To list all tables in the database that do not have an associated model, run:
|
28
|
+
|
29
|
+
```sh
|
30
|
+
bundle exec rails_schema_cleaner detect
|
31
|
+
```
|
32
|
+
|
33
|
+
This will return an array of orphaned table names.
|
34
|
+
|
35
|
+
### **2. Generate Migration to Drop Orphaned Tables**
|
36
|
+
To create a Rails migration file that drops these tables, run:
|
37
|
+
|
38
|
+
```sh
|
39
|
+
bundle exec rails_schema_cleaner clean
|
40
|
+
```
|
41
|
+
|
42
|
+
This will generate a timestamped migration file in `db/migrate/`, e.g.:
|
43
|
+
|
44
|
+
```sh
|
45
|
+
db/migrate/20240228123456_drop_orphaned_tables.rb
|
46
|
+
```
|
47
|
+
|
48
|
+
### **3. Run the Migration**
|
49
|
+
Execute the migration to drop the orphaned tables:
|
50
|
+
|
51
|
+
```sh
|
52
|
+
rails db:migrate
|
53
|
+
```
|
54
|
+
|
55
|
+
## Compatibility
|
56
|
+
- Works with Rails >=5.0
|
57
|
+
- Supports SQLite, PostgreSQL, and MySQL
|
58
|
+
|
59
|
+
## How It Works
|
60
|
+
1. Fetches all database tables using `ActiveRecord::Base.connection.tables`
|
61
|
+
2. Fetches all existing model table names using `ActiveRecord::Base.descendants.map(&:table_name)`
|
62
|
+
3. Compares both lists and identifies tables that do not have an associated model
|
63
|
+
4. Generates a Rails migration file to drop those tables
|
64
|
+
|
65
|
+
## Running Tests
|
66
|
+
Run RSpec tests with:
|
67
|
+
|
68
|
+
```sh
|
69
|
+
rspec
|
70
|
+
```
|
71
|
+
|
72
|
+
## License
|
73
|
+
This project is licensed under the MIT License.
|
74
|
+
|
75
|
+
## Contributions
|
76
|
+
Pull requests are welcome! Please open an issue to discuss any significant changes before submitting a PR.
|
77
|
+
|
78
|
+
## Author
|
79
|
+
Created by [Hassan Murtaza](https://github.com/hmurtaza7).
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
|
5
|
+
begin
|
6
|
+
require "rails"
|
7
|
+
require "active_record"
|
8
|
+
require "rails_schema_cleaner"
|
9
|
+
|
10
|
+
# Ensure we are inside a Rails application
|
11
|
+
unless Dir.exist?("config") && File.exist?("config/application.rb")
|
12
|
+
puts "Error: This command must be run inside a Rails application."
|
13
|
+
exit 1
|
14
|
+
end
|
15
|
+
|
16
|
+
# Command-line arguments
|
17
|
+
command = ARGV[0]
|
18
|
+
|
19
|
+
case command
|
20
|
+
when "detect"
|
21
|
+
puts "Orphaned tables: #{RailsSchemaCleaner.orphaned_tables.join(', ')}"
|
22
|
+
when "clean"
|
23
|
+
RailsSchemaCleaner.generate_migration
|
24
|
+
puts "Migration file created to drop orphaned tables."
|
25
|
+
else
|
26
|
+
puts "Usage:"
|
27
|
+
puts " rails_schema_cleaner detect # Lists orphaned tables"
|
28
|
+
puts " rails_schema_cleaner clean # Generates migration to drop orphaned tables"
|
29
|
+
end
|
30
|
+
|
31
|
+
rescue LoadError
|
32
|
+
puts "Error: Rails is not installed. Ensure you are running this inside a Rails application."
|
33
|
+
exit 1
|
34
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "rails_schema_cleaner/version"
|
4
|
+
|
5
|
+
module RailsSchemaCleaner
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
DEFAULT_TABLES = ["ar_internal_metadata", "schema_migrations"].to_set
|
9
|
+
|
10
|
+
def self.orphaned_tables
|
11
|
+
establish_db_connection! unless ActiveRecord::Base.connected?
|
12
|
+
|
13
|
+
db_tables = ActiveRecord::Base.connection.tables.to_set
|
14
|
+
model_tables = ActiveRecord::Base.descendants.map(&:table_name).compact.to_set
|
15
|
+
db_tables - model_tables - DEFAULT_TABLES
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.generate_migration
|
19
|
+
tables_to_drop = orphaned_tables
|
20
|
+
return puts "No orphaned tables found." if tables_to_drop.empty?
|
21
|
+
|
22
|
+
timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
|
23
|
+
migration_filename = "db/migrate/#{timestamp}_drop_orphaned_tables.rb"
|
24
|
+
|
25
|
+
# Define the migration class with the correct format
|
26
|
+
migration_class = "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.0]"
|
27
|
+
|
28
|
+
migration_content = <<-RUBY
|
29
|
+
class DropOrphanedTables < #{migration_class}
|
30
|
+
def change
|
31
|
+
#{tables_to_drop.map { |t| "drop_table :#{t}" }.join("\n ")}
|
32
|
+
end
|
33
|
+
end
|
34
|
+
RUBY
|
35
|
+
|
36
|
+
File.write(migration_filename, migration_content)
|
37
|
+
puts "Migration created: #{migration_filename}"
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.establish_db_connection!
|
41
|
+
require File.expand_path(Dir.pwd + "/config/environment", __FILE__)
|
42
|
+
|
43
|
+
db_config = Rails.configuration.database_configuration[Rails.env]
|
44
|
+
ActiveRecord::Base.establish_connection(db_config)
|
45
|
+
|
46
|
+
Rails.application.eager_load!
|
47
|
+
|
48
|
+
puts "Database connection established!"
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_schema_cleaner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hassan Murtaza
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-01 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: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
description: Finds and drops tables that have no corresponding ActiveRecord model
|
28
|
+
email:
|
29
|
+
- "...@gmail.com"
|
30
|
+
executables:
|
31
|
+
- rails_schema_cleaner
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".rspec"
|
36
|
+
- ".standard.yml"
|
37
|
+
- CHANGELOG.md
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- bin/rails_schema_cleaner
|
42
|
+
- lib/rails_schema_cleaner.rb
|
43
|
+
- lib/rails_schema_cleaner/version.rb
|
44
|
+
- sig/rails_schema_cleaner.rbs
|
45
|
+
homepage: https://github.com/hmurtaza7/rails_schema_cleaner
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://github.com/hmurtaza7/rails_schema_cleaner
|
50
|
+
source_code_uri: https://github.com/hmurtaza7/rails_schema_cleaner
|
51
|
+
changelog_uri: https://github.com/hmurtaza7/rails_schema_cleaner/blob/main/CHANGELOG.md
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.0.0
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.5.22
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Cleans up orphaned tables in your Rails database
|
71
|
+
test_files: []
|