migration_data 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ea11a5103b28e5486a3e9568d4788036c2705d038648af2dba3ce67f97fef48
4
- data.tar.gz: 476de225ff2914003630c272755e113e610b8b96729c9e1c02e4723a7037ed2d
3
+ metadata.gz: fcfa827b7ff6fe4bf4509b6c4a5d148a52ad5328312de09c482b6532d3bdf893
4
+ data.tar.gz: 7ce0546a76606344b3b17c0e26bfae78724c66556e3c928cefc8a2919910489c
5
5
  SHA512:
6
- metadata.gz: 73c45622d5caace7a6841f556a6a10719722488fc63733aca8165e26c23cf61149707b053002c6de37ac67dd535d9a289ff80310b75e18eaa1ce4f24aaf45903
7
- data.tar.gz: bb7b355049417e35eb0bb359111eace4be2b33faf0f3262be48c85b108331af214f5f2d628bb1798eb273e61cf0cbdb4dcd8cfc0b9c93cfea3df9c1a984807b5
6
+ metadata.gz: bb1ed960aadd4db399fd6a9d0b1881484bfa494cbe654cc995114b6c23e0031d78ae77d9b1e2f888c72df885f77c33d69e9d8bb3e4f8a9df67f34b084fee6146
7
+ data.tar.gz: 6ed2163de0401cf380e786a823358d714f948eb968a3132c33a1672dfaac5a2e83e7c04b6b74cb0a14c767ab36ab1254a227cee71c7b3ce0ea9eef7c0380ea37
data/README.md CHANGED
@@ -81,7 +81,21 @@ describe CreateUsers do
81
81
  end
82
82
  ```
83
83
 
84
- The helper to load migrations `require_migration` is defined in the `migration_data/testing`. So you should to require it to have access to this convinient require extension.
84
+ The helper to load migrations `require_migration` is defined in the `migration_data/testing`. So you should to require it to have access to this convenient require extension.
85
+
86
+ ## Skipping data migrations execution
87
+
88
+ At some point, one might realize that data migrations should not run on particular environments, e.g. `test`.
89
+
90
+ On performing migrations in `test` environment, a data migration might try to add the same data that has already been added by seeds. In that case, migrations might fail with a duplication error.
91
+
92
+ Use `MigrationData.config.skip = true` to skip data migrations execution. One might put this code in an initializer, e.g. `config/initializers/migration_data.rb`:
93
+
94
+ ```ruby
95
+ if Rails.env.test?
96
+ MigrationData.config.skip = true
97
+ end
98
+ ```
85
99
 
86
100
  ## Contributing
87
101
 
@@ -1,8 +1,6 @@
1
1
  require 'active_record'
2
2
  require 'migration_data/version'
3
3
  require 'migration_data/active_record/migration'
4
-
5
- require 'rake'
6
- import File.join(File.dirname(__FILE__), 'tasks', 'db.rake')
4
+ require 'migration_data/config'
7
5
 
8
6
  ActiveRecord::Migration.send(:include, MigrationData::ActiveRecord::Migration)
@@ -15,6 +15,8 @@ module MigrationData
15
15
  base.class_eval do
16
16
  def exec_migration_with_data(conn, direction)
17
17
  origin_exec_migration(conn, direction)
18
+ ::ActiveRecord::Base.connection.schema_cache.clear!
19
+ return if MigrationData.config.skip_data_on_test
18
20
  data if direction == :up && respond_to?(:data)
19
21
  rollback if direction == :down && respond_to?(:rollback)
20
22
  end
@@ -0,0 +1,6 @@
1
+ module MigrationData
2
+ @config = Struct.new(:skip_data_on_test).new(false)
3
+ def self.config
4
+ @config
5
+ end
6
+ end
@@ -17,6 +17,8 @@ end
17
17
  def all_migrations(path)
18
18
  if Rails::VERSION::MAJOR >= 5 && Rails::VERSION::MINOR >= 2
19
19
  ActiveRecord::MigrationContext.new(path).migrations
20
+ elsif Rails::VERSION::MAJOR > 5
21
+ ActiveRecord::MigrationContext.new(path, nil).migrations
20
22
  else
21
23
  ActiveRecord::Migrator.migrations(path)
22
24
  end
@@ -1,3 +1,3 @@
1
1
  module MigrationData
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -38,4 +38,24 @@ describe MyMigration do
38
38
  assert_nil @migration.rolled_back_data
39
39
  end
40
40
  end
41
+
42
+ describe "#migrate with Rails.env = test and skip_data_on_test = true" do
43
+ before do
44
+ @old_skip, MigrationData.config.skip_data_on_test = MigrationData.config.skip_data_on_test, true
45
+ end
46
+
47
+ after do
48
+ MigrationData.config.skip_data_on_test = @old_skip
49
+ end
50
+
51
+ it "doesn't runs #data" do
52
+ @migration.migrate(:up)
53
+ refute @migration.migrated_data
54
+ end
55
+
56
+ it "doesn't runs #rollback" do
57
+ @migration.migrate(:down)
58
+ refute @migration.rolled_back_data
59
+ end
60
+ end
41
61
  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.4.0
4
+ version: 0.5.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: 2020-01-27 00:00:00.000000000 Z
11
+ date: 2020-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,7 @@ files:
72
72
  - Rakefile
73
73
  - lib/migration_data.rb
74
74
  - lib/migration_data/active_record/migration.rb
75
+ - lib/migration_data/config.rb
75
76
  - lib/migration_data/testing.rb
76
77
  - lib/migration_data/version.rb
77
78
  - migration_data.gemspec