attribute-stats 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c859c509e53910fa8ae29b43e7c34843a9da02f
4
- data.tar.gz: 6355de45fcd0dea7370e39665d801b5a8bf79983
3
+ metadata.gz: 2cc768178279a193172676127468cc10c3f9ee32
4
+ data.tar.gz: bdef7d993c67edcf4e8b7ca07fa6f9ac13820eba
5
5
  SHA512:
6
- metadata.gz: e97db1b7cf0a71d90f095e9922f4e1f1d0d801738e331f3f3c052fe5a44d118ca59521138d339b7bc203efcc9808fac1b0af39e50ee26dd4f7c0c78cb62dab7c
7
- data.tar.gz: 0774b4ac6d48cfadeec8b8a807f06a87b462b16296967976a00bdaa607080f7dc9ad0589006bdcdfed59b854afd3fc0576cf43ffd8bd45f43c8ea6b10db422a0
6
+ metadata.gz: 542a32dfb641a8e486c4310249b46403f22c322d39cdbdaeabfdba468ef238e72b009275dd4f7bbe8fe04e7b777abb0612654ca3d2d0da4837b3800615c57c50
7
+ data.tar.gz: 7d75586f3939d08b5939b6edbf7807c7eeccb3a98c275f3d6b0ef89a5cdf0692d710d452b81c00273cbe6ae2b050cc7c98403b77cd02606ee50599c39aa9d32d
data/README.md CHANGED
@@ -80,13 +80,11 @@ i.e. `rake db:stats:dormant_tables['1.year.ago',json,false]`
80
80
  ---
81
81
 
82
82
  #### rake attribute-stats:migration
83
- Generates a sample migration syntax to remove all unused attributes. (This is just output, not saved to disk. See the TODO below.)
83
+ Generates a migration file to remove all unused attributes.
84
84
 
85
85
  **Argument Options:**
86
86
  1. consider_defaults_unused: true or false (default: false). This option considers attributes set to the databse default value to be unused.
87
87
 
88
- *TODO: actually save that generated file to the db/migrate path of the host Rails app.*
89
-
90
88
  ## Caveats
91
89
 
92
90
  The gem does not support:
@@ -1,13 +1,14 @@
1
1
  require 'attribute-stats'
2
2
  namespace :'attribute-stats' do
3
- desc "Generate sample migration to remove unused attributes (and optionally those using default values) [options CONSIDER_DEFAULTS_UNUSED: false, FORMAT: json, tabular, VERBOSE: false]"
4
- task :migration, [:consider_defaults_unused,:format,:verbose] => :environment do |task, args|
5
- args.with_defaults(consider_defaults_unused: 'false', format: 'tabular', verbose: 'true')
3
+ desc "Generate migration file to remove unused attributes (and optionally those using default values) [options CONSIDER_DEFAULTS_UNUSED: false, VERBOSE: false]"
4
+ task :migration, [:consider_defaults_unused,:verbose] => :environment do |task, args|
5
+ args.with_defaults(consider_defaults_unused: 'false', verbose: 'true')
6
6
  options = {
7
7
  consider_defaults_unused: args[:consider_defaults_unused].downcase != 'false',
8
- formatter: args[:format],
9
8
  verbose: args[:verbose].downcase != 'false',
10
9
  source: :cli }
11
- AttributeStats::StatsGenerator.new(options).migration
10
+ migration_file_path = AttributeStats::StatsGenerator.new(options).generate_migration
11
+ return unless verbose
12
+ puts "Generated migration at #{migration_file_path}"
12
13
  end
13
14
  end
@@ -1,3 +1,3 @@
1
1
  module AttributeStats
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -41,9 +41,5 @@ module AttributeStats
41
41
  end
42
42
  output
43
43
  end
44
-
45
- def output_migration
46
- @migration.data
47
- end
48
44
  end
49
45
  end
@@ -12,9 +12,5 @@ module AttributeStats
12
12
  def output_unused_attributes
13
13
  super.to_json
14
14
  end
15
-
16
- def output_migration
17
- super.to_json
18
- end
19
15
  end
20
16
  end
@@ -65,11 +65,6 @@ module AttributeStats
65
65
  @buffer
66
66
  end
67
67
 
