mover 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe Mover::Migrator do
4
+
5
+ before(:each) do
6
+ $db.migrate(0)
7
+ $db.migrate(1)
8
+ end
9
+
10
+ describe :method_missing_with_mover do
11
+
12
+ it 'should migrate both tables up' do
13
+ migrate_with_state(2)
14
+ (@new_article_columns - @old_article_columns).should == [ 'permalink' ]
15
+ (@new_archive_columns - @old_archive_columns).should == [ 'permalink' ]
16
+ end
17
+
18
+ it 'should migrate both tables down' do
19
+ $db.migrate(2)
20
+ migrate_with_state(1)
21
+ (@old_article_columns - @new_article_columns).should == [ 'permalink' ]
22
+ (@old_archive_columns - @new_archive_columns).should == [ 'permalink' ]
23
+ end
24
+
25
+ it "should not touch the archive's move_id or moved_at column" do
26
+ connection.add_column(:articles, :move_id, :integer)
27
+ connection.add_column(:articles, :moved_at, :datetime)
28
+ migrate_with_state(3)
29
+ (@old_article_columns - @new_article_columns).should == [ 'move_id', 'moved_at' ]
30
+ (@old_archive_columns - @new_archive_columns).should == []
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,155 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe Mover::Base::Record do
4
+
5
+ before(:all) do
6
+ $db.migrate(0)
7
+ $db.migrate(1)
8
+ end
9
+
10
+ describe :InstanceMethods do
11
+ describe :move_to do
12
+
13
+ before(:all) do
14
+ @articles = create_records
15
+ @comments = create_records(Comment)
16
+ @articles[0..1].each do |a|
17
+ a.move_to(:archived)
18
+ end
19
+ end
20
+
21
+ it "should move some records to the archive table" do
22
+ Article.count.should == 3
23
+ ArchivedArticle.count.should == 2
24
+ end
25
+
26
+ it "should preserve record attributes" do
27
+ 2.times do |x|
28
+ original = @articles[x]
29
+ copy = ArchivedArticle.find(original.id)
30
+ record_match?(original, copy)
31
+ end
32
+ end
33
+
34
+ it "should move associated records" do
35
+ Comment.count.should == 3
36
+ ArchivedComment.count.should == 2
37
+ end
38
+
39
+ it "should preserve associated record attributes" do
40
+ 2.times do |x|
41
+ original = @comments[x]
42
+ copy = ArchivedComment.find(original.id)
43
+ record_match?(original, copy)
44
+ end
45
+ end
46
+
47
+ it "should populate move_id" do
48
+ (1..2).each do |x|
49
+ article = ArchivedArticle.find(x)
50
+ comment = ArchivedComment.find(x)
51
+ comment.move_id.nil?.should == false
52
+ comment.move_id.length.should == 32
53
+ comment.move_id.should == article.move_id
54
+ end
55
+ end
56
+
57
+ it "should populate moved_at" do
58
+ (1..2).each do |x|
59
+ article = ArchivedArticle.find(x)
60
+ comment = ArchivedComment.find(x)
61
+ comment.moved_at.nil?.should == false
62
+ comment.moved_at.should == article.moved_at
63
+ end
64
+ end
65
+ end
66
+
67
+ describe :move_from do
68
+
69
+ before(:all) do
70
+ articles = create_records
71
+ create_records(Comment)
72
+ articles[0..1].each do |a|
73
+ a.move_to(:archived)
74
+ end
75
+ @articles = ArchivedArticle.find(1, 2)
76
+ @comments = ArchivedComment.find(1, 2)
77
+ @articles.each do |article|
78
+ article.move_from
79
+ end
80
+ end
81
+
82
+ it "should move records back to the original table" do
83
+ Article.count.should == 5
84
+ ArchivedArticle.count.should == 0
85
+ end
86
+
87
+ it "should preserve record attributes" do
88
+ 2.times do |x|
89
+ original = @articles[x]
90
+ copy = Article.find(original.id)
91
+ record_match?(original, copy)
92
+ end
93
+ end
94
+
95
+ it "should move associated records" do
96
+ Comment.count.should == 5
97
+ ArchivedComment.count.should == 0
98
+ end
99
+
100
+ it "should preserve associated record attributes" do
101
+ 2.times do |x|
102
+ original = @comments[x]
103
+ copy = Comment.find(original.id)
104
+ record_match?(original, copy)
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ describe :ClassMethods do
111
+ describe :move_to do
112
+
113
+ before(:all) do
114
+ create_records
115
+ create_records(Comment)
116
+ Article.move_to(:archived, [ 'id = ? OR id = ?', 1, 2 ])
117
+ Article.move_to(:drafted, [ 'id = ? OR id = ?', 3, 4 ])
118
+ end
119
+
120
+ it "should move the records" do
121
+ Article.count.should == 1
122
+ ArchivedArticle.count.should == 2
123
+ DraftedArticle.count.should == 2
124
+ end
125
+
126
+ it "should move associated records" do
127
+ Comment.count.should == 3
128
+ ArchivedComment.count.should == 2
129
+ end
130
+ end
131
+
132
+ describe :move_from do
133
+
134
+ before(:all) do
135
+ create_records
136
+ create_records(Comment)
137
+ Article.move_to(:archived, [ 'id = ? OR id = ?', 1, 2 ])
138
+ Article.move_to(:drafted, [ 'id = ? OR id = ?', 3, 4 ])
139
+ Article.move_from(:archived, [ 'id = ? OR id = ?', 1, 2 ])
140
+ Article.move_from(:drafted, [ 'id = ? OR id = ?', 3, 4 ])
141
+ end
142
+
143
+ it "should move the records" do
144
+ Article.count.should == 5
145
+ ArchivedArticle.count.should == 0
146
+ DraftedArticle.count.should == 0
147
+ end
148
+
149
+ it "should move associated records" do
150
+ Comment.count.should == 5
151
+ ArchivedComment.count.should == 0
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe Mover::Base::Table do
4
+
5
+ before(:all) do
6
+ $db.migrate(0)
7
+ $db.migrate(1)
8
+ end
9
+
10
+ describe :create_movable_table do
11
+
12
+ before(:all) do
13
+ @article_columns = connection.columns("articles").collect(&:name)
14
+ @archive_columns = connection.columns("archived_articles").collect(&:name)
15
+ end
16
+
17
+ it "should create an archive table" do
18
+ connection.table_exists?("archived_articles").should == true
19
+ end
20
+
21
+ it "should create an archive table with the same structure as the original table" do
22
+ @article_columns.each do |col|
23
+ @archive_columns.include?(col).should == true
24
+ end
25
+ end
26
+
27
+ describe 'with options' do
28
+
29
+ before(:all) do
30
+ Article.drop_movable_table(:archived)
31
+ Article.create_movable_table(
32
+ :archived,
33
+ :columns => %w(id read),
34
+ :indexes => %w(read)
35
+ )
36
+ @archive_columns = connection.columns("archived_articles").collect(&:name)
37
+ end
38
+
39
+ after(:all) do
40
+ Article.drop_movable_table(:archived)
41
+ Article.create_movable_table(:archived)
42
+ end
43
+
44
+ it "should create the correct columns" do
45
+ @archive_columns.length.should == 2
46
+ %w(id read).each do |col|
47
+ @archive_columns.include?(col).should == true
48
+ end
49
+ end
50
+
51
+ it "should create archive indexes" do
52
+ indexes = Article.send(:indexed_columns, 'archived_articles')
53
+ indexes.to_set.should == [ "read" ].to_set
54
+ end
55
+ end
56
+
57
+ describe 'without options' do
58
+
59
+ it "should create archive indexes" do
60
+ indexes = Article.send(:indexed_columns, 'archived_articles')
61
+ indexes.to_set.should == [ "id", "title" ].to_set
62
+ end
63
+ end
64
+ end
65
+
66
+ describe :drop_movable_table do
67
+
68
+ it "should drop the table" do
69
+ Article.drop_movable_table(:archived)
70
+ output = connection.execute(<<-SQL)
71
+ SELECT COUNT(*)
72
+ FROM information_schema.tables
73
+ WHERE table_schema = '#{Article.configurations['test']['database']}'
74
+ AND table_name = 'archived_articles';
75
+ SQL
76
+ output.fetch_row.should == ['0']
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/../require")
2
+ Require.spec_helper!
3
+
4
+ Spec::Runner.configure do |config|
5
+ end
6
+
7
+ $db, $log = ActiveWrapper.setup(
8
+ :base => File.dirname(__FILE__),
9
+ :env => 'test'
10
+ )
11
+ $db.establish_connection
12
+
13
+ def record_match?(original, copy)
14
+ (original.class.column_names & copy.class.column_names).each do |col|
15
+ copy.send(col).should == original.send(col)
16
+ end
17
+ end
18
+
19
+ def columns(table)
20
+ connection.columns(table).collect(&:name)
21
+ end
22
+
23
+ def connection
24
+ ActiveRecord::Base.connection
25
+ end
26
+
27
+ def create_records(klass=Article, values={})
28
+ klass.delete_all
29
+ (1..5).collect do |x|
30
+ klass.column_names.each do |column|
31
+ if column == 'article_id'
32
+ values[:article_id] = x
33
+ else
34
+ values[column.intern] = "#{klass} #{x} #{column}"
35
+ end
36
+ end
37
+ values[:id] = x
38
+ klass.create(values)
39
+ end
40
+ end
41
+
42
+ def migrate_with_state(version)
43
+ @old_article_columns = columns("articles")
44
+ @old_archive_columns = columns("archived_articles")
45
+ $db.migrate(version)
46
+ @new_article_columns = columns("articles")
47
+ @new_archive_columns = columns("archived_articles")
48
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mover
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Winton Welsh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: require
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.6
24
+ version:
25
+ description:
26
+ email: mail@wintoni.us
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.markdown
33
+ files:
34
+ - init.rb
35
+ - lib/mover/migrator.rb
36
+ - lib/mover/record.rb
37
+ - lib/mover/table.rb
38
+ - lib/mover.rb
39
+ - log/development.log
40
+ - MIT-LICENSE
41
+ - rails/init.rb
42
+ - Rakefile
43
+ - README.markdown
44
+ - require.rb
45
+ - spec/config/database.yml.example
46
+ - spec/db/migrate/001_create_articles.rb
47
+ - spec/db/migrate/002_add_permalink.rb
48
+ - spec/db/migrate/003_remove_magic_columns.rb
49
+ - spec/fixtures/article.rb
50
+ - spec/fixtures/comment.rb
51
+ - spec/log/test.log
52
+ - spec/mover/migrator_spec.rb
53
+ - spec/mover/record_spec.rb
54
+ - spec/mover/table_spec.rb
55
+ - spec/Rakefile
56
+ - spec/spec_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://github.com/winton/mover
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.5
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Move ActiveRecord records across tables like it ain't no thang
85
+ test_files: []
86
+