dekiru 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/rspec.yml +1 -1
- data/README.md +43 -3
- data/dekiru.gemspec +2 -2
- data/lib/dekiru/data_migration_operator.rb +7 -5
- data/lib/dekiru/tasks/db.rake +7 -7
- data/lib/dekiru/transaction_provider.rb +11 -0
- data/lib/dekiru/version.rb +1 -1
- data/lib/dekiru.rb +3 -2
- metadata +6 -8
- data/lib/dekiru/validators/existence.rb +0 -27
- data/spec/dekiru/validators/existence_spec.rb +0 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1c032693e2658b61ee9744b7e872a4379eb194d9f4e3a4ac1147c4b5025682b
|
4
|
+
data.tar.gz: 14321e4bf0b2574358bfc29147bf801480801181af1c8e40879d8f4357cc7fca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5f36a181ddd30cf6500c5d454ceffb5cb058a5aeb5de29b1f44b96eb9eb6fe4796522bd8e0b20f0e3ae09cb5ca83f86688ac7ff1df4421522c0bbacc83ad9ac
|
7
|
+
data.tar.gz: e39f08ff48c0f412c7fbe7ff755b2c2dd77378aca03172bff6622b09d26c6bc7f94e08cefbe5ebdff575504550520da146e7b4f312b9c3b14fef6b6f96242523
|
data/.github/workflows/rspec.yml
CHANGED
data/README.md
CHANGED
@@ -25,7 +25,7 @@ Or install it yourself as:
|
|
25
25
|
```ruby
|
26
26
|
require 'dekiru/capybara/helpers'
|
27
27
|
RSpec.configure do |config|
|
28
|
-
config.include Dekiru::Capybara::Helpers, type: :
|
28
|
+
config.include Dekiru::Capybara::Helpers, type: :system
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
@@ -55,7 +55,7 @@ wait_for_element_position_stable(element)
|
|
55
55
|
```ruby
|
56
56
|
require 'dekiru/capybara/matchers'
|
57
57
|
RSpec.configure do |config|
|
58
|
-
config.include Dekiru::Capybara::Matchers, type: :
|
58
|
+
config.include Dekiru::Capybara::Matchers, type: :system
|
59
59
|
end
|
60
60
|
```
|
61
61
|
|
@@ -64,7 +64,7 @@ end
|
|
64
64
|
```ruby
|
65
65
|
# javascriptエラーがあったらテスト失敗するように
|
66
66
|
RSpec.configure do |config|
|
67
|
-
config.after(:each, type: :
|
67
|
+
config.after(:each, type: :system) do |example|
|
68
68
|
if example.metadata[:js] == true
|
69
69
|
expect(page).to have_no_js_errors
|
70
70
|
end
|
@@ -201,6 +201,46 @@ Dekiru.configure do |config|
|
|
201
201
|
end
|
202
202
|
```
|
203
203
|
|
204
|
+
### Dekiru::TransactionProvider
|
205
|
+
|
206
|
+
`Dekiru::DataMigrationOperator` を使うスクリプトにおいて、複数データベースへの書き込みが必要な場合など、`ActiveRecord::Base.transaction` によるトランザクション開始だけでは不十分な場合があります。`Dekiru::TransactionProvider` を実装することで `Dekiru::DataMigrationOperator` のトランザクション開始の挙動をカスタマイズすることができます。
|
207
|
+
|
208
|
+
以下のようなアプリケーションコードがあるとします。
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
class LegacyRecord < ApplicationRecord
|
212
|
+
connects_to database: { writing: :legacy, reading: :legacy }
|
213
|
+
end
|
214
|
+
|
215
|
+
class ApplicationRecord < ActiveRecord::Base
|
216
|
+
connects_to database: { writing: :primary, reading: :primary }
|
217
|
+
|
218
|
+
def self.with_legacy_transaction
|
219
|
+
ActiveRecord::Base.transaction do
|
220
|
+
LegacyRecord.transaction do
|
221
|
+
yield
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
```
|
227
|
+
|
228
|
+
`Dekiru::DataMigrationOperator` においても `ApplicationRecord.with_legacy_transaction` を使ってトランザクション開始するために、以下のような設定を用意します。
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
# config/initializer/dekiru.rb
|
232
|
+
class MyTransactionProvider < Dekiru::TransactionProvider
|
233
|
+
def within_transaction(&)
|
234
|
+
ApplicationRecord.with_legacy_transaction(&)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
Dekiru.configure do |config|
|
239
|
+
config.transaction_provider = MyTransactionProvider.new
|
240
|
+
end
|
241
|
+
```
|
242
|
+
|
243
|
+
|
204
244
|
## Refinements
|
205
245
|
|
206
246
|
### Dekiru::CamelizeHash
|
data/dekiru.gemspec
CHANGED
@@ -19,9 +19,9 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
gem.version = Dekiru::VERSION
|
21
21
|
|
22
|
-
gem.required_ruby_version = '>= 3.
|
22
|
+
gem.required_ruby_version = '>= 3.1.0'
|
23
23
|
|
24
|
-
gem.add_dependency 'rails', '>=
|
24
|
+
gem.add_dependency 'rails', '>= 7.0'
|
25
25
|
gem.add_dependency 'ruby-progressbar'
|
26
26
|
gem.add_development_dependency 'rake'
|
27
27
|
gem.add_development_dependency 'rspec'
|
@@ -30,7 +30,7 @@ module Dekiru
|
|
30
30
|
else
|
31
31
|
raise NestedTransactionError if current_transaction_open?
|
32
32
|
|
33
|
-
@result =
|
33
|
+
@result = transaction_provider.within_transaction do
|
34
34
|
run(&block)
|
35
35
|
log "Finished execution: #{title}"
|
36
36
|
confirm?("\nAre you sure to commit?")
|
@@ -75,10 +75,6 @@ module Dekiru
|
|
75
75
|
|
76
76
|
private
|
77
77
|
|
78
|
-
def current_transaction_open?
|
79
|
-
ActiveRecord::Base.connection.current_transaction.open?
|
80
|
-
end
|
81
|
-
|
82
78
|
def log(message)
|
83
79
|
if @pb && !@pb.finished?
|
84
80
|
@pb.log(message)
|
@@ -148,5 +144,11 @@ module Dekiru
|
|
148
144
|
instance_eval(&block)
|
149
145
|
end
|
150
146
|
end
|
147
|
+
|
148
|
+
def transaction_provider
|
149
|
+
Dekiru.configuration.transaction_provider
|
150
|
+
end
|
151
|
+
|
152
|
+
delegate :current_transaction_open?, to: :transaction_provider
|
151
153
|
end
|
152
154
|
end
|
data/lib/dekiru/tasks/db.rake
CHANGED
@@ -2,13 +2,13 @@ namespace :db do
|
|
2
2
|
namespace :migrate do
|
3
3
|
desc 'Check migrate conflict'
|
4
4
|
task check_conflict: :environment do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
migration_context =
|
6
|
+
if ActiveRecord::Base.connection_pool.respond_to?(:migration_context)
|
7
|
+
ActiveRecord::Base.connection_pool.migration_context
|
8
|
+
else
|
9
|
+
ActiveRecord::Base.connection.migration_context
|
10
|
+
end
|
11
|
+
migrations_status = migration_context.current_version.zero? ? [] : migration_context.migrations_status
|
12
12
|
|
13
13
|
if migrations_status.map(&:third).any? { |name| name.include?('NO FILE') }
|
14
14
|
abort 'Migration conflict!'
|
data/lib/dekiru/version.rb
CHANGED
data/lib/dekiru.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'dekiru/version'
|
2
2
|
require 'dekiru/railtie' if defined?(::Rails)
|
3
3
|
require 'dekiru/helper'
|
4
|
-
require 'dekiru/validators/existence'
|
5
4
|
require 'dekiru/data_migration_operator'
|
6
5
|
require 'dekiru/mail_security_interceptor'
|
7
6
|
require 'dekiru/camelize_hash'
|
7
|
+
require 'dekiru/transaction_provider'
|
8
8
|
|
9
9
|
require 'active_support'
|
10
10
|
require 'active_support/all'
|
@@ -21,11 +21,12 @@ module Dekiru
|
|
21
21
|
end
|
22
22
|
|
23
23
|
class Configuration
|
24
|
-
attr_accessor :mail_security_hook, :maintenance_script_directory
|
24
|
+
attr_accessor :mail_security_hook, :maintenance_script_directory, :transaction_provider
|
25
25
|
|
26
26
|
def initialize
|
27
27
|
@mail_security_hook = false # default
|
28
28
|
@maintenance_script_directory = 'scripts'
|
29
|
+
@transaction_provider = TransactionProvider.new
|
29
30
|
end
|
30
31
|
end
|
31
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dekiru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akihiro Matsumura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '7.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '7.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ruby-progressbar
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -139,7 +139,7 @@ files:
|
|
139
139
|
- lib/dekiru/railtie.rb
|
140
140
|
- lib/dekiru/task_with_logger.rb
|
141
141
|
- lib/dekiru/tasks/db.rake
|
142
|
-
- lib/dekiru/
|
142
|
+
- lib/dekiru/transaction_provider.rb
|
143
143
|
- lib/dekiru/version.rb
|
144
144
|
- lib/generators/maintenance_script/USAGE
|
145
145
|
- lib/generators/maintenance_script/maintenance_script_generator.rb
|
@@ -147,7 +147,6 @@ files:
|
|
147
147
|
- spec/dekiru/camelize_hash_spec.rb
|
148
148
|
- spec/dekiru/data_migration_operator_spec.rb
|
149
149
|
- spec/dekiru/mail_security_interceptor_spec.rb
|
150
|
-
- spec/dekiru/validators/existence_spec.rb
|
151
150
|
- spec/spec_helper.rb
|
152
151
|
- spec/supports/action_mailer.rb
|
153
152
|
- spec/supports/mock_active_record.rb
|
@@ -165,7 +164,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
165
164
|
requirements:
|
166
165
|
- - ">="
|
167
166
|
- !ruby/object:Gem::Version
|
168
|
-
version: 3.
|
167
|
+
version: 3.1.0
|
169
168
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
169
|
requirements:
|
171
170
|
- - ">="
|
@@ -180,7 +179,6 @@ test_files:
|
|
180
179
|
- spec/dekiru/camelize_hash_spec.rb
|
181
180
|
- spec/dekiru/data_migration_operator_spec.rb
|
182
181
|
- spec/dekiru/mail_security_interceptor_spec.rb
|
183
|
-
- spec/dekiru/validators/existence_spec.rb
|
184
182
|
- spec/spec_helper.rb
|
185
183
|
- spec/supports/action_mailer.rb
|
186
184
|
- spec/supports/mock_active_record.rb
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# class Post < ActiveRecord::Base
|
2
|
-
# belongs_to :company
|
3
|
-
# belongs_to :user
|
4
|
-
# validates :user_id, presence: true, existence: { in: -> { company.users } }
|
5
|
-
# end
|
6
|
-
|
7
|
-
module ActiveModel
|
8
|
-
module Validations
|
9
|
-
class ExistenceValidator < EachValidator
|
10
|
-
def validate_each(record, attribute, value)
|
11
|
-
ActiveSupport::Deprecation.warn('`ExistenceValidator` is deprecated and will be removed in v0.5. Please use `inclusion: { in: xx }` instead.')
|
12
|
-
|
13
|
-
unless exists?(record, value)
|
14
|
-
record.errors.add(attribute, :existence, **options.except(:in).merge!(value: value))
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def exists?(record, value)
|
19
|
-
unless options[:in].respond_to?(:call)
|
20
|
-
raise ArgumentError, '`in` option should be proc'
|
21
|
-
end
|
22
|
-
collection = record.instance_exec(&options[:in])
|
23
|
-
collection.exists?(value)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ActiveModel::Validations::ExistenceValidator do
|
4
|
-
let(:user_class) do
|
5
|
-
Class.new do
|
6
|
-
def self.exists?(id)
|
7
|
-
%w[valid_id].member?(id)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
let(:model_class) do
|
12
|
-
_options = options
|
13
|
-
|
14
|
-
Struct.new(:user_id, :users) do
|
15
|
-
include ActiveModel::Validations
|
16
|
-
|
17
|
-
def self.name
|
18
|
-
'DummyModel'
|
19
|
-
end
|
20
|
-
|
21
|
-
validates :user_id, existence: _options
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe 'validate' do
|
26
|
-
subject(:valid?) do
|
27
|
-
model_class.new(user_id, user_class).valid?
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'with exists id' do
|
31
|
-
let(:user_id) { 'valid_id' }
|
32
|
-
let(:options) { { in: -> { users } } }
|
33
|
-
|
34
|
-
it { is_expected.to eq true }
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'with exists not id' do
|
38
|
-
let(:user_id) { 'invalid_id' }
|
39
|
-
let(:options) { { in: -> { users } } }
|
40
|
-
|
41
|
-
it { is_expected.to eq false }
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with invalid option' do
|
45
|
-
let(:user_id) { 'valid_id' }
|
46
|
-
let(:options) { { in: user_class } }
|
47
|
-
|
48
|
-
it { expect { valid? }.to raise_error(ArgumentError) }
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|