68
- def output_migration
69
- print_section_header "Migration Syntax to delete unused attributes"
70
- @migration.to_migration_file
71
- end
72
-
73
68
  private
74
69
 
75
70
  def puts(*values)
@@ -0,0 +1,69 @@
1
+ module AttributeStats
2
+ class GenerateMigration
3
+ def initialize(table_info: nil, options: {})
4
+ @table_info, @options = table_info, options
5
+ @table_info || set_table_info
6
+ end
7
+
8
+ def output_migration
9
+ return nil if migration_template.blank?
10
+ File.open(migration_file_path, 'w') do |f|
11
+ f.write(migration_template)
12
+ end
13
+ migration_file_path
14
+ end
15
+
16
+ def set_table_info
17
+ stats_generator = AttributeStats::StatsGenerator.new(@options)
18
+ stats_generator.send(:fetch_empty_attributes)
19
+ @table_info = stats_generator.table_info
20
+ end
21
+
22
+ private
23
+
24
+ def migration_file_path
25
+ "#{base_path}/#{next_migration_number}_remove_unused_attributes_#{migration_class_suffix}.rb"
26
+ end
27
+
28
+ def migration_template
29
+ MigrationTemplateContents.new(
30
+ table_info: @table_info, migration_class_suffix: migration_class_suffix
31
+ ).content
32
+ end
33
+
34
+ def migration_class_suffix
35
+ @migration_class_suffix ||= find_migration_class_suffix
36
+ end
37
+
38
+ def find_migration_class_suffix
39
+ existing_migration_suffix = Dir.glob("#{base_path}/[0-9]*_remove_unused_attributes_*.rb").map do |fn|
40
+ next unless match = fn.match(/(\d+).rb/)
41
+ match[1].to_i
42
+ end.compact.max
43
+ existing_migration_suffix ||= 0
44
+ existing_migration_suffix + 1
45
+ end
46
+
47
+ # The following methods are extracted from Railties
48
+ # railties/lib/rails/generators/migration.rb
49
+ def next_migration_number
50
+ next_migration_number = current_migration_number + 1
51
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
52
+ end
53
+
54
+ def current_migration_number
55
+ existing_migration_lookup.collect do |file|
56
+ File.basename(file).split("_").first.to_i
57
+ end.max.to_i
58
+ end
59
+
60
+ def existing_migration_lookup
61
+ Dir.glob("#{base_path}/[0-9]*_*.rb")
62
+ end
63
+ # End Railties methods
64
+
65
+ def base_path
66
+ Rails.root.join('db', 'migrate')
67
+ end
68
+ end
69
+ end
@@ -1,46 +1,33 @@
1
1
  module AttributeStats
2
- class GenerateMigration
3
-
4
- def initialize(table_info: [], options: {})
5
- @table_info, @options = table_info, options
6
- @up_buffer = []
7
- @down_buffer = []
2
+ class MigrationTemplateContents
3
+ def initialize(table_info: nil, migration_class_suffix: nil)
4
+ @table_info = table_info
5
+ @table_info || set_table_info
6
+ @migration_class_suffix = migration_class_suffix
7
+ @migration_buffer = ''
8
8
  @table_info.each do |table_info|
9
9
  add_migrations_for_table_to_buffer(table_info)
10
10
  end
11
11
  end
12
12
 
13
- def data
14
- {
15
- up_commands: @up_buffer,
16
- down_commands: @down_buffer,
17
- warning: warning
18
- }
19
- end
20
-
21
- def to_migration_file
22
- output = "class RemoveUnusedAttributes < ActiveRecord::Migration"
13
+ def content
14
+ return nil if @migration_buffer.blank?
15
+ output = "class RemoveUnusedAttributes#{@migration_class_suffix} < ActiveRecord::Migration"
23
16
  output << "[#{Rails::VERSION::STRING}]" if Rails::VERSION::MAJOR >= 5
24
17
  output << "\n#{warning_to_width}"
25
18
  output << <<-OUTPUT
26
- def up
27
- # #{@up_buffer.join("\n # ")}
28
- end
29
-
30
- def down
31
- # #{@down_buffer.join("\n # ")}
19
+ def change
20
+ #{@migration_buffer}
32
21
  end
