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.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +67 -12
  3. data/lib/mv-core.rb +18 -100
  4. data/lib/mv/core/active_record/connection_adapters/abstract_adapter_decorator.rb +51 -0
  5. data/lib/mv/core/active_record/connection_adapters/table_decorator.rb +13 -0
  6. data/lib/mv/core/active_record/connection_adapters/table_definition_decorator.rb +21 -0
  7. data/lib/mv/core/active_record/migration/command_recorder_decorator.rb +13 -0
  8. data/lib/mv/core/active_record/migration_decorator.rb +14 -0
  9. data/lib/mv/core/active_record/schema_decorator.rb +16 -0
  10. data/lib/mv/core/active_record/schema_dumper_decorator.rb +29 -0
  11. data/lib/mv/core/constraint/base.rb +33 -0
  12. data/lib/mv/core/constraint/builder/base.rb +45 -0
  13. data/lib/mv/core/constraint/builder/factory.rb +42 -0
  14. data/lib/mv/core/constraint/builder/index.rb +50 -0
  15. data/lib/mv/core/constraint/builder/trigger.rb +13 -0
  16. data/lib/mv/core/constraint/description.rb +24 -0
  17. data/lib/mv/core/constraint/factory.rb +33 -0
  18. data/lib/mv/core/constraint/index.rb +13 -0
  19. data/lib/mv/core/constraint/trigger.rb +20 -0
  20. data/lib/mv/core/db/helpers/column_validators.rb +57 -0
  21. data/lib/mv/core/db/helpers/table_validators.rb +38 -0
  22. data/lib/mv/core/db/migration_validator.rb +26 -0
  23. data/lib/mv/core/error.rb +25 -0
  24. data/lib/mv/core/migration/base.rb +94 -0
  25. data/lib/mv/core/migration/operations/add_column.rb +27 -0
  26. data/lib/mv/core/migration/operations/change_column.rb +31 -0
  27. data/lib/mv/core/migration/operations/drop_table.rb +21 -0
  28. data/lib/mv/core/migration/operations/factory.rb +20 -0
  29. data/lib/mv/core/migration/operations/list.rb +28 -0
  30. data/lib/mv/core/migration/operations/remove_column.rb +22 -0
  31. data/lib/mv/core/migration/operations/rename_column.rb +26 -0
  32. data/lib/mv/core/migration/operations/rename_table.rb +25 -0
  33. data/lib/mv/core/presenter/constraint/description.rb +26 -0
  34. data/lib/mv/core/presenter/validation/base.rb +73 -0
  35. data/lib/mv/core/railtie.rb +40 -0
  36. data/lib/mv/core/route/base.rb +25 -0
  37. data/lib/mv/core/route/index.rb +17 -0
  38. data/lib/mv/core/route/trigger.rb +22 -0
  39. data/lib/mv/core/router.rb +33 -0
  40. data/lib/mv/core/services/compare_constraint_arrays.rb +50 -0
  41. data/lib/mv/core/services/compare_constraints.rb +31 -0
  42. data/lib/mv/core/services/create_constraints.rb +30 -0
  43. data/lib/mv/core/services/create_migration_validators_table.rb +31 -0
  44. data/lib/mv/core/services/delete_constraints.rb +30 -0
  45. data/lib/mv/core/services/load_constraints.rb +45 -0
  46. data/lib/mv/core/services/say_constraints_diff.rb +66 -0
  47. data/lib/mv/core/services/show_constraints.rb +41 -0
  48. data/lib/mv/core/services/synchronize_constraints.rb +54 -0
  49. data/lib/mv/core/services/uninstall.rb +25 -0
  50. data/lib/mv/core/validation/absence.rb +35 -0
  51. data/lib/mv/core/validation/base.rb +98 -0
  52. data/lib/mv/core/validation/builder/absence.rb +19 -0
  53. data/lib/mv/core/validation/builder/base.rb +58 -0
  54. data/lib/mv/core/validation/builder/exclusion.rb +42 -0
  55. data/lib/mv/core/validation/builder/factory.rb +43 -0
  56. data/lib/mv/core/validation/builder/inclusion.rb +42 -0
  57. data/lib/mv/core/validation/builder/length.rb +68 -0
  58. data/lib/mv/core/validation/builder/presence.rb +19 -0
  59. data/lib/mv/core/validation/builder/uniqueness.rb +19 -0
  60. data/lib/mv/core/validation/exclusion.rb +27 -0
  61. data/lib/mv/core/validation/factory.rb +56 -0
  62. data/lib/mv/core/validation/inclusion.rb +27 -0
  63. data/lib/mv/core/validation/length.rb +59 -0
  64. data/lib/mv/core/validation/presence.rb +25 -0
  65. data/lib/mv/core/validation/uniqueness.rb +45 -0
  66. data/lib/mv/core/validators/array_validator.rb +5 -0
  67. data/lib/mv/core/validators/integers_array_validator.rb +12 -0
  68. data/lib/mv/core/validators/valid_validator.rb +9 -0
  69. metadata +158 -30
  70. data/lib/migration_validators/active_record/base.rb +0 -29
  71. data/lib/migration_validators/active_record/connection_adapters/abstract_adapter.rb +0 -38
  72. data/lib/migration_validators/active_record/connection_adapters/native_adapter.rb +0 -129
  73. data/lib/migration_validators/active_record/connection_adapters/table.rb +0 -17
  74. data/lib/migration_validators/active_record/connection_adapters/table_definition.rb +0 -33
  75. data/lib/migration_validators/active_record/migration.rb +0 -25
  76. data/lib/migration_validators/active_record/schema.rb +0 -32
  77. data/lib/migration_validators/active_record/schema_dumper.rb +0 -25
  78. data/lib/migration_validators/adapters/base.rb +0 -15
  79. data/lib/migration_validators/adapters/containers.rb +0 -100
  80. data/lib/migration_validators/adapters/routing.rb +0 -102
  81. data/lib/migration_validators/adapters/syntax.rb +0 -51
  82. data/lib/migration_validators/adapters/validator_definitions.rb +0 -132
  83. data/lib/migration_validators/core/adapter_wrapper.rb +0 -88
  84. data/lib/migration_validators/core/db_validator.rb +0 -131
  85. data/lib/migration_validators/core/statement_builder.rb +0 -61
  86. data/lib/migration_validators/core/validator_constraints_list.rb +0 -32
  87. data/lib/migration_validators/core/validator_container.rb +0 -110
  88. data/lib/migration_validators/core/validator_definition.rb +0 -91
  89. data/lib/migration_validators/core/validator_router.rb +0 -45
  90. data/lib/options.rb +0 -7
