ignored_columns_tasks 0.0.1

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: ad7e03cc537a362226785d3937b5e7642eb97720cfa6353272e2b691738bc9f4
4
+ data.tar.gz: 2c3e07d772548bc75f0051c23ca0290ec3ead2c37a55999ad1a67f610d92fa3c
5
+ SHA512:
6
+ metadata.gz: 06756504d577020eb1611ab1f361418ca5d90cca980a592b6c7a8f4fa9fdcecd803c91164925b9699e7c3254c5146829b39540d2d515e8bead5738d0f99ebd06
7
+ data.tar.gz: 39099157082e6e3f5aafc4e7768c4d126197b4e6bab7d5f4be87281dccd285c22e5f92525652d84a33d900ee84dee45b3cba16c5b53f116cfb124b47d37b4704
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ # rspec failure tracking
11
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ignored_columns_tasks.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Skye Shaw
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,67 @@
1
+ # Ignored Columns Tasks
2
+
3
+ Rails tasks for managing
4
+ [Active Record ignored columns](https://api.rubyonrails.org/v7.0.6/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-ignored_columns-3D).
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's `Gemfile` in the `:development` group:
9
+
10
+ ```ruby
11
+ group :development do
12
+ gem "ignored_columns_tasks"
13
+ end
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ All functionality is provided as Rake tasks.
19
+
20
+ ### Generating a Migration to Drop Ignored Columns
21
+
22
+ This will generate (but not run!) migrations to drop columns currently being ignored. One migration is generated per model:
23
+
24
+ ```
25
+ ./bin/rails ignored_columns:migration
26
+ ```
27
+
28
+ If you have ignored columns that must not be dropped add them to the `SKIP_COLUMNS` environment variable:
29
+
30
+ ```
31
+ ./bin/rails ignored_columns:migration SKIP_COLUMNS="some_column,another_column"
32
+ ```
33
+
34
+ You can set this once for your project instead of specifying it every time.
35
+ In this case it is recommended to use the `IGNORED_COLUMNS_SKIP_COLUMNS` environment variable.
36
+
37
+ ```sh
38
+ export IGNORED_COLUMNS_SKIP_COLUMNS="some_column,another_column"
39
+ ```
40
+
41
+ This task can also be limited to a single model via the `MODEL` environment variable:
42
+
43
+ ```
44
+ ./bin/rails ignored_columns:migration MODEL=User
45
+ ```
46
+
47
+ ### Ignored Columns That Have Been Dropped From Your Database
48
+
49
+ This will print ignored columns that no longer exist the database:
50
+
51
+ ```
52
+ ./bin/rails ignored_columns:dropped
53
+ ```
54
+
55
+ This can be limited to a single model via the `MODEL` environment variable:
56
+
57
+ ```
58
+ ./bin/rails ignored_columns:dropped MODEL=User
59
+ ```
60
+
61
+ ## Author
62
+
63
+ Skye Shaw (skye.shaw +AT+ gmail)
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ignored_columns_tasks"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
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,30 @@
1
+ require_relative 'lib/ignored_columns_tasks/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ignored_columns_tasks"
5
+ spec.version = IgnoredColumnsTasks::VERSION
6
+ spec.authors = ["Skye Shaw"]
7
+ spec.email = ["skye.shaw@gmail.com"]
8
+
9
+ spec.summary = %q{Rails tasks for managing Active Record ignored columns}
10
+ spec.description = %q{Rails tasks to create migrations to remove ignored columns and output dropped columns that are still being ignored}
11
+ spec.homepage = "https://github.com/sshaw/ignored_columns_tasks"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/sshaw/ignored_columns_tasks"
17
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "rake"
30
+ end
@@ -0,0 +1,7 @@
1
+ module IgnoredColumnTasks
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "ignored_columns_tasks/tasks/ignored_columns.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ namespace :ignored_columns do
2
+ def options
3
+ optz = {}
4
+ optz[:model] = ENV["MODEL"].constantize if ENV["MODEL"]
5
+ optz[:skip_columns] = skip_columns
6
+ optz
7
+ end
8
+
9
+ def skip_columns
10
+ skip = nil
11
+
12
+ # IGNORED_COLUMNS_SKIP_COLUMNS is our global, namespaced verison
13
+ # SKIP_COLUMNS is shorter, command-line argument
14
+ %w[IGNORED_COLUMNS_SKIP_COLUMNS SKIP_COLUMNS].each do |name|
15
+ if ENV[name]
16
+ skip ||= []
17
+ skip.concat(ENV[name].split(/\s*,\s/))
18
+ end
19
+ end
20
+
21
+ skip
22
+ end
23
+
24
+ desc "Output ignored columns for MODEL that have been dropped, ignoring SKIP_COLUMNS; if MODEL not given output for all"
25
+ task :dropped => :environment do
26
+ IgnoredColumnsTasks.dropped(options)
27
+ end
28
+
29
+ desc "Generate a migration to drop ignored columns for MODEL, ignoring SKIP_COLUMNS; if MODEL not given create migrations for all"
30
+ task :migration => :environment do
31
+ IgnoredColumnsTasks.migration(options)
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module IgnoredColumnsTasks
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,120 @@
1
+ require "ignored_columns_tasks/version"
2
+ require "rails/generators"
3
+
4
+ module IgnoredColumnsTasks
5
+ class Column
6
+ attr_reader :klass, :name, :data
7
+
8
+ def initialize(klass, name, data = nil)
9
+ @klass = klass
10
+ @name = name
11
+ # Aren't using this now outside of exists
12
+ @data = data
13
+ end
14
+
15
+ def exists?
16
+ !@data.nil?
17
+ end
18
+
19
+ def to_s
20
+ name
21
+ end
22
+ end
23
+
24
+ class << self
25
+ ##
26
+ #
27
+ # Output ignored columns that have been dropped for +model+.
28
+ # If +model+ is not given output for all classes
29
+ #
30
+ # +model+ must a an ActiveRecord::Base subclass
31
+ #
32
+ def dropped(options = nil)
33
+ Rails.application.eager_load!
34
+
35
+ ignored = find_columns(options || {})
36
+ if ignored.none?
37
+ puts "No ignored columns exist"
38
+ return
39
+ end
40
+
41
+ dropped = ignored.reject(&:exists?)
42
+ if dropped.none?
43
+ puts "No ignored columns have been dropped"
44
+ return
45
+ end
46
+
47
+ dropped.group_by(&:klass).sort_by { |klass, _| klass.name }.each do |klass, columns|
48
+ puts "%s: %s" % [klass.name, columns.sort_by(&:name).to_sentence]
49
+ end
50
+ end
51
+
52
+ ##
53
+ #
54
+ # Generate a migration to drop ignored columns for +model+.
55
+ # If +model+ is not given create migrations for all classes
56
+ # with ignored columns. 1 migration per class.
57
+ #
58
+ # +model+ must a an ActiveRecord::Base subclass
59
+ #
60
+ def migration(options = nil)
61
+ Rails.application.eager_load!
62
+
63
+ ignored_columns = find_columns(options || {})
64
+ if ignored_columns.none?
65
+ puts "No ignored columns exist"
66
+ return
67
+ end
68
+
69
+ to_drop = ignored_columns.select(&:exists?)
70
+ if to_drop.none?
71
+ puts "No ignored columns to drop"
72
+ return
73
+ end
74
+
75
+ to_drop.group_by(&:klass).each do |klass, columns|
76
+ migration = "remove_ignored_columns_from_#{klass.table_name}"
77
+
78
+ # Assume Rails does the shell quoting on Column#name?
79
+ Rails::Generators.invoke("active_record:migration", [migration, columns.map(&:name)])
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def all_classes
86
+ (ApplicationRecord.subclasses + ActiveRecord::Base.subclasses).reject do |klass|
87
+ klass.abstract_class? || klass.ignored_columns.none?
88
+ end
89
+ end
90
+
91
+ def class_with_all_columns(klass)
92
+ ignored = klass.ignored_columns
93
+
94
+ klass.ignored_columns = []
95
+ klass.reset_column_information
96
+ yield klass, ignored.dup
97
+ ensure
98
+ # I mean do we really need to do this?
99
+ klass.ignored_columns = ignored
100
+ end
101
+
102
+ def find_columns(options)
103
+ if options[:model]
104
+ ignored_columns(options[:model], options[:skip_columns])
105
+ else
106
+ all_classes.flat_map { |klass| ignored_columns(klass, options[:skip_columns]) }
107
+ end
108
+ end
109
+
110
+ def ignored_columns(klass, skip = nil)
111
+ skip ||= []
112
+ class_with_all_columns klass do |class_with_all, ignored|
113
+ ignored -= skip if skip.any?
114
+ ignored.map { |column| Column.new(class_with_all, column, class_with_all.columns_hash[column]) }
115
+ end
116
+ end
117
+ end
118
+ end
119
+
120
+ require "ignored_columns_tasks/railtie" if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ignored_columns_tasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Skye Shaw
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Rails tasks to create migrations to remove ignored columns and output
42
+ dropped columns that are still being ignored
43
+ email:
44
+ - skye.shaw@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - ignored_columns_tasks.gemspec
57
+ - lib/ignored_columns_tasks.rb
58
+ - lib/ignored_columns_tasks/railtie.rb
59
+ - lib/ignored_columns_tasks/tasks/ignored_columns.rake
60
+ - lib/ignored_columns_tasks/version.rb
61
+ homepage: https://github.com/sshaw/ignored_columns_tasks
62
+ licenses:
63
+ - MIT
64
+ metadata:
65
+ homepage_uri: https://github.com/sshaw/ignored_columns_tasks
66
+ source_code_uri: https://github.com/sshaw/ignored_columns_tasks
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.3.0
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubygems_version: 3.0.3
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: Rails tasks for managing Active Record ignored columns
86
+ test_files: []