migration_data 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -11
- data/lib/migration_data/active_record/migration.rb +1 -0
- data/lib/migration_data/version.rb +1 -1
- data/test/migration_test.rb +15 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d1f60bf8d7678f0a5e055ec9072236970ea0e5a
|
4
|
+
data.tar.gz: 0e2bfdf1e435d67eb0da4990f7b0d6169272581a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ed9ae7be493e852733dba3c5a0e71ec9f1aac4544eaa161105d97169ab7c80867e26accbc984c8382a4a5e8a358f2fdacc7e2b192633db0dd1db5488dd97a00
|
7
|
+
data.tar.gz: 491a28d86c4b1088e9b7dfa18c896feb00004453fda127985b2df51a7462a740a7121b7a3af332edce1bfbf7d84b3ac4a765310717717b97639963fb7d99a2b6
|
data/README.md
CHANGED
@@ -5,14 +5,14 @@
|
|
5
5
|
This gem provides functionality to write any code in migrations safely without regression.
|
6
6
|
|
7
7
|
Sometimes we have to write some Rails code in the migrations and it's hard to
|
8
|
-
keep them in working state because models
|
8
|
+
keep them in a working state because models which are used there change too often. There are
|
9
9
|
some techniques which help to avoid these pitfalls. For example, define model
|
10
|
-
classes in the migrations or write raw SQL. But they don't help in 100% cases
|
11
|
-
This gem promises to solve
|
10
|
+
classes in the migrations or write raw SQL. But they don't help in 100% of all cases.
|
11
|
+
This gem promises to solve this problem in a simple way.
|
12
12
|
|
13
13
|
Currently the gem supports Rails 4.
|
14
14
|
|
15
|
-
If you still don't understand what
|
15
|
+
If you still don't understand what this gem is for please check out [this blog post](http://railsguides.net/2014/01/30/change-data-in-migrations-like-a-boss/).
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
@@ -41,28 +41,35 @@ class CreateUsers < ActiveRecord::Migration
|
|
41
41
|
def data
|
42
42
|
User.create!(name: 'Andrey', email: 'ka8725@gmail.com')
|
43
43
|
end
|
44
|
+
|
45
|
+
def rollback
|
46
|
+
User.find_by(name: 'Andrey', email: 'ka8725@gmail.com').destroy
|
47
|
+
end
|
44
48
|
end
|
45
49
|
```
|
46
50
|
|
47
51
|
That's it. Now when you run migrations with the `rake db:migrate` command the `data` method will be run on `up`.
|
48
|
-
|
49
|
-
NOTE: it's not run on `down`. If you have any reason to do it please fell free to make pull request.
|
52
|
+
When you rollback migrations with the `rake db:rollback` command the `rollback` method will be run on `down`.
|
50
53
|
|
51
54
|
## Testing migrations
|
52
55
|
|
53
|
-
To keep your migrations working don't forget to write tests for them.
|
54
|
-
Possible `RSpec` test for the migration looks like this:
|
56
|
+
To keep your migrations working don't forget to write tests for them. It's preferably to put the tests for migrations into `spec/db/migrations` folder, but actually it's up to you. Possible `RSpec` test (`spec/db/migrations/create_user.rb`) for the migration looks like this:
|
55
57
|
|
56
58
|
```ruby
|
57
|
-
|
58
59
|
require 'spec_helper'
|
59
60
|
require 'migration_data/testing'
|
60
61
|
require_migration 'create_users'
|
61
62
|
|
62
|
-
|
63
|
+
describe CreateUsers do
|
63
64
|
describe '#data' do
|
64
65
|
it 'works' do
|
65
|
-
expect {
|
66
|
+
expect { described_class.new.data }.to_not raise_exception
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#rollback' do
|
71
|
+
it 'works' do
|
72
|
+
expect { described_class.new.rollback }.to_not raise_exception
|
66
73
|
end
|
67
74
|
end
|
68
75
|
end
|
@@ -6,6 +6,7 @@ module MigrationData
|
|
6
6
|
def exec_migration_with_data(conn, direction)
|
7
7
|
origin_exec_migration(conn, direction)
|
8
8
|
data if direction == :up && respond_to?(:data)
|
9
|
+
rollback if direction == :down && respond_to?(:rollback)
|
9
10
|
end
|
10
11
|
|
11
12
|
alias origin_exec_migration exec_migration
|
data/test/migration_test.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class MyMigration < ActiveRecord::Migration
|
4
|
-
attr_reader :migrated_data
|
4
|
+
attr_reader :migrated_data, :rolled_back_data
|
5
5
|
|
6
6
|
def data
|
7
7
|
@migrated_data = true
|
8
8
|
end
|
9
|
+
|
10
|
+
def rollback
|
11
|
+
@rolled_back_data = true
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
describe MyMigration do
|
@@ -23,5 +27,15 @@ describe MyMigration do
|
|
23
27
|
@migration.migrate(:down)
|
24
28
|
assert_nil @migration.migrated_data
|
25
29
|
end
|
30
|
+
|
31
|
+
it 'runs #rollback on down direction' do
|
32
|
+
@migration.migrate(:down)
|
33
|
+
assert @migration.rolled_back_data
|
34
|
+
end
|
35
|
+
|
36
|
+
it "doesn't run #rollback on up direction" do
|
37
|
+
@migration.migrate(:up)
|
38
|
+
assert_nil @migration.rolled_back_data
|
39
|
+
end
|
26
40
|
end
|
27
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: migration_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Koleshko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
113
|
version: '0'
|
114
114
|
requirements: []
|
115
115
|
rubyforge_project:
|
116
|
-
rubygems_version: 2.2.
|
116
|
+
rubygems_version: 2.2.2
|
117
117
|
signing_key:
|
118
118
|
specification_version: 4
|
119
119
|
summary: Provides possibility to write any code in migrations safely without regression.
|
@@ -122,4 +122,3 @@ test_files:
|
|
122
122
|
- test/migration_test.rb
|
123
123
|
- test/require_migration_test.rb
|
124
124
|
- test/test_helper.rb
|
125
|
-
has_rdoc:
|