activerecord-migration_notes 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
+ SHA1:
3
+ metadata.gz: 918a80da6c0de93012f575c35e570ec22f9af9c2
4
+ data.tar.gz: 1c4991e6e9de395d55de45bdc1af780042ca727e
5
+ SHA512:
6
+ metadata.gz: c70923dc7ccaa93416749c4eeaef5532c62a66330752eaf5f22dc5a05d32bd66a96bc057bad003aa19995c1203898167d2ab47ed7e51dedd25c87ef8ec131dcd
7
+ data.tar.gz: 9c03578c7c6489d046c05e922999bd7daf8ff603beaf5237888365f07bb62fef9517f7f9559172db62d462659ae2fc76cd7564e0010d8a1e3cb9a671df6444d9
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-migration_notes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Manuel van Rijn
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,71 @@
1
+ # ActiveRecord::MigrationNotes
2
+
3
+ ActiveRecord Migration Notes hooks into the Rails migrations and monitors if the migrations contain a note methods that should be shown afterwards.
4
+
5
+ ## Why?
6
+
7
+ Have you ever been in a situation where you have to deploy some code and forgot what special things you had to execute manually after a migration?
8
+
9
+ Searching for this answer within pull requests, issues, code comments, tickets, emails, faxes made me think this should be easier.
10
+
11
+ Why not give the developer who creates the migration, an easy way to add some additional notes to the migration?
12
+
13
+ ![image](https://i.imgur.com/P2H99Fy.png)
14
+
15
+ ## Installation
16
+
17
+ Add this line to your Rails application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'activerecord-migration_notes'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ ## Usage
28
+
29
+ You can now add two new methods within your migration called `up_notes` (trigged on `rake db:migrate`) and `down_notes` (triggered on `rake db:rollback`).
30
+
31
+ Both these methods are optional and you do not have to specify them both.
32
+
33
+ ### Example
34
+
35
+ ```ruby
36
+ class AddSomeBreakingChanges < ActiveRecord::Migration
37
+ def change
38
+ # your normal migration task
39
+ end
40
+
41
+ def up_notes
42
+ <<-NOTES
43
+ Please run the following manually to not break the DB!
44
+
45
+ $ rake some_task
46
+ NOTES
47
+ end
48
+
49
+ def down_notes
50
+ "All is well no additional notes"
51
+ end
52
+ end
53
+ ```
54
+
55
+ ## TODO
56
+
57
+ * test if we covered all other cases like `db:down` and `db:up`
58
+ * ... more?
59
+ * probably add some docs
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/migration_notes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'activerecord-migration_notes'
8
+ spec.version = ActiveRecord::MigrationNotes::VERSION
9
+ spec.authors = ['Manuel van Rijn']
10
+ spec.email = ['manuel@manuelvanrijn.nl']
11
+
12
+ spec.summary = 'Add notes to your migrations, to explain additional steps to take for example'
13
+ spec.description = 'ActiveRecord Migration Notes hooks into the Rails migrations and monitors if the migrations contain a note methods that should be shown afterwards.'
14
+ spec.homepage = 'http://manuelvanrijn.nl'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+
25
+ spec.add_dependency 'activerecord', '>= 4.2'
26
+ end
@@ -0,0 +1,76 @@
1
+ require 'singleton'
2
+
3
+ module ActiveRecord
4
+ module MigrationNotes
5
+ class Handler
6
+ include Singleton
7
+
8
+ attr_accessor :notes
9
+
10
+ def initialize
11
+ @notes = []
12
+ end
13
+
14
+ def add(version, name, content, direction = :up)
15
+ content = ' ' + content.split("\n").map(&:strip).join("\n ")
16
+ notes << { version: version, name: name, content: content,
17
+ direction: direction }
18
+ rescue
19
+ nil
20
+ end
21
+
22
+ def output
23
+ migration_notes unless notes.empty?
24
+ @notes = []
25
+ end
26
+
27
+ private
28
+
29
+ def migration_notes
30
+ migration_notes_header
31
+
32
+ notes.each do |note|
33
+ say_message(note)
34
+ end
35
+
36
+ migration_notes_footer
37
+ end
38
+
39
+ def say_message(note)
40
+ puts title(note)
41
+ puts note[:content]
42
+ puts '' unless notes.last == note
43
+ end
44
+
45
+ def title(note)
46
+ title = case note[:direction]
47
+ when :up then '»» MIGRATED'
48
+ when :down then '«« REVERTED'
49
+ end
50
+ title += " - #{note[:name]}"
51
+ len = [0, 77 - (title.length + note[:version].to_s.length)].max
52
+ format('%s %s %s', colorize(title, 33), ' ' * len, note[:version])
53
+ end
54
+
55
+ def migration_notes_header
56
+ text = 'MIGRATION NOTES:'
57
+ len = [0, 75 - text.length].max
58
+ puts format('== %s %s', colorize(text, 36), '=' * len)
59
+ end
60
+
61
+ def migration_notes_footer
62
+ puts '=' * 79
63
+ end
64
+
65
+ def colorize(str, color_code)
66
+ return str unless terminal_supports_colors?
67
+ "\e[#{color_code}m#{str}\e[0m"
68
+ end
69
+
70
+ def terminal_supports_colors?
71
+ cmd = 'which tput>/dev/null 2>&1 && [[ $(tput -T$TERM colors) -ge 8 ]]'
72
+ system(cmd)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,17 @@
1
+ module ActiveRecord
2
+ class Migration
3
+ alias _original_migrate migrate
4
+
5
+ def migrate(direction)
6
+ _original_migrate(direction)
7
+ case direction
8
+ when :up
9
+ return unless respond_to?(:up_notes)
10
+ MigrationNotes::Handler.instance.add(version, name, up_notes, :up)
11
+ when :down
12
+ return unless respond_to?(:down_notes)
13
+ MigrationNotes::Handler.instance.add(version, name, down_notes, :down)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module ActiveRecord
4
+ module MigrationNotes
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load File.join(File.dirname(__FILE__), 'tasks.rake')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ task :migration_notes do
2
+ ActiveRecord::MigrationNotes::Handler.instance.output
3
+ end
4
+
5
+ %w(db:migrate db:rollback).each do |task|
6
+ Rake::Task[task].enhance do
7
+ Rake::Task[:migration_notes].invoke
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ module MigrationNotes
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ # rubocop:disable Style/FileName
2
+ require 'active_record'
3
+ # rubocop:enable Style/FileName
4
+ require 'active_record/migration_notes/migration'
5
+ require 'active_record/migration_notes/handler'
6
+ require 'active_record/migration_notes/railtie' if defined? Rails
7
+ require 'active_record/migration_notes/version'
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::MigrationNotes::Handler do
4
+ subject { ActiveRecord::MigrationNotes::Handler.instance }
5
+
6
+ it 'initializes with an empty message array' do
7
+ expect(subject.notes).to eq []
8
+ end
9
+
10
+ before :each do
11
+ subject.notes = []
12
+ end
13
+ describe '#add' do
14
+ it 'adds the input as message' do
15
+ subject.add(1234, 'test', 'some content')
16
+ expect(subject.notes.count).to eq 1
17
+ end
18
+ it 'rescues if something goes wrong' do
19
+ expect(subject.add(nil, nil, nil)).to eq nil
20
+ expect(subject.notes.count).to eq 0
21
+ end
22
+ describe 'formats the content' do
23
+ it 'removes the trailing spaces at the beginnen and adds 3 spaces instead' do
24
+ content = "line
25
+ text
26
+ some more"
27
+ subject.add(1, 'test', content)
28
+ expect(subject.notes.first[:content]).to eq " line\n text\n some more"
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#output' do
34
+ it 'skips the call migration_notes if there are no messages' do
35
+ expect(subject).not_to receive(:migration_notes)
36
+ subject.output
37
+ end
38
+ it 'calls migration_notes if there are messages' do
39
+ subject.add(1234, 'test', 'some content')
40
+ expect(subject).to receive(:migration_notes)
41
+ subject.output
42
+ end
43
+ it 'clears the messages afterwards' do
44
+ subject.add(1234, 'test', 'some content')
45
+ allow(subject).to receive(:migration_notes)
46
+ expect(subject.notes.count).to eq 1
47
+ subject.output
48
+ expect(subject.notes.count).to eq 0
49
+ end
50
+ end
51
+
52
+ describe '#migration_notes' do
53
+ it 'calls the header and footer' do
54
+ expect(subject).to receive(:migration_notes_header)
55
+ expect(subject).to receive(:migration_notes_footer)
56
+ subject.send(:migration_notes)
57
+ end
58
+ it 'loops trough the messages and calls say_message' do
59
+ allow(subject).to receive(:migration_notes_header)
60
+ allow(subject).to receive(:migration_notes_footer)
61
+
62
+ expect(subject.notes).to receive(:each).and_yield 1
63
+ expect(subject).to receive(:say_message).with(1)
64
+ subject.send(:migration_notes)
65
+ end
66
+ end
67
+
68
+ describe '#say_message' do
69
+ before :each do
70
+ subject.notes = []
71
+ subject.add(1, 'name-1', 'content-1', :up)
72
+ subject.add(2, 'name-2', 'content-2', :down)
73
+ end
74
+ it 'outputs the version and name and content' do
75
+ expect(subject).to receive(:puts).with("\e[33m»» MIGRATED - name-1\e[0m 1")
76
+ expect(subject).to receive(:puts).with(' content-1')
77
+ expect(subject).to receive(:puts).with('')
78
+ subject.send(:say_message, subject.notes.first)
79
+ end
80
+ it 'will not add a blank line if message was the last message' do
81
+ expect(subject).to receive(:puts).with("\e[33m«« REVERTED - name-2\e[0m 2")
82
+ expect(subject).to receive(:puts).with(' content-2')
83
+ expect(subject).not_to receive(:puts).with('')
84
+ subject.send(:say_message, subject.notes.last)
85
+ end
86
+ end
87
+
88
+ describe '#migration_notes_header' do
89
+ it 'outputs the header text' do
90
+ header = "== \e[36mMIGRATION NOTES:\e[0m ==========================================================="
91
+ expect(subject).to receive('puts').with(header)
92
+ subject.send(:migration_notes_header)
93
+ end
94
+ end
95
+
96
+ describe '#migration_notes_footer' do
97
+ it 'outputs the footer text' do
98
+ footer = '==============================================================================='
99
+ expect(subject).to receive('puts').with(footer)
100
+ subject.send(:migration_notes_footer)
101
+ end
102
+ end
103
+
104
+ describe '#colorize' do
105
+ it 'returns the str if terminal_supports_colors? is false' do
106
+ expect(subject).to receive(:terminal_supports_colors?) { false }
107
+ expect(subject.send(:colorize, 'content', 36)).to eq 'content'
108
+ end
109
+ it 'returns the color formatted string' do
110
+ expect(subject).to receive(:terminal_supports_colors?) { true }
111
+ expect(subject.send(:colorize, 'content', 36)).to eq "\e[36mcontent\e[0m"
112
+ end
113
+ end
114
+
115
+ describe '#terminal_supports_colors?' do
116
+ it 'executes the system call to check terminal color support' do
117
+ cmd = 'which tput>/dev/null 2>&1 && [[ $(tput -T$TERM colors) -ge 8 ]]'
118
+ expect(subject).to receive(:system).with(cmd)
119
+ subject.send(:terminal_supports_colors?)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Migration do
4
+ subject { ActiveRecord::Migration.new }
5
+
6
+ describe '#migrate' do
7
+ it 'triggers the original aliased method' do
8
+ expect(subject).to receive(:_original_migrate).with(:up)
9
+ subject.migrate(:up)
10
+ end
11
+
12
+ describe 'message handling' do
13
+ before :each do
14
+ allow(subject).to receive(:_original_migrate)
15
+ subject.version = 1
16
+ subject.name = 'name'
17
+ end
18
+ context 'direction is up' do
19
+ it 'adds the message if migration responds to up_notes' do
20
+ expect(subject).to receive(:up_notes) { 'content' }
21
+ expect(ActiveRecord::MigrationNotes::Handler.instance)
22
+ .to receive(:add).with(1, 'name', 'content', :up)
23
+ subject.migrate(:up)
24
+ end
25
+ it 'skips if migration has no up_notes method' do
26
+ expect(ActiveRecord::MigrationNotes::Handler.instance)
27
+ .not_to receive(:add)
28
+ subject.migrate(:up)
29
+ end
30
+ end
31
+ context 'direction is down' do
32
+ it 'adds the message if migration responds to down_notes' do
33
+ expect(subject).to receive(:down_notes) { 'content' }
34
+ expect(ActiveRecord::MigrationNotes::Handler.instance)
35
+ .to receive(:add).with(1, 'name', 'content', :down)
36
+ subject.migrate(:down)
37
+ end
38
+ it 'skips if migration has no down_notes method' do
39
+ expect(ActiveRecord::MigrationNotes::Handler.instance)
40
+ .not_to receive(:add)
41
+ subject.migrate(:down)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::MigrationNotes do
4
+ it 'has a version number' do
5
+ expect(ActiveRecord::MigrationNotes::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'activerecord-migration_notes'
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-migration_notes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Manuel van Rijn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-30 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: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ description: ActiveRecord Migration Notes hooks into the Rails migrations and monitors
70
+ if the migrations contain a note methods that should be shown afterwards.
71
+ email:
72
+ - manuel@manuelvanrijn.nl
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - activerecord-migration_notes.gemspec
85
+ - lib/active_record/migration_notes/handler.rb
86
+ - lib/active_record/migration_notes/migration.rb
87
+ - lib/active_record/migration_notes/railtie.rb
88
+ - lib/active_record/migration_notes/tasks.rake
89
+ - lib/active_record/migration_notes/version.rb
90
+ - lib/activerecord-migration_notes.rb
91
+ - spec/active_record/migration_notes/handler_spec.rb
92
+ - spec/active_record/migration_notes/migration_spec.rb
93
+ - spec/active_record/migration_notes/post_message_spec.rb
94
+ - spec/spec_helper.rb
95
+ homepage: http://manuelvanrijn.nl
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.5.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Add notes to your migrations, to explain additional steps to take for example
119
+ test_files:
120
+ - spec/active_record/migration_notes/handler_spec.rb
121
+ - spec/active_record/migration_notes/migration_spec.rb
122
+ - spec/active_record/migration_notes/post_message_spec.rb
123
+ - spec/spec_helper.rb