@@ -0,0 +1,45 @@
1
+ require 'mv/core/validation/builder/factory'
2
+
3
+ module Mv
4
+ module Core
5
+ module Constraint
6
+ module Builder
7
+ class Base
8
+ attr_reader :constraint
9
+
10
+ delegate :name, to: :constraint
11
+
12
+ def initialize(constraint)
13
+ @constraint = constraint
14
+ end
15
+
16
+ def create
17
+ end
18
+
19
+ def delete
20
+ end
21
+
22
+ def update new_constraint_builder
23
+ end
24
+
25
+ def self.validation_builders_factory
26
+ @validation_builders_factory ||= Mv::Core::Validation::Builder::Factory.new
27
+ end
28
+
29
+ def validation_builders
30
+ @validation_builders ||= constraint.validations.collect do |validation|
31
+ self.class.validation_builders_factory.create_builder(validation)
32
+ end
33
+ end
34
+
35
+ protected
36
+
37
+
38
+ def db
39
+ ::ActiveRecord::Base.connection
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,42 @@
1
+ require 'mv/core/constraint/builder/index'
2
+ require 'mv/core/constraint/builder/trigger'
3
+
4
+ module Mv
5
+ module Core
6
+ module Constraint
7
+ module Builder
8
+ class Factory
9
+ include Singleton
10
+
11
+ def create_builder constraint
12
+ factory_map[constraint.class].new(constraint)
13
+ end
14
+
15
+ def register_builder constraint_class, builder_class
16
+ factory_map[constraint_class] = builder_class
17
+ end
18
+
19
+ def register_builders opts
20
+ opts.each do |constraint_class, builder_class|
21
+ register_builder(constraint_class, builder_class)
22
+ end
23
+ end
24
+
25
+ class << self
26
+ delegate :create_builder, :register_builder, :register_builders, to: :instance
27
+ end
28
+
29
+
30
+ private
31
+
32
+ def factory_map
33
+ @factory_map ||= {
34
+ Mv::Core::Constraint::Index => Mv::Core::Constraint::Builder::Index,
35
+ Mv::Core::Constraint::Trigger => Mv::Core::Constraint::Builder::Trigger
36
+ }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,50 @@
1
+ require 'mv/core/constraint/builder/base'
2
+
3
+ module Mv
4
+ module Core
5
+ module Constraint
6
+ module Builder
7
+ class Index < Base
8
+ def create
9
+ super
10
+
11
+ constraint.validations.group_by(&:table_name).each do |table_name, validations|
12
+ remove_index(table_name)
13
+ add_index(table_name, validations.collect(&:column_name))
14
+ end
15
+ end
16
+
17
+ def delete
18
+ super
19
+
20
+ constraint.validations.group_by(&:table_name).each do |table_name, validations|
21
+ remove_index(table_name)
22
+ end
23
+ end
24
+
25
+ def update new_constraint_builder
26
+ super
27
+
28
+ delete
29
+ new_constraint_builder.create
30
+ end
31
+
32
+ private
33
+
34
+ def index_exists?(table_name)
35
+ db.table_exists?(table_name) &&
36
+ db.index_name_exists?(table_name, name, false)
37
+ end
38
+
39
+ def remove_index(table_name)
40
+ db.remove_index!(table_name, name) if index_exists?(table_name)
41
+ end
42
+
43
+ def add_index(table_name, columns)
44
+ db.add_index(table_name, columns, name: name, unique: true)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ require 'mv/core/constraint/builder/base'
2
+
3
+ module Mv
4
+ module Core
5
+ module Constraint
6
+ module Builder
7
+ class Trigger < Base
8
+ delegate :event, :update?, to: :constraint
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module Mv
2
+ module Core
3
+ module Constraint
4
+ class Description
5
+ include Comparable
6
+
7
+ attr_reader :name, :type, :options
8
+
9
+ def initialize name, type, options = {}
10
+ @name = name.to_sym
11
+ @type = type.to_sym
12
+ @options = options.inject({}) do |res, (name, value)|
13
+ res[name.to_sym] = value.to_s
14
+ res
15
+ end
16
+ end
17
+
18
+ def <=> other_info
19
+ [name, type, options] <=> [other_info.name, other_info.type, other_info.options]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'mv/core/constraint/trigger'
2
+ require 'mv/core/constraint/index'
3
+
4
+ module Mv
5
+ module Core
6
+ module Constraint
7
+ class Factory
8
+ include Singleton
9
+
10
+ def create_constraint description
11
+ factory_map[description.type.to_sym].new(description)
12
+ end
13
+
14
+ def register_constraint type, klass
15
+ factory_map[type.to_sym] = klass
16
+ end
17
+
18
+ class << self
19
+ delegate :create_constraint, :register_constraint, to: :instance
20
+ end
21
+
22
+ private
23
+
24
+ def factory_map
25
+ @factory_map ||= {
26
+ trigger: Mv::Core::Constraint::Trigger,
27
+ index: Mv::Core::Constraint::Index
28
+ }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ require 'mv/core/constraint/base'
2
+
3
+ module Mv
4
+ module Core
5
+ module Constraint
6
+ class Index < Base
7
+ def initialize description
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ require 'mv/core/constraint/base'
2
+
3
+ module Mv
4
+ module Core
5
+ module Constraint
6
+ class Trigger < Base
7
+ attr_reader :event
8
+
9
+ def initialize description
10
+ super
11
+ @event = @description.options[:event].try(:to_sym)
12
+ end
13
+
14
+ def update?
15
+ event == :update
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,57 @@
1
+ require 'mv/core/db/helpers/table_validators'
2
+
3
+ module Mv
4
+ module Core
5
+ module Db
6
+ module Helpers
7
+ module ColumnValidators
8
+ include TableValidators
9
+
10
+ attr_accessor :column_name
11
+
12
+ def column_validators
13
+ table_validators.where(column_name: column_name)
14
+ end
15
+
16
+ def create_column_validator validation_type, opts
17
+ raise_column_validation_error(validation_type, opts) if opts == false
18
+
19
+ update_column_validator(validation_type, opts)
20
+ end
21
+
22
+ def delete_column_validator
23
+ delete_validators(column_validators) > 0
24
+ end
25
+
26
+ def update_column_validator validation_type, opts
27
+ return delete_validators(column_validators.where(validation_type: validation_type)) if opts == false
28
+
29
+ column_validators.where(validation_type: validation_type).first_or_initialize.tap do |validator|
30
+ validator.options = normalize_opts(opts)
31
+ end.save!
32
+ end
33
+
34
+ def rename_column new_column_name
35
+ column_validators.update_all(column_name: new_column_name)
36
+ end
37
+
38
+ private
39
+
40
+ def normalize_opts opts
41
+ opts == true ? {} : opts
42
+ end
43
+
44
+ def raise_column_validation_error validation_type, opts
45
+ raise Mv::Core::Error.new(
46
+ table_name: table_name,
47
+ column_name: column_name,
48
+ validation_type: validation_type,
49
+ options: opts,
50
+ error: 'Validator can not be removed when new column is being added'
51
+ )
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,38 @@
1
+ require 'mv/core/db/migration_validator'
2
+
3
+ module Mv
4
+ module Core
5
+ module Db
6
+ module Helpers
7
+ module TableValidators
8
+ attr_accessor :table_name
9
+
10
+ def table_validators
11
+ Mv::Core::Db::MigrationValidator.where(table_name: table_name)
12
+ end
13
+
14
+ def delete_table_validators
15
+ delete_validators(table_validators)
16
+ end
17
+
18
+ def update_table_validators new_table_name
19
+ table_validators.update_all(table_name: new_table_name)
20
+ end
21
+
22
+ private
23
+
24
+ def say(message)
25
+ ::ActiveRecord::Migration.say(message, true)
26
+ end
27
+
28
+ def delete_validators validators
29
+ validators.each do |validator|
30
+ validator.destroy
31
+ end
32
+ validators.length
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ require 'mv/core/validation/factory'
2
+ require 'mv/core/constraint/description'
3
+ require 'mv/core/validators/valid_validator'
4
+
5
+ module Mv
6
+ module Core
7
+ module Db
8
+ class MigrationValidator < ::ActiveRecord::Base
9
+ serialize :options, Hash
10
+
11
+ validates :table_name, presence: true
12
+ validates :column_name, presence: true
13
+ validates :validation_type, presence: true
14
+
15
+ validates :validation, valid: true
16
+
17
+ def validation
18
+ Mv::Core::Validation::Factory.create_validation(table_name,
19
+ column_name,
20
+ validation_type,
21
+ options)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module Mv
2
+ module Core
3
+ class Error < StandardError
4
+ attr_reader :table_name, :column_name, :validation_type, :options, :error
5
+
6
+ def initialize(opts = {})
7
+ opts = opts.with_indifferent_access
8
+
9
+ @table_name = opts[:table_name]
10
+ @column_name = opts[:column_name]
11
+ @validation_type = opts[:validation_type]
12
+ @options = opts[:options]
13
+ @error = opts[:error]
14
+
15
+ super [
16
+ @table_name ? "table: '#{@table_name}'" : nil,
17
+ @column_name ? "column: '#{@column_name}'" : nil,
18
+ @validation_type ? "validation: '#{@validation_type}'" : nil,
19
+ @options ? "options: '#{@options}'" : nil,
20
+ @error ? "error: '#{@error}'" : nil,
21
+ ].compact.join(', ')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,94 @@
1
+ require 'mv/core/db/migration_validator'
2
+ require 'mv/core/migration/operations/list'
3
+ require 'mv/core/migration/operations/factory'
4
+ require 'mv/core/services/load_constraints'
5
+ require 'mv/core/services/compare_constraint_arrays'
6
+ require 'mv/core/services/synchronize_constraints'
7
+ require 'mv/core/services/create_migration_validators_table.rb'
8
+
9
+ module Mv
10
+ module Core
11
+ module Migration
12
+ class Base
13
+ include Singleton
14
+
15
+ SUPPORTED_METHODS = %i{ add_column remove_column rename_column
16
+ change_column rename_table drop_table}
17
+
18
+ attr_reader :operations_list, :operations_factory
19
+
20
+ def initialize()
21
+ @operations_list = Mv::Core::Migration::Operations::List.new
22
+ @operations_factory = Mv::Core::Migration::Operations::Factory.new()
23
+ end
24
+
25
+ SUPPORTED_METHODS.each do |operation_name|
26
+ define_method operation_name do |*args|
27
+
28
+ unless disabled_validations?
29
+ operation = operations_factory.create_operation(operation_name, *args)
30
+ operations_list.add_operation(operation)
31
+ end
32
+ end
33
+ end
34
+
35
+ def execute
36
+ if operations_list.present?
37
+ Mv::Core::Services::CreateMigrationValidatorsTable.new.execute
38
+
39
+ constraints_loader = Mv::Core::Services::LoadConstraints.new(operations_list.tables)
40
+
41
+ old_constraints = constraints_loader.execute
42
+
43
+ operations_list.execute()
44
+
45
+ new_constraints = constraints_loader.execute
46
+
47
+ constraints_comparizon = Mv::Core::Services::CompareConstraintArrays.new(old_constraints, new_constraints)
48
+ .execute
49
+
50
+ Mv::Core::Services::SynchronizeConstraints.new(constraints_comparizon[:added],
51
+ constraints_comparizon[:updated],
52
+ constraints_comparizon[:deleted])
53
+ .execute
54
+ end
55
+ end
56
+
57
+ def add_column table_name, column_name, opts
58
+ return unless opts.present?
59
+
60
+ unless disabled_validations?
61
+ operation = operations_factory.create_operation(:add_column, table_name, column_name, opts)
62
+ operations_list.add_operation(operation)
63
+ end
64
+ end
65
+
66
+ def with_suppressed_validations
67
+ disable_validations!
68
+ yield
69
+ enable_validations!
70
+ end
71
+
72
+ class << self
73
+ alias_method :current, :instance
74
+
75
+ delegate *[SUPPORTED_METHODS, :execute, :with_suppressed_validations].flatten, to: :current, allow_nil: true
76
+ end
77
+
78
+ private
79
+
80
+ def disabled_validations?
81
+ !!@disabled_validations
82
+ end
83
+
84
+ def disable_validations!
85
+ @disabled_validations = true
86
+ end
87
+
88
+ def enable_validations!
89
+ @disabled_validations = false
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end