safe-migrations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in safe-migrations.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bob Remeika
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ safe-migrations
2
+ ===============
3
+
4
+ Assert rails migration safety at dev time.
5
+
6
+ blah blah blah
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'safe-migrations'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install safe-migrations
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,32 @@
1
+
2
+ __ __ _____ _______ _
3
+ \ \ / /\ |_ _|__ __| |
4
+ \ \ /\ / / \ | | | | | |
5
+ \ \/ \/ / /\ \ | | | | | |
6
+ \ /\ / ____ \ _| |_ | | |_|
7
+ \/ \/_/ \_\_____| |_| (_)
8
+
9
+ YOUR MIGRATION DID NOT FINISH!
10
+
11
+ You are running a migration that can be problematic
12
+ unless you have taken the proper steps to ensure that
13
+ it is successful.
14
+
15
+ ActiveRecord caches attributes which can cause problems
16
+ when removing columns, adding columns or changing column
17
+ names.
18
+
19
+ Read this article for more information:
20
+
21
+ http://pedro.herokuapp.com/past/2011/7/13/rails_migrations_with_no_downtime/
22
+
23
+ Once you are confident that your migration is safe, wrap
24
+ your migration in a safety_assured block. See below:
25
+
26
+ class MySafeMigration < ActiveRecord::Migration
27
+ def self.up
28
+ safety_assured { remove_column :foo_table, :foo_column }
29
+ end
30
+ end
31
+
32
+ Have a nice day!
@@ -0,0 +1,8 @@
1
+ require "active_record"
2
+ require "benchmark"
3
+
4
+ require "safe_migrations/version"
5
+ require "safe_migrations/errors"
6
+ require "safe_migrations/migration_ext"
7
+
8
+ ActiveRecord::Migration.extend(SafeMigrations::MigrationExt::ClassMethods)
@@ -0,0 +1,13 @@
1
+ module SafeMigrations
2
+ class UnsafeMigration < StandardError
3
+ GENERIC_BANNER = File.read(File.join(File.dirname(__FILE__), "..", "..", "banner.txt"))
4
+
5
+ def initialize(message = "")
6
+ super(GENERIC_BANNER + "\n#{message}\n")
7
+ end
8
+ end
9
+
10
+ class UnsafeAddColumn < UnsafeMigration; end
11
+ class UnsafeRemoveColumn < UnsafeMigration; end
12
+ class UnsafeDropTable < UnsafeMigration; end
13
+ end
@@ -0,0 +1,38 @@
1
+ module SafeMigrations
2
+ module MigrationExt
3
+ module ClassMethods
4
+ UNSAFE_METHODS = [:drop_table, :remove_column]
5
+
6
+ def self.extended(base)
7
+ class << base
8
+ alias_method_chain :method_missing, :safety
9
+ end
10
+ end
11
+
12
+ def safety_assured(&block)
13
+ @safety = true
14
+ yield
15
+ ensure
16
+ @safety = false
17
+ end
18
+
19
+ def safe?
20
+ !!@safety
21
+ end
22
+
23
+ def method_missing_with_safety(method, *args, &block)
24
+ if safe? || !UNSAFE_METHODS.include?(method)
25
+ return method_missing_without_safety(method, *args, &block)
26
+ end
27
+
28
+ case method
29
+ when :remove_column
30
+ raise UnsafeRemoveColumn
31
+ when :drop_table
32
+ raise UnsafeDropTable
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module SafeMigrations
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'safe_migrations/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "safe-migrations"
8
+ gem.version = SafeMigrations::VERSION
9
+ gem.authors = ["Bob Remeika"]
10
+ gem.email = ["bob.remeika@gmail.com"]
11
+ gem.description = %q{Assert rails migration safety at dev time}
12
+ gem.summary = %q{Protect yourself from migrations that are unsafe to run without downtime}
13
+ gem.homepage = "http://github.com/foobarfighter/safe-migrations"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rr"
21
+ gem.add_development_dependency "rspec"
22
+ gem.add_development_dependency "rake"
23
+ gem.add_runtime_dependency "activerecord", "3.0.20"
24
+ end
@@ -0,0 +1,23 @@
1
+ class TestUnsafeDropTable < ActiveRecord::Migration
2
+ def self.up
3
+ drop_table :some_table
4
+ end
5
+ end
6
+
7
+ class TestUnsafeRemoveColumn < ActiveRecord::Migration
8
+ def self.up
9
+ remove_column :some_table, :some_column
10
+ end
11
+ end
12
+
13
+ class TestSafeDropTable < ActiveRecord::Migration
14
+ def self.up
15
+ safety_assured { drop_table :some_table }
16
+ end
17
+ end
18
+
19
+ class TestSafeRemoveColumn < ActiveRecord::Migration
20
+ def self.up
21
+ safety_assured { remove_column :some_table, :some_column }
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+ require File.dirname(__FILE__) + "/migrations"
3
+
4
+ describe "SafeMigrations::MigrationExtTest" do
5
+ describe "when there is no safety assurance" do
6
+ it "should not let you run remove_column" do
7
+ lambda {
8
+ TestUnsafeRemoveColumn.up
9
+ }.should raise_error(SafeMigrations::UnsafeRemoveColumn)
10
+ end
11
+
12
+ it "should not let you run drop table" do
13
+ lambda {
14
+ TestUnsafeDropTable.up
15
+ }.should raise_error(SafeMigrations::UnsafeDropTable)
16
+ end
17
+ end
18
+
19
+ describe "when there is safety assurance" do
20
+ it "should let you run remove_column" do
21
+ mock(TestSafeRemoveColumn).method_missing_without_safety(
22
+ :remove_column,
23
+ anything,
24
+ anything
25
+ )
26
+
27
+ TestSafeRemoveColumn.up
28
+ end
29
+
30
+ it "should let you run drop_table" do
31
+ mock(TestSafeDropTable).method_missing_without_safety(
32
+ :drop_table,
33
+ anything
34
+ )
35
+
36
+ TestSafeDropTable.up
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ require "safe_migrations"
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ config.backtrace_clean_patterns = [ ]
6
+ config.mock_framework = :rr
7
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe-migrations
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Bob Remeika
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-05-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ prerelease: false
31
+ type: :development
32
+ name: rr
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ prerelease: false
45
+ type: :development
46
+ name: rspec
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ prerelease: false
59
+ type: :development
60
+ name: rake
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - "="
67
+ - !ruby/object:Gem::Version
68
+ hash: 47
69
+ segments:
70
+ - 3
71
+ - 0
72
+ - 20
73
+ version: 3.0.20
74
+ prerelease: false
75
+ type: :runtime
76
+ name: activerecord
77
+ version_requirements: *id004
78
+ description: Assert rails migration safety at dev time
79
+ email:
80
+ - bob.remeika@gmail.com
81
+ executables: []
82
+
83
+ extensions: []
84
+
85
+ extra_rdoc_files: []
86
+
87
+ files:
88
+ - .gitignore
89
+ - Gemfile
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - banner.txt
94
+ - lib/safe_migrations.rb
95
+ - lib/safe_migrations/errors.rb
96
+ - lib/safe_migrations/migration_ext.rb
97
+ - lib/safe_migrations/version.rb
98
+ - safe-migrations.gemspec
99
+ - spec/migrations.rb
100
+ - spec/safe_migrations_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: http://github.com/foobarfighter/safe-migrations
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.24
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Protect yourself from migrations that are unsafe to run without downtime
135
+ test_files:
136
+ - spec/migrations.rb
137
+ - spec/safe_migrations_spec.rb
138
+ - spec/spec_helper.rb