column_info_reset 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ active_record.log
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "mysql", "2.7"
6
+ gem "activesupport", "1.4.4"
7
+ gem "activerecord", "1.15.6"
8
+
9
+ gem "rspec", "1.1.12"
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ column_info_reset (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activerecord (1.15.6)
10
+ activesupport (= 1.4.4)
11
+ activesupport (1.4.4)
12
+ mysql (2.7)
13
+ rspec (1.1.12)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ activerecord (= 1.15.6)
20
+ activesupport (= 1.4.4)
21
+ column_info_reset!
22
+ mysql (= 2.7)
23
+ rspec (= 1.1.12)
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "mysql2", "0.2.13"
6
+ gem "activesupport", "3.0.10"
7
+ gem "activerecord", "3.0.10"
8
+
9
+ gem "rspec", "2.6.0"
@@ -0,0 +1,42 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ column_info_reset (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activemodel (3.0.10)
10
+ activesupport (= 3.0.10)
11
+ builder (~> 2.1.2)
12
+ i18n (~> 0.5.0)
13
+ activerecord (3.0.10)
14
+ activemodel (= 3.0.10)
15
+ activesupport (= 3.0.10)
16
+ arel (~> 2.0.10)
17
+ tzinfo (~> 0.3.23)
18
+ activesupport (3.0.10)
19
+ arel (2.0.10)
20
+ builder (2.1.2)
21
+ diff-lcs (1.1.3)
22
+ i18n (0.5.0)
23
+ mysql2 (0.2.13)
24
+ rspec (2.6.0)
25
+ rspec-core (~> 2.6.0)
26
+ rspec-expectations (~> 2.6.0)
27
+ rspec-mocks (~> 2.6.0)
28
+ rspec-core (2.6.4)
29
+ rspec-expectations (2.6.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.6.0)
32
+ tzinfo (0.3.29)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ activerecord (= 3.0.10)
39
+ activesupport (= 3.0.10)
40
+ column_info_reset!
41
+ mysql2 (= 0.2.13)
42
+ rspec (= 2.6.0)
@@ -0,0 +1,31 @@
1
+ # Column Info Reset
2
+
3
+ Simple gem that reset ActiveRecord column information when
4
+
5
+ Unknown column 'bla' in 'field list'
6
+
7
+ exception occurs.
8
+
9
+ This gem allows you safely run migrations that to drop **unused** table columns.
10
+
11
+ ## Installation
12
+
13
+ gem install column_info_reset
14
+
15
+ ## Tests
16
+
17
+ Run all tests against two active records versions:
18
+
19
+ script/test
20
+
21
+ ### ActiveRecord v1.x.x
22
+
23
+ rvm install ruby-1.8.6
24
+ script/bundle-ar1 install
25
+ script/bundle-ar1 exec spec -fs -c spec
26
+
27
+ ### ActiveRecord v3.x.x
28
+
29
+ rvm install ruby-1.9.2
30
+ script/bundle-ar3 install
31
+ script/bundle-ar3 exec spec -fs -c spec
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "column_info_reset/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "column_info_reset"
7
+ s.version = ColumnInfoReset::VERSION
8
+ s.authors = ["Andriy Yanko"]
9
+ s.email = ["andriy.yanko@gmail.com"]
10
+ s.homepage = "https://github.com/railsware/column_info_reset"
11
+ s.summary = %q{Reset ActiveRecord column info when unknown column exception occurs}
12
+
13
+ s.rubyforge_project = "column_info_reset"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,11 @@
1
+ DROP TABLE IF EXISTS `column_info_reset_users`;
2
+
3
+ CREATE TABLE `column_info_reset_users` (
4
+ `id` int(11) NOT NULL AUTO_INCREMENT,
5
+ `email` varchar(127) DEFAULT NULL,
6
+ `state` varchar(127) DEFAULT NULL,
7
+ `country` varchar(127) DEFAULT NULL,
8
+ `created_at` datetime DEFAULT NULL,
9
+ `updated_at` datetime DEFAULT NULL,
10
+ PRIMARY KEY (`id`)
11
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@@ -0,0 +1,32 @@
1
+ require "column_info_reset/version"
2
+
3
+ module ColumnInfoReset
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_eval do
7
+ class << self
8
+ alias_method :transaction_without_column_info_reset, :transaction
9
+ alias_method :transaction, :transaction_with_column_info_reset
10
+ end
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def transaction_with_column_info_reset(*objects, &block)
16
+ begin
17
+ transaction_without_column_info_reset(*objects, &block)
18
+ rescue ActiveRecord::StatementInvalid => e
19
+ if e.message =~ /Unknown column '.*' in 'field list'/
20
+ logger.info "Retrying with column information reset"
21
+ reset_column_information
22
+ transaction_without_column_info_reset(*objects, &block)
23
+ else
24
+ raise e
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ require 'active_record'
32
+ ActiveRecord::Base.send :include, ColumnInfoReset
@@ -0,0 +1,3 @@
1
+ module ColumnInfoReset
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ BUNDLE_GEMFILE=Gemfile-ar1 rvm ruby-1.8.6 exec bundle $*
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ BUNDLE_GEMFILE=Gemfile-ar3 rvm ruby-1.9.2 exec bundle $*
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+
3
+ echo "*** Against ActiveRecord v1.x.x"
4
+ script/bundle-ar1 exec spec -f progress -c spec
5
+
6
+ echo "*** Against ActiveRecord v3.x.x"
7
+ script/bundle-ar3 exec rspec -f progress -c spec
@@ -0,0 +1,74 @@
1
+ require 'column_info_reset'
2
+ require 'logger'
3
+
4
+ begin
5
+ require 'mysql2'
6
+ rescue LoadError
7
+ end
8
+
9
+ begin
10
+ require 'mysql'
11
+ rescue LoadError
12
+ end
13
+
14
+ class ColumnInfoResetUser < ActiveRecord::Base
15
+ end
16
+
17
+ describe "Record Creation" do
18
+ before(:each) do
19
+ ActiveRecord::Base.establish_connection({
20
+ :database => "test",
21
+ :adapter => defined?(Mysql2) ? "mysql2" : "mysql",
22
+ :host => "localhost",
23
+ :username => "root",
24
+ :password => nil,
25
+ :encoding => "utf8",
26
+ })
27
+ ActiveRecord::Base.logger = Logger.new('active_record.log')
28
+
29
+ File.read('db/structure.sql').strip.split("\n\n").each do |statement|
30
+ ActiveRecord::Base.connection.execute(statement)
31
+ end
32
+ end
33
+
34
+ it "shoud create record as usual" do
35
+ ColumnInfoResetUser.column_names.sort.should == [
36
+ "country", "created_at", "email", "id", "state", "updated_at"
37
+ ]
38
+
39
+ lambda {
40
+ ColumnInfoResetUser.create!(:email => 'alice@mirror.com')
41
+ }.should_not raise_error
42
+
43
+ record = ColumnInfoResetUser.find :first
44
+ record.email.should == 'alice@mirror.com'
45
+ record.should respond_to(:country)
46
+ end
47
+
48
+ context "when column does not exist in database" do
49
+ it "shoud retry transaction and create record without column" do
50
+ ColumnInfoResetUser.column_names.sort.should == [
51
+ "country", "created_at", "email", "id", "state", "updated_at"
52
+ ]
53
+
54
+ ActiveRecord::Base.connection.remove_column(ColumnInfoResetUser.table_name, "country")
55
+
56
+ ColumnInfoResetUser.column_names.sort.should == [
57
+ "country", "created_at", "email", "id", "state", "updated_at"
58
+ ]
59
+
60
+ lambda {
61
+ ColumnInfoResetUser.create!(:email => 'alice@mirror.com')
62
+ }.should_not raise_error
63
+
64
+ record = ColumnInfoResetUser.find :first
65
+ record.email.should == 'alice@mirror.com'
66
+ record.should_not respond_to(:country)
67
+
68
+ ColumnInfoResetUser.column_names.sort.should == [
69
+ "created_at", "email", "id", "state", "updated_at"
70
+ ]
71
+ end
72
+ end
73
+ end
74
+
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: column_info_reset
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andriy Yanko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-20 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - andriy.yanko@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile-ar1
23
+ - Gemfile-ar1.lock
24
+ - Gemfile-ar3
25
+ - Gemfile-ar3.lock
26
+ - README.md
27
+ - Rakefile
28
+ - column_info_reset.gemspec
29
+ - db/structure.sql
30
+ - lib/column_info_reset.rb
31
+ - lib/column_info_reset/version.rb
32
+ - script/bundle-ar1
33
+ - script/bundle-ar3
34
+ - script/test
35
+ - spec/column_info_reset_spec.rb
36
+ homepage: https://github.com/railsware/column_info_reset
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project: column_info_reset
56
+ rubygems_version: 1.8.6
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Reset ActiveRecord column info when unknown column exception occurs
60
+ test_files:
61
+ - spec/column_info_reset_spec.rb