mv-core 1.0.1 → 2.0.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/README.md +67 -12
- data/lib/mv-core.rb +18 -100
- data/lib/mv/core/active_record/connection_adapters/abstract_adapter_decorator.rb +51 -0
- data/lib/mv/core/active_record/connection_adapters/table_decorator.rb +13 -0
- data/lib/mv/core/active_record/connection_adapters/table_definition_decorator.rb +21 -0
- data/lib/mv/core/active_record/migration/command_recorder_decorator.rb +13 -0
- data/lib/mv/core/active_record/migration_decorator.rb +14 -0
- data/lib/mv/core/active_record/schema_decorator.rb +16 -0
- data/lib/mv/core/active_record/schema_dumper_decorator.rb +29 -0
- data/lib/mv/core/constraint/base.rb +33 -0
- data/lib/mv/core/constraint/builder/base.rb +45 -0
- data/lib/mv/core/constraint/builder/factory.rb +42 -0
- data/lib/mv/core/constraint/builder/index.rb +50 -0
- data/lib/mv/core/constraint/builder/trigger.rb +13 -0
- data/lib/mv/core/constraint/description.rb +24 -0
- data/lib/mv/core/constraint/factory.rb +33 -0
- data/lib/mv/core/constraint/index.rb +13 -0
- data/lib/mv/core/constraint/trigger.rb +20 -0
- data/lib/mv/core/db/helpers/column_validators.rb +57 -0
- data/lib/mv/core/db/helpers/table_validators.rb +38 -0
- data/lib/mv/core/db/migration_validator.rb +26 -0
- data/lib/mv/core/error.rb +25 -0
- data/lib/mv/core/migration/base.rb +94 -0
- data/lib/mv/core/migration/operations/add_column.rb +27 -0
- data/lib/mv/core/migration/operations/change_column.rb +31 -0
- data/lib/mv/core/migration/operations/drop_table.rb +21 -0
- data/lib/mv/core/migration/operations/factory.rb +20 -0
- data/lib/mv/core/migration/operations/list.rb +28 -0
- data/lib/mv/core/migration/operations/remove_column.rb +22 -0
- data/lib/mv/core/migration/operations/rename_column.rb +26 -0
- data/lib/mv/core/migration/operations/rename_table.rb +25 -0
- data/lib/mv/core/presenter/constraint/description.rb +26 -0
- data/lib/mv/core/presenter/validation/base.rb +73 -0
- data/lib/mv/core/railtie.rb +40 -0
- data/lib/mv/core/route/base.rb +25 -0
- data/lib/mv/core/route/index.rb +17 -0
- data/lib/mv/core/route/trigger.rb +22 -0
- data/lib/mv/core/router.rb +33 -0
- data/lib/mv/core/services/compare_constraint_arrays.rb +50 -0
- data/lib/mv/core/services/compare_constraints.rb +31 -0
- data/lib/mv/core/services/create_constraints.rb +30 -0
- data/lib/mv/core/services/create_migration_validators_table.rb +31 -0
- data/lib/mv/core/services/delete_constraints.rb +30 -0
- data/lib/mv/core/services/load_constraints.rb +45 -0
- data/lib/mv/core/services/say_constraints_diff.rb +66 -0
- data/lib/mv/core/services/show_constraints.rb +41 -0
- data/lib/mv/core/services/synchronize_constraints.rb +54 -0
- data/lib/mv/core/services/uninstall.rb +25 -0
- data/lib/mv/core/validation/absence.rb +35 -0
- data/lib/mv/core/validation/base.rb +98 -0
- data/lib/mv/core/validation/builder/absence.rb +19 -0
- data/lib/mv/core/validation/builder/base.rb +58 -0
- data/lib/mv/core/validation/builder/exclusion.rb +42 -0
- data/lib/mv/core/validation/builder/factory.rb +43 -0
- data/lib/mv/core/validation/builder/inclusion.rb +42 -0
- data/lib/mv/core/validation/builder/length.rb +68 -0
- data/lib/mv/core/validation/builder/presence.rb +19 -0
- data/lib/mv/core/validation/builder/uniqueness.rb +19 -0
- data/lib/mv/core/validation/exclusion.rb +27 -0
- data/lib/mv/core/validation/factory.rb +56 -0
- data/lib/mv/core/validation/inclusion.rb +27 -0
- data/lib/mv/core/validation/length.rb +59 -0
- data/lib/mv/core/validation/presence.rb +25 -0
- data/lib/mv/core/validation/uniqueness.rb +45 -0
- data/lib/mv/core/validators/array_validator.rb +5 -0
- data/lib/mv/core/validators/integers_array_validator.rb +12 -0
- data/lib/mv/core/validators/valid_validator.rb +9 -0
- metadata +158 -30
- data/lib/migration_validators/active_record/base.rb +0 -29
- data/lib/migration_validators/active_record/connection_adapters/abstract_adapter.rb +0 -38
- data/lib/migration_validators/active_record/connection_adapters/native_adapter.rb +0 -129
- data/lib/migration_validators/active_record/connection_adapters/table.rb +0 -17
- data/lib/migration_validators/active_record/connection_adapters/table_definition.rb +0 -33
- data/lib/migration_validators/active_record/migration.rb +0 -25
- data/lib/migration_validators/active_record/schema.rb +0 -32
- data/lib/migration_validators/active_record/schema_dumper.rb +0 -25
- data/lib/migration_validators/adapters/base.rb +0 -15
- data/lib/migration_validators/adapters/containers.rb +0 -100
- data/lib/migration_validators/adapters/routing.rb +0 -102
- data/lib/migration_validators/adapters/syntax.rb +0 -51
- data/lib/migration_validators/adapters/validator_definitions.rb +0 -132
- data/lib/migration_validators/core/adapter_wrapper.rb +0 -88
- data/lib/migration_validators/core/db_validator.rb +0 -131
- data/lib/migration_validators/core/statement_builder.rb +0 -61
- data/lib/migration_validators/core/validator_constraints_list.rb +0 -32
- data/lib/migration_validators/core/validator_container.rb +0 -110
- data/lib/migration_validators/core/validator_definition.rb +0 -91
- data/lib/migration_validators/core/validator_router.rb +0 -45
- data/lib/options.rb +0 -7
@@ -0,0 +1,50 @@
|
|
1
|
+
module Mv
|
2
|
+
module Core
|
3
|
+
module Services
|
4
|
+
class CompareConstraintArrays
|
5
|
+
attr_reader :old_constraints, :new_constraints
|
6
|
+
|
7
|
+
def initialize(old_constraints, new_constraints)
|
8
|
+
@old_constraints = old_constraints
|
9
|
+
@new_constraints = new_constraints
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute
|
13
|
+
{
|
14
|
+
deleted: deleted_constraints,
|
15
|
+
updated: updated_constraints,
|
16
|
+
added: added_constraints
|
17
|
+
}.delete_if{ |key, value| value.blank? }
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def deleted_constraints
|
23
|
+
old_constraints.select do |old_constraint|
|
24
|
+
new_constraints.none? do |new_constraint|
|
25
|
+
old_constraint.description == new_constraint.description
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def updated_constraints
|
31
|
+
old_constraints.inject([]) do |res, old_constraint|
|
32
|
+
new_constraints.select do |new_constraint|
|
33
|
+
old_constraint.description == new_constraint.description &&
|
34
|
+
old_constraint.validations.sort != new_constraint.validations.sort
|
35
|
+
end.each{|new_constraint| res << [old_constraint, new_constraint]}
|
36
|
+
res
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def added_constraints
|
41
|
+
new_constraints.select do |new_constraint|
|
42
|
+
old_constraints.none? do |old_constraint|
|
43
|
+
old_constraint.description == new_constraint.description
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Mv
|
2
|
+
module Core
|
3
|
+
module Services
|
4
|
+
class CompareConstraints
|
5
|
+
attr_reader :old_constraint, :new_constraint
|
6
|
+
|
7
|
+
def initialize(old_constraint, new_constraint)
|
8
|
+
@old_constraint = old_constraint
|
9
|
+
@new_constraint = new_constraint
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute
|
13
|
+
{
|
14
|
+
deleted: old_validations - new_validations,
|
15
|
+
added: new_validations - old_validations
|
16
|
+
}.delete_if{ |key, value| value.blank? }
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def old_validations
|
22
|
+
@old_validations ||= old_constraint.try(:validations) || []
|
23
|
+
end
|
24
|
+
|
25
|
+
def new_validations
|
26
|
+
@new_validations ||= new_constraint.try(:validations) || []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'mv/core/services/load_constraints'
|
2
|
+
|
3
|
+
module Mv
|
4
|
+
module Core
|
5
|
+
module Services
|
6
|
+
class CreateConstraints
|
7
|
+
attr_reader :tables
|
8
|
+
|
9
|
+
def initialize(tables)
|
10
|
+
@tables = tables.present? ? tables : Mv::Core::Db::MigrationValidator.pluck(:table_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
constraints_comparizon = Mv::Core::Services::CompareConstraintArrays.new([], existing_constraints)
|
15
|
+
.execute
|
16
|
+
Mv::Core::Services::SynchronizeConstraints.new(constraints_comparizon[:added],
|
17
|
+
constraints_comparizon[:updated],
|
18
|
+
constraints_comparizon[:deleted])
|
19
|
+
.execute
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def existing_constraints
|
25
|
+
Mv::Core::Services::LoadConstraints.new(tables).execute
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Mv
|
2
|
+
module Core
|
3
|
+
module Services
|
4
|
+
class CreateMigrationValidatorsTable
|
5
|
+
attr_reader :db
|
6
|
+
delegate :create_table, :table_exists?, :add_index, to: :db
|
7
|
+
|
8
|
+
def initialize db = ::ActiveRecord::Base.connection
|
9
|
+
@db = db
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute
|
13
|
+
unless table_exists?(:migration_validators)
|
14
|
+
::ActiveRecord::Migration.say_with_time('initialize migration_validators table') do
|
15
|
+
create_table(:migration_validators) do |table|
|
16
|
+
table.string :table_name, null: false
|
17
|
+
table.string :column_name, null: false
|
18
|
+
table.string :validation_type, null: false
|
19
|
+
table.string :options
|
20
|
+
end
|
21
|
+
|
22
|
+
add_index(:migration_validators,
|
23
|
+
[:table_name, :column_name, :validation_type],
|
24
|
+
name: 'unique_idx_on_migration_validators')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'mv/core/services/load_constraints'
|
2
|
+
|
3
|
+
module Mv
|
4
|
+
module Core
|
5
|
+
module Services
|
6
|
+
class DeleteConstraints
|
7
|
+
attr_reader :tables
|
8
|
+
|
9
|
+
def initialize(tables = [])
|
10
|
+
@tables = tables.present? ? tables : Mv::Core::Db::MigrationValidator.pluck(:table_name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
constraints_comparizon = Mv::Core::Services::CompareConstraintArrays.new(existing_constraints, [])
|
15
|
+
.execute
|
16
|
+
Mv::Core::Services::SynchronizeConstraints.new(constraints_comparizon[:added],
|
17
|
+
constraints_comparizon[:updated],
|
18
|
+
constraints_comparizon[:deleted])
|
19
|
+
.execute
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def existing_constraints
|
25
|
+
Mv::Core::Services::LoadConstraints.new(tables).execute
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'mv/core/db/migration_validator'
|
2
|
+
require 'mv/core/router'
|
3
|
+
require 'mv/core/constraint/factory'
|
4
|
+
|
5
|
+
module Mv
|
6
|
+
module Core
|
7
|
+
module Services
|
8
|
+
class LoadConstraints
|
9
|
+
attr_reader :tables
|
10
|
+
|
11
|
+
def initialize(tables)
|
12
|
+
@tables = tables
|
13
|
+
end
|
14
|
+
|
15
|
+
def execute
|
16
|
+
res = []
|
17
|
+
|
18
|
+
Mv::Core::Db::MigrationValidator.where(table_name: tables).each do |migration_validator|
|
19
|
+
validation = migration_validator.validation
|
20
|
+
descriptions = Mv::Core::Router.route(validation)
|
21
|
+
|
22
|
+
descriptions.each do |description|
|
23
|
+
add_constraint(res, description).validations << validation
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
return res
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def add_constraint(constraints_list, description)
|
33
|
+
res = constraints_list.find{|constraint| constraint.description == description}
|
34
|
+
|
35
|
+
unless res
|
36
|
+
res = Mv::Core::Constraint::Factory.create_constraint(description)
|
37
|
+
constraints_list << res
|
38
|
+
end
|
39
|
+
|
40
|
+
res
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'mv/core/services/compare_constraints'
|
2
|
+
require 'mv/core/presenter/validation/base'
|
3
|
+
require 'mv/core/presenter/constraint/description'
|
4
|
+
|
5
|
+
module Mv
|
6
|
+
module Core
|
7
|
+
module Services
|
8
|
+
class SayConstraintsDiff
|
9
|
+
attr_reader :old_constraint, :new_constraint
|
10
|
+
|
11
|
+
def initialize(old_constraint, new_constraint)
|
12
|
+
@old_constraint = old_constraint
|
13
|
+
@new_constraint = new_constraint
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute
|
17
|
+
::ActiveRecord::Migration.say_with_time("#{constraint_operation} #{constraint_presenter} on table \"#{table_name}\"") do
|
18
|
+
comparison[:deleted].each do |validation|
|
19
|
+
say("delete #{validation_presenter(validation)}")
|
20
|
+
end if comparison[:deleted]
|
21
|
+
|
22
|
+
comparison[:added].each do |validation|
|
23
|
+
say("create #{validation_presenter(validation)}")
|
24
|
+
end if comparison[:added]
|
25
|
+
|
26
|
+
yield
|
27
|
+
end unless comparison.blank?
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def constraint_operation
|
33
|
+
return 'create' unless old_constraint
|
34
|
+
return 'delete' unless new_constraint
|
35
|
+
'update'
|
36
|
+
end
|
37
|
+
|
38
|
+
def table_name
|
39
|
+
old_constraint.try(:validations).try(:first).try(:table_name) ||
|
40
|
+
new_constraint.try(:validations).try(:first).try(:table_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def constraint_presenter
|
44
|
+
Mv::Core::Presenter::Constraint::Description.new((old_constraint || new_constraint).description)
|
45
|
+
end
|
46
|
+
|
47
|
+
def validation_presenter validation
|
48
|
+
Mv::Core::Presenter::Validation::Base.new(validation)
|
49
|
+
end
|
50
|
+
|
51
|
+
def comparison
|
52
|
+
@comparison ||= Mv::Core::Services::CompareConstraints.new(old_constraint, new_constraint)
|
53
|
+
.execute
|
54
|
+
end
|
55
|
+
|
56
|
+
def say_with_time msg, &block
|
57
|
+
::ActiveRecord::Migration.say_with_time(msg, &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def say msg
|
61
|
+
::ActiveRecord::Migration.say(msg, true)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'mv/core/services/load_constraints'
|
2
|
+
require 'mv/core/presenter/constraint/description'
|
3
|
+
require 'mv/core/presenter/validation/base'
|
4
|
+
|
5
|
+
module Mv
|
6
|
+
module Core
|
7
|
+
module Services
|
8
|
+
class ShowConstraints
|
9
|
+
attr_reader :tables
|
10
|
+
|
11
|
+
def initialize(tables)
|
12
|
+
@tables = tables.present? ? tables : Mv::Core::Db::MigrationValidator.pluck(:table_name)
|
13
|
+
end
|
14
|
+
|
15
|
+
def execute
|
16
|
+
existing_constraints.each do |constraint|
|
17
|
+
say_with_time("#{Mv::Core::Presenter::Constraint::Description.new(constraint.description)}") do
|
18
|
+
constraint.validations.each do |validation|
|
19
|
+
say("#{Mv::Core::Presenter::Validation::Base.new(validation)}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def say_with_time msg, &block
|
28
|
+
::ActiveRecord::Migration::say_with_time(msg, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def say msg
|
32
|
+
::ActiveRecord::Migration::say(msg, true)
|
33
|
+
end
|
34
|
+
|
35
|
+
def existing_constraints
|
36
|
+
Mv::Core::Services::LoadConstraints.new(tables).execute
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'mv/core/constraint/builder/factory'
|
2
|
+
require 'mv/core/services/say_constraints_diff'
|
3
|
+
|
4
|
+
module Mv
|
5
|
+
module Core
|
6
|
+
module Services
|
7
|
+
class SynchronizeConstraints
|
8
|
+
attr_reader :additions, :updates, :deletions
|
9
|
+
|
10
|
+
def initialize(additions, updates, deletions)
|
11
|
+
@additions = additions
|
12
|
+
@updates = updates
|
13
|
+
@deletions = deletions
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute
|
17
|
+
delete
|
18
|
+
update
|
19
|
+
create
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def builder(constraint)
|
25
|
+
Mv::Core::Constraint::Builder::Factory.create_builder(constraint)
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete
|
29
|
+
deletions.each do |constraint_to_delete|
|
30
|
+
Mv::Core::Services::SayConstraintsDiff.new(constraint_to_delete, nil).execute do
|
31
|
+
builder(constraint_to_delete).delete
|
32
|
+
end
|
33
|
+
end if deletions
|
34
|
+
end
|
35
|
+
|
36
|
+
def update
|
37
|
+
updates.each do |old_constraint, new_constraint|
|
38
|
+
Mv::Core::Services::SayConstraintsDiff.new(old_constraint, new_constraint).execute do
|
39
|
+
builder(old_constraint).update(builder(new_constraint))
|
40
|
+
end
|
41
|
+
end if updates
|
42
|
+
end
|
43
|
+
|
44
|
+
def create
|
45
|
+
additions.each do |constraint_to_be_added|
|
46
|
+
Mv::Core::Services::SayConstraintsDiff.new(nil, constraint_to_be_added).execute do
|
47
|
+
builder(constraint_to_be_added).create
|
48
|
+
end
|
49
|
+
end if additions
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'mv/core/services/delete_constraints'
|
2
|
+
|
3
|
+
module Mv
|
4
|
+
module Core
|
5
|
+
module Services
|
6
|
+
class Uninstall
|
7
|
+
def execute
|
8
|
+
if db.table_exists?(:migration_validators)
|
9
|
+
Mv::Core::Services::DeleteConstraints.new.execute
|
10
|
+
|
11
|
+
::ActiveRecord::Migration.say_with_time('drop migration_validators table') do
|
12
|
+
db.drop_table(:migration_validators)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def db
|
20
|
+
::ActiveRecord::Base.connection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'mv/core/validation/base'
|
2
|
+
|
3
|
+
module Mv
|
4
|
+
module Core
|
5
|
+
module Validation
|
6
|
+
class Absence < Base
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
9
|
+
validate :nil_and_blank_can_not_be_both_denied
|
10
|
+
|
11
|
+
def initialize(table_name, column_name, opts)
|
12
|
+
super(table_name, column_name, opts)
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def default_allow_nil
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
def default_allow_blank
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def nil_and_blank_can_not_be_both_denied
|
28
|
+
if !(allow_blank || allow_nil)
|
29
|
+
errors.add(:allow_blank, 'can not be denied when nil is denied')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|