33
22
  end
34
23
  OUTPUT
35
24
  output
36
25
  end
37
26
 
38
- def to_json
39
- {
40
- up_commands: @up_buffer,
41
- down_commands: @down_buffer,
42
- warning: warning
43
- }
27
+ def set_table_info
28
+ stats_generator = AttributeStats::StatsGenerator.new
29
+ stats_generator.send(:fetch_empty_attributes)
30
+ @table_info = stats_generator.table_info
44
31
  end
45
32
 
46
33
  private
@@ -51,7 +38,7 @@ end
51
38
  # ActiveRecord::SchemaDumper cannot be reused for this case, so I had to extract it.
52
39
  def add_migrations_for_table_to_buffer(table_info)
53
40
  @connection ||= ActiveRecord::Base.connection
54
- @types ||= @connection.native_database_types
41
+ @types ||= @connection.native_database_types
55
42
 
56
43
  column_specs = []
57
44
  @connection.columns(table_info.table_name).each do |column|
@@ -59,11 +46,10 @@ end
59
46
  column_specs << column_spec_for(column)
60
47
  end
61
48
 
62
- quoted_table_name = @connection.quote_table_name(table_info.table_name)
49
+ quoted_table_name = "\"#{table_info.table_name}\""
63
50
  column_specs.each do |colspec|
64
- down_syntax = [quoted_table_name, colspec[:name], ":#{colspec[:type]}", colspec[:options].presence].compact.join(', ')
65
- @down_buffer << "add_column #{down_syntax}"
66
- @up_buffer << "remove_column #{quoted_table_name}, #{colspec[:name]}"
51
+ down_syntax = [quoted_table_name, colspec[:name], ":#{colspec[:type]}", colspec[:options].presence].compact.join(', ')
52
+ @migration_buffer << " # remove_column #{down_syntax}\n"
67
53
  end
68
54
  end
69
55
 
@@ -141,11 +127,11 @@ end
141
127
 
142
128
  def warning
143
129
  <<-WARNING
144
- BUYER BEWARE/CAVEAT EMPTOR. Review this migration carefully!
145
- This migration code was autogenerated by analyzing database columns which are empty at the time the script was executed.
130
+ REVIEW THIS MIGRATION CAREFULLY!
131
+ This migration code was generated by analyzing database columns which are empty at the time the script was executed.
146
132
  IT IS YOUR RESPONSIBILITY to verify that these attributes are indeed unused in your application before running this migration. For your protection, the generated commands are COMMENTED OUT.
147
133
  If you were to uncomment the below commands and migrate your database, it is extremely likely that your application will break due to references to the removed attributes in your application code.
148
134
  WARNING
149
135
  end
150
- end
151
- end
136
+ end
137
+ end
@@ -29,9 +29,9 @@ module AttributeStats
29
29
  output formatter.output_unused_attributes
30
30
  end
31
31
 
32
- def migration
33
- generate_migration
34
- output formatter.output_migration
32
+ def generate_migration
33
+ fetch_empty_attributes
34
+ GenerateMigration.new(table_info: table_info, options: options).output_migration
35
35
  end
36
36
 
37
37
  def set_formatter(formatter_type)
@@ -104,11 +104,6 @@ module AttributeStats
104
104
  end
105
105
  end
106
106
 
107
- def generate_migration
108
- fetch_empty_attributes
109
- @migration ||= GenerateMigration.new(table_info: table_info, options: options)
110
- end
111
-
112
107
  def initialize_tables
113
108
  return unless table_info.nil?
114
109
  @table_info = []
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute-stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Hodges
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-28 00:00:00.000000000 Z
11
+ date: 2018-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -143,7 +143,8 @@ files:
143
143
  - lib/formatters/hash_formatter.rb
144
144
  - lib/formatters/json_formatter.rb
145
145
  - lib/formatters/tabular_formatter.rb
146
- - lib/stats_generation/generate_migration.rb
146
+ - lib/migration_generator/generate_migration.rb
147
+ - lib/migration_generator/migration_template_contents.rb
147
148
  - lib/stats_generation/set_attribute_stats.rb
148
149
  - lib/stats_generation/set_dormant_tables.rb
149
150
  - lib/stats_generation/stats_generator.rb