mv-core 0.1.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 (38) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +88 -0
  3. data/lib/migration_validators/active_record/base.rb +24 -0
  4. data/lib/migration_validators/active_record/connection_adapters/abstract_adapter.rb +36 -0
  5. data/lib/migration_validators/active_record/connection_adapters/native_adapter.rb +118 -0
  6. data/lib/migration_validators/active_record/connection_adapters/table.rb +15 -0
  7. data/lib/migration_validators/active_record/connection_adapters/table_definition.rb +24 -0
  8. data/lib/migration_validators/active_record/migration.rb +29 -0
  9. data/lib/migration_validators/active_record/schema.rb +31 -0
  10. data/lib/migration_validators/active_record/schema_dumper.rb +24 -0
  11. data/lib/migration_validators/adapters/base.rb +15 -0
  12. data/lib/migration_validators/adapters/containers.rb +102 -0
  13. data/lib/migration_validators/adapters/routing.rb +104 -0
  14. data/lib/migration_validators/adapters/syntax.rb +53 -0
  15. data/lib/migration_validators/adapters/validator_definitions.rb +131 -0
  16. data/lib/migration_validators/core/adapter_wrapper.rb +88 -0
  17. data/lib/migration_validators/core/db_validator.rb +178 -0
  18. data/lib/migration_validators/core/statement_builder.rb +60 -0
  19. data/lib/migration_validators/core/validator_container.rb +110 -0
  20. data/lib/migration_validators/core/validator_definition.rb +91 -0
  21. data/lib/migration_validators/core/validator_router.rb +45 -0
  22. data/lib/mv-core.rb +100 -0
  23. data/lib/options.rb +7 -0
  24. data/spec/migration_validators/active_record/connection_adapters/abstract_adapter_spec.rb +440 -0
  25. data/spec/migration_validators/active_record/connection_adapters/table_definition_spec.rb +4 -0
  26. data/spec/migration_validators/active_record/migration.rb +82 -0
  27. data/spec/migration_validators/active_record/schema_dumper_spec.rb +44 -0
  28. data/spec/migration_validators/adapters/base_spec.rb +89 -0
  29. data/spec/migration_validators/core/adapter_wrapper_spec.rb +168 -0
  30. data/spec/migration_validators/core/db_validator_spec.rb +347 -0
  31. data/spec/migration_validators/core/statement_builder_spec.rb +36 -0
  32. data/spec/migration_validators/core/validator_container_spec.rb +121 -0
  33. data/spec/migration_validators/core/validator_definition_spec.rb +131 -0
  34. data/spec/migration_validators/core/validator_router_spec.rb +60 -0
  35. data/spec/mv-core_spec.rb +4 -0
  36. data/spec/spec_helper.rb +15 -0
  37. data/spec/support/factories/db_validator.rb +43 -0
  38. metadata +152 -0
@@ -0,0 +1,60 @@
1
+ module MigrationValidators
2
+ module Core
3
+ class StatementBuilder
4
+ attr_reader :actions
5
+
6
+ def initialize value = "", builder = nil
7
+ @stmt = value
8
+ @actions = builder ? builder.actions.clone : {}
9
+ end
10
+
11
+ def to_s
12
+ @stmt
13
+ end
14
+
15
+ def operation name, &block
16
+ @actions[name.to_s] = block || lambda{|stmt| stmt}
17
+ end
18
+
19
+ def compile value
20
+ StatementBuilder.new value, self
21
+ end
22
+
23
+ def merge! builder
24
+ @actions.merge!(builder.actions) if builder
25
+ end
26
+
27
+ alias_method :old_method_missing, :method_missing
28
+ def method_missing method_name, *args
29
+ call_action(method_name, *args) || old_method_missing(method_name, *args)
30
+ end
31
+
32
+ protected
33
+
34
+
35
+ attr_accessor :stmt
36
+
37
+ def clear!
38
+ @stmt = ""
39
+ end
40
+
41
+ def change *args, &block
42
+ @stmt = instance_exec(*args, &block).to_s
43
+ end
44
+
45
+ private
46
+
47
+ def call_action action_name, *args
48
+ block = @actions[action_name.to_s]
49
+
50
+ if (block)
51
+ change(@stmt, *args, &block)
52
+ return self
53
+ end
54
+
55
+ return nil
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,110 @@
1
+ module MigrationValidators
2
+ module Core
3
+ class ValidatorContainer < StatementBuilder
4
+ attr_reader :name
5
+
6
+ def initialize name, definitions, builder = nil
7
+ super "", builder
8
+
9
+ @name = name
10
+ @definitions = definitions
11
+
12
+ group do |validator|
13
+ validator.name
14
+ end
15
+
16
+ constraint_name do |group_key|
17
+ group_key.to_s
18
+ end
19
+
20
+ operation :create do |stmt, group_name|
21
+ stmt
22
+ end
23
+
24
+ operation :drop do
25
+ ""
26
+ end
27
+
28
+ operation :join do |stmt, stmt_1|
29
+ [stmt, stmt_1].delete_if(&:blank?).join(" JOIN ")
30
+ end
31
+ end
32
+
33
+ def group &block
34
+ @group_proc = block
35
+ end
36
+
37
+ def constraint_name &block
38
+ @constraint_name_proc = block
39
+ end
40
+
41
+ def add_validators validators
42
+ res, constraint_name = process_validators(validators) do |existing_validators|
43
+ validators + existing_validators
44
+ end
45
+
46
+ validators.each {|validator| validator.save_to_constraint(constraint_name) }
47
+
48
+ res
49
+ end
50
+
51
+ def remove_validators validators
52
+ res, constraint_name = process_validators(validators) do |existing_validators|
53
+ existing_validators - validators
54
+ end
55
+
56
+ validators.each {|validator| validator.remove_from_constraint(constraint_name) }
57
+
58
+ res
59
+ end
60
+
61
+ private
62
+
63
+ def process_validators validators
64
+ validators.group_by(&@group_proc).inject([]) do |res, (group_name, group)|
65
+ constraint_name = @constraint_name_proc.call(group_name)
66
+ constraint_validators = MigrationValidators::Core::DbValidator.constraint_validators(constraint_name)
67
+
68
+ group = yield(constraint_validators).uniq
69
+
70
+ stmt = drop_group(constraint_name, group_name)
71
+ res << stmt unless stmt.blank?
72
+
73
+ unless group.blank?
74
+ stmt = create_group(constraint_name, group_name, group)
75
+ res << stmt unless stmt.blank?
76
+ end
77
+ [res, constraint_name]
78
+ end
79
+ end
80
+
81
+ def isolate
82
+ clear!
83
+ yield if block_given?
84
+ res = self.to_s
85
+ clear!
86
+ res
87
+ end
88
+
89
+ def drop_group constraint_name, group_name
90
+ isolate { drop(constraint_name, group_name) }
91
+ end
92
+
93
+ def create_group constraint_name, group_name, group
94
+ isolate do
95
+ group.each do |validator|
96
+ definition = @definitions[validator.validator_name.to_s] || @definitions[validator.validator_name.to_sym]
97
+
98
+ raise MigrationValidators::MigrationValidatorsException.new("Validator defintion for #{validator.validator_name} is not defined.") unless definition
99
+
100
+ definition.clone(self).process(validator).each do |statement|
101
+ join(statement)
102
+ end
103
+ end
104
+
105
+ create(constraint_name, group_name) unless group.blank?
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,91 @@
1
+ module MigrationValidators
2
+ module Core
3
+ class ValidatorDefinition < StatementBuilder
4
+ attr_reader :validator
5
+
6
+ def initialize definition = nil, validator = nil, properties = {}, posts = []
7
+ super "", definition
8
+
9
+ @properties = properties
10
+ @posts = posts
11
+
12
+ self.validator = validator if validator
13
+
14
+ operation :bind_to_error do |stmt, error|
15
+ stmt
16
+ end
17
+ end
18
+
19
+ def validator= validator
20
+ @validator = validator
21
+ @stmt = validator.column_name
22
+ end
23
+
24
+ def column
25
+ clone
26
+ end
27
+
28
+ def clone builder = nil
29
+ res = ValidatorDefinition.new self, validator, @properties.clone, @posts.clone
30
+ res.merge!(builder) if builder
31
+ res
32
+ end
33
+
34
+ def property name = "", opts = {}, &block
35
+ @properties[name.to_s] ||= [opts, block]
36
+ end
37
+
38
+ def post opts = {}, &block
39
+ @posts << [opts, block]
40
+ end
41
+
42
+ def process validator, filter = []
43
+ self.validator = validator
44
+
45
+ return [] if validator.options.nil?
46
+
47
+ unless filter.blank?
48
+ filter = filter.collect(&:to_s)
49
+ options = validator.options.select{|name, value| filter.include?(name.to_s) }
50
+ else
51
+ options = validator.options
52
+ end
53
+
54
+ res = options.inject([]) do |res, (property_name, property_value)|
55
+ res << self.to_s if handle_property(property_name, property_value)
56
+ res
57
+ end
58
+
59
+ return res unless res.blank?
60
+ return (handle_property("", options) && res << self.to_s) || []
61
+ end
62
+
63
+ private
64
+
65
+ def handle_property property_name, property_value
66
+ opts, block = @properties[property_name.to_s]
67
+
68
+ if (block)
69
+ at_least_one_property_handled = true
70
+ change(property_value, &block)
71
+ apply_posts
72
+ bind_to_error(message(opts))
73
+
74
+ return true
75
+ end
76
+
77
+ false
78
+ end
79
+
80
+ def apply_posts
81
+ @posts.each{|opts, post_block| change(&post_block) if validator.satisfies(opts)}
82
+ end
83
+
84
+ def message opts
85
+ validator.options[opts[:message]] ||
86
+ validator.options[:message] ||
87
+ validator.error_message
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,45 @@
1
+ module MigrationValidators
2
+ module Core
3
+ class ValidatorRouter
4
+ def initialize containers
5
+ @containers = containers
6
+ @routes = {}
7
+ end
8
+
9
+ def to container_name, conditions = {}
10
+ raise "Container name is undefined" if container_name.blank?
11
+
12
+ @routes[container_name] ||= conditions[:if]
13
+ end
14
+
15
+ def add_validators validators
16
+ process(validators) do |container, filtered_validators|
17
+ container.add_validators(filtered_validators)
18
+ end
19
+ end
20
+
21
+ def remove_validators validators
22
+ process(validators) do |container, filtered_validators|
23
+ container.remove_validators(filtered_validators)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def process validators
30
+ @routes.inject([]) do |res, (container_name, conditions)|
31
+ filtered_validators = validators.select {|validator| validator.satisfies(conditions)}
32
+
33
+ unless filtered_validators.blank?
34
+ container = @containers[container_name]
35
+ raise MigrationValidators::MigrationValidatorsException.new("Routing error. Contianer #{container_name} is not defined.") if container.nil?
36
+
37
+ res.concat(yield(container, filtered_validators))
38
+ end
39
+
40
+ res
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,100 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+
4
+ require File.expand_path(File.dirname(__FILE__)) + '/options'
5
+
6
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/connection_adapters/table'
7
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/connection_adapters/abstract_adapter'
8
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/connection_adapters/native_adapter'
9
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/connection_adapters/table_definition'
10
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/base'
11
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/migration'
12
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/schema'
13
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/active_record/schema_dumper'
14
+
15
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/core/db_validator'
16
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/core/adapter_wrapper'
17
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/core/statement_builder'
18
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/core/validator_definition'
19
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/core/validator_container'
20
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/core/validator_router'
21
+
22
+ require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/adapters/base'
23
+
24
+ module MigrationValidators
25
+ class MigrationValidatorsException < Exception; end
26
+ class << self
27
+ def validators
28
+ @validators ||= {}
29
+ end
30
+
31
+ def dumpers
32
+ @dumpers ||= {}
33
+ end
34
+
35
+ def register_adapter! name, klass
36
+ validators[name] = klass
37
+ reload_validator
38
+ end
39
+
40
+ def register_dumper! name, klass
41
+ dumpers[name] = klass
42
+ end
43
+
44
+ def current_connection_adapter
45
+ ::ActiveRecord::Base.connection_pool.spec.config[:adapter]
46
+ end
47
+
48
+ def dumper
49
+ @dumper || dumpers[current_adapter].new
50
+ end
51
+
52
+ def adapter= adapter
53
+ @adapter = adapter
54
+ @validator = nil
55
+ end
56
+
57
+ def adapter
58
+ @adapter
59
+ end
60
+
61
+ def validator
62
+ return nil unless @adapter || validators[current_connection_adapter]
63
+
64
+ @validator ||= MigrationValidators::Core::AdapterWrapper.new(@adapter || validators[current_connection_adapter].new)
65
+ end
66
+
67
+ def reload_validator
68
+ @adapter = nil
69
+ @validator = nil
70
+ end
71
+
72
+ def load!
73
+ ::ActiveRecord::ConnectionAdapters::TableDefinition.class_eval { include MigrationValidators::ActiveRecord::ConnectionAdapters::TableDefinition }
74
+ ::ActiveRecord::ConnectionAdapters::Table.class_eval { include MigrationValidators::ActiveRecord::ConnectionAdapters::Table }
75
+ ::ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval { include MigrationValidators::ActiveRecord::ConnectionAdapters::AbstractAdapter }
76
+ ::ActiveRecord::Base.instance_eval { include MigrationValidators::ActiveRecord::Base }
77
+ ::ActiveRecord::Migration.instance_eval { include MigrationValidators::ActiveRecord::Migration }
78
+ ::ActiveRecord::Schema.instance_eval { include MigrationValidators::ActiveRecord::Schema }
79
+ ::ActiveRecord::SchemaDumper.instance_eval { include MigrationValidators::ActiveRecord::SchemaDumper }
80
+
81
+ ::ActiveRecord::SchemaDumper.ignore_tables << MigrationValidators.migration_validators_table_name.to_s
82
+ end
83
+ end
84
+ end
85
+
86
+ Dir.glob('adapters/**/*.rb').each {|file_name| require file_name}
87
+
88
+ if defined?(Rails::Railtie)
89
+ module Foreigner
90
+ class Railtie < Rails::Railtie
91
+ initializer 'migration-validators.load' do
92
+ ActiveSupport.on_load :active_record do
93
+ MigrationValidators.load!
94
+ end
95
+ end
96
+ end
97
+ end
98
+ else
99
+ MigrationValidators.load!
100
+ end
@@ -0,0 +1,7 @@
1
+ module MigrationValidators
2
+ @migration_validators_table_name = :migration_validators
3
+
4
+ class << self
5
+ attr_accessor :migration_validators_table_name
6
+ end
7
+ end
@@ -0,0 +1,440 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
2
+
3
+ AbstractAdapter = ::ActiveRecord::ConnectionAdapters::AbstractAdapter
4
+
5
+ describe ::ActiveRecord::ConnectionAdapters::AbstractAdapter, "migration validators extension", :type => :mv_test do
6
+
7
+ before :all do
8
+ use_memory_db
9
+ @migrations_table = ::ActiveRecord::Migrator::schema_migrations_table_name
10
+ @validators_table = MigrationValidators.migration_validators_table_name
11
+ end
12
+
13
+ before :each do
14
+ MigrationValidators::Core::DbValidator.rollback
15
+ end
16
+
17
+ describe :initialize_migration_validators_table do
18
+ before :each do
19
+ db.drop_table @validators_table if db.table_exists?(@validators_table)
20
+ end
21
+
22
+ it "creates table if it's not created" do
23
+ db.initialize_migration_validators_table
24
+ db.table_exists?(@validators_table).should be_true
25
+ end
26
+
27
+ it "should not throw an error if table already exists" do
28
+ db.initialize_migration_validators_table
29
+
30
+ lambda { db.initialize_migration_validators_table }.should_not raise_error
31
+ end
32
+ end
33
+
34
+ describe :initialize_schema_migrations_table do
35
+ before :each do
36
+
37
+ db.do_internally do
38
+ db.drop_table @migrations_table if db.table_exists?(@migrations_table)
39
+ db.drop_table @validators_table if db.table_exists?(@validators_table)
40
+ end
41
+ db.initialize_schema_migrations_table
42
+ end
43
+
44
+ it "should initialize migrations table as usuall" do
45
+ db.table_exists?(@migrations_table).should be_true
46
+ end
47
+
48
+ it "should also initialize validators table" do
49
+ db.table_exists?(@validators_table).should be_true
50
+ end
51
+ end
52
+
53
+ describe "synchronization with schema statements" do
54
+ before :each do
55
+
56
+ db.do_internally do
57
+ db.drop_table :new_table_name if db.table_exists?(:new_table_name)
58
+ db.drop_table :table_name if db.table_exists?(:table_name)
59
+ db.create_table(:table_name) do |t|
60
+ t.string :column_name
61
+ t.string :column_name1
62
+ end
63
+ end
64
+
65
+ MigrationValidators::Core::DbValidator.delete_all
66
+ end
67
+
68
+ describe :drop_table do
69
+ it "drops all validators of the dropped table" do
70
+ db.validate_column :table_name, :column_name, :uniqueness => {:message => "some message"}
71
+ MigrationValidators::Core::DbValidator.commit
72
+
73
+ db.drop_table :table_name
74
+ MigrationValidators::Core::DbValidator.commit
75
+
76
+ MigrationValidators::Core::DbValidator.count.should be_zero
77
+ end
78
+
79
+ it "still drops table" do
80
+ db.drop_table :table_name
81
+ db.table_exists?(:table_name).should be_false
82
+ end
83
+ end
84
+
85
+ describe :remove_column do
86
+ it "removes column validators" do
87
+ db.validate_column :table_name, :column_name, :uniqueness => {:message => "some message"}
88
+ db.validate_column :table_name, :column_name1, :uniqueness => {:message => "some message"}
89
+ MigrationValidators::Core::DbValidator.commit
90
+
91
+ db.remove_column :table_name, :column_name1
92
+ MigrationValidators::Core::DbValidator.commit
93
+
94
+ MigrationValidators::Core::DbValidator.count.should == 1
95
+ MigrationValidators::Core::DbValidator.first.column_name.should == "column_name"
96
+ end
97
+
98
+ it "still removes column" do
99
+ db.remove_column :table_name, :column_name1
100
+ db.column_exists?(:table_name, :column_name1).should be_false
101
+ end
102
+ end
103
+
104
+
105
+ describe :rename_column do
106
+ it "updates column name in validators table" do
107
+ db.validate_column :table_name, :column_name, :uniqueness => {:message => "some message"}
108
+ db.validate_column :table_name, :column_name1, :uniqueness => {:message => "some message"}
109
+ MigrationValidators::Core::DbValidator.commit
110
+
111
+ db.rename_column :table_name, :column_name1, :column_name2
112
+ MigrationValidators::Core::DbValidator.commit
113
+
114
+ MigrationValidators::Core::DbValidator.column_validators(:table_name, :column_name2).should_not be_blank
115
+ end
116
+
117
+ it "still renames column" do
118
+ db.rename_column :table_name, :column_name1, :column_name2
119
+ MigrationValidators::Core::DbValidator.commit
120
+
121
+ db.column_exists?(:table_name, :column_name1).should be_false
122
+ db.column_exists?(:table_name, :column_name2).should be_true
123
+ end
124
+ end
125
+
126
+ describe :rename_table do
127
+ it "updates table name in validators table" do
128
+ db.validate_column :table_name, :column_name, :uniqueness => {:message => "some message"}
129
+ MigrationValidators::Core::DbValidator.commit
130
+
131
+ db.rename_table :table_name, :new_table_name
132
+ MigrationValidators::Core::DbValidator.commit
133
+
134
+ MigrationValidators::Core::DbValidator.table_validators(:new_table_name).should_not be_blank
135
+ end
136
+
137
+ it "still renames table" do
138
+ db.rename_table :table_name, :new_table_name
139
+
140
+ db.table_exists?(:table_name).should be_false
141
+ db.table_exists?(:new_table_name).should be_true
142
+ end
143
+ end
144
+
145
+ describe :add_column do
146
+ before :each do
147
+ db.add_column :table_name, :new_column, :integer, :validates => {:uniqueness => true}
148
+ MigrationValidators::Core::DbValidator.commit
149
+ end
150
+
151
+ it "should update validators table" do
152
+ MigrationValidators::Core::DbValidator.column_validators(:table_name, :new_column).should_not be_blank
153
+ end
154
+
155
+ it "should still add column" do
156
+ db.column_exists?(:table_name, :new_column).should be_true
157
+ end
158
+ end
159
+
160
+ describe :change_column do
161
+ before :each do
162
+ db.add_column :table_name, :new_column, :integer, :validates => {:uniqueness => true}
163
+ MigrationValidators::Core::DbValidator.commit
164
+
165
+ db.change_column :table_name, :new_column, :string, :validates => {:presense => true}
166
+ MigrationValidators::Core::DbValidator.commit
167
+ end
168
+
169
+ it "should update validators table" do
170
+ MigrationValidators::Core::DbValidator.column_validators(:table_name, :new_column).first.validator_name.should == "presense"
171
+ end
172
+
173
+ it "should still change column" do
174
+ db.columns(:table_name).find{|col| col.name.to_sym == :new_column}.type.should == :string
175
+ end
176
+ end
177
+
178
+ describe :create_table do
179
+ before :each do
180
+ db.drop_table :created_table if db.table_exists?(:created_table)
181
+ db.create_table :created_table do |t|
182
+ t.column :column, :string, :validates => {:presense => true}
183
+ t.string :string_column, :validates => {:presense => true}
184
+ t.text :text_column, :validates => {:presense => true}
185
+ t.integer :integer_column, :validates => {:presense => true}
186
+ t.float :float_column, :validates => {:presense => true}
187
+ t.decimal :decimal_column, :validates => {:presense => true}
188
+ t.datetime :datetime_column, :validates => {:presense => true}
189
+ t.time :time_column, :validates => {:presense => true}
190
+ t.date :date_column, :validates => {:presense => true}
191
+ t.binary :binary_column, :validates => {:presense => true}
192
+ t.boolean :boolean_column, :validates => {:presense => true}
193
+ end
194
+
195
+ MigrationValidators::Core::DbValidator.commit
196
+ end
197
+
198
+ it "with generall column should update validators table" do
199
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :column).should_not be_blank
200
+ end
201
+
202
+ it "with string column should update validators table" do
203
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :string_column).should_not be_blank
204
+ end
205
+
206
+ it "with text column should update validators table" do
207
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :text_column).should_not be_blank
208
+ end
209
+
210
+ it "with integer column should update validators table" do
211
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :integer_column).should_not be_blank
212
+ end
213
+
214
+ it "with float column should update validators table" do
215
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :float_column).should_not be_blank
216
+ end
217
+
218
+ it "with decimal column should update validators table" do
219
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :decimal_column).should_not be_blank
220
+ end
221
+
222
+ it "with datetime column should update validators table" do
223
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :datetime_column).should_not be_blank
224
+ end
225
+
226
+ it "with time column should update validators table" do
227
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :time_column).should_not be_blank
228
+ end
229
+
230
+ it "with date column should update validators table" do
231
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :date_column).should_not be_blank
232
+ end
233
+
234
+ it "with binary column should update validators table" do
235
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :binary_column).should_not be_blank
236
+ end
237
+
238
+ it "with boolean column should update validators table" do
239
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :boolean_column).should_not be_blank
240
+ end
241
+ end
242
+ end
243
+
244
+ describe :change_table do
245
+ describe "create_columns" do
246
+ before :each do
247
+ db.drop_table :created_table if db.table_exists?(:created_table)
248
+ db.create_table(:created_table) {|t| t.string :dummy_column }
249
+ db.change_table :created_table do |t|
250
+ t.column :column, :string, :validates => {:presense => true}
251
+ t.string :string_column, :validates => {:presense => true}
252
+ t.text :text_column, :validates => {:presense => true}
253
+ t.integer :integer_column, :validates => {:presense => true}
254
+ t.float :float_column, :validates => {:presense => true}
255
+ t.decimal :decimal_column, :validates => {:presense => true}
256
+ t.datetime :datetime_column, :validates => {:presense => true}
257
+ t.time :time_column, :validates => {:presense => true}
258
+ t.date :date_column, :validates => {:presense => true}
259
+ t.binary :binary_column, :validates => {:presense => true}
260
+ t.boolean :boolean_column, :validates => {:presense => true}
261
+ end
262
+
263
+ MigrationValidators::Core::DbValidator.commit
264
+ end
265
+
266
+ it "with generall column should update validators table" do
267
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :column).should_not be_blank
268
+ end
269
+
270
+ it "with string column should update validators table" do
271
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :string_column).should_not be_blank
272
+ end
273
+
274
+ it "with text column should update validators table" do
275
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :text_column).should_not be_blank
276
+ end
277
+
278
+ it "with integer column should update validators table" do
279
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :integer_column).should_not be_blank
280
+ end
281
+
282
+ it "with float column should update validators table" do
283
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :float_column).should_not be_blank
284
+ end
285
+
286
+ it "with decimal column should update validators table" do
287
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :decimal_column).should_not be_blank
288
+ end
289
+
290
+ it "with datetime column should update validators table" do
291
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :datetime_column).should_not be_blank
292
+ end
293
+
294
+ it "with time column should update validators table" do
295
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :time_column).should_not be_blank
296
+ end
297
+
298
+ it "with date column should update validators table" do
299
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :date_column).should_not be_blank
300
+ end
301
+
302
+ it "with binary column should update validators table" do
303
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :binary_column).should_not be_blank
304
+ end
305
+
306
+ it "with boolean column should update validators table" do
307
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :boolean_column).should_not be_blank
308
+ end
309
+ end
310
+
311
+ describe "change_existing_columns" do
312
+ before :each do
313
+ db.drop_table :created_table if db.table_exists?(:created_table)
314
+
315
+ db.create_table :created_table do |t|
316
+ t.column :column, :string, :validates => {:presense => true}
317
+ t.column :column_1, :string, :validates => {:presense => true}
318
+ t.column :column_to_remove, :string, :validates => {:presense => true}
319
+ t.column :old_column_name, :string, :validates => {:presense => true}
320
+ end
321
+
322
+ MigrationValidators::Core::DbValidator.commit
323
+
324
+
325
+ db.change_table :created_table do |t|
326
+ t.change :column, :string, :validates => {:presense => false}
327
+ t.remove :column_to_remove
328
+ t.rename :old_column_name, :new_column_name
329
+ t.change_validates :column_1, :presense => false
330
+ end
331
+
332
+ MigrationValidators::Core::DbValidator.commit
333
+ end
334
+
335
+ it "with generall column should update validators table" do
336
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :column).should be_blank
337
+ end
338
+
339
+ it "should track column removing" do
340
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :column_to_remove).should be_blank
341
+ end
342
+
343
+ it "should trach column renaming" do
344
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :old_column_name).should be_blank
345
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :new_column_name).should_not be_blank
346
+ end
347
+
348
+ it "supports special change_validates method" do
349
+ MigrationValidators::Core::DbValidator.column_validators(:created_table, :column_1).should be_blank
350
+ end
351
+ end
352
+ end
353
+
354
+
355
+ describe "operations with validators" do
356
+ before :each do
357
+ db.create_table(:table_name) do |t|
358
+ t.string :column_name
359
+ end unless db.table_exists?(:table_name)
360
+
361
+ MigrationValidators::Core::DbValidator.remove_table_validators :table_name
362
+ MigrationValidators::Core::DbValidator.commit
363
+ end
364
+
365
+ describe :validate_column do
366
+ it "raises an exception if 0 validators defined" do
367
+ lambda {
368
+ db.validate_column :table_name, :column_name, {}
369
+ }.should raise_error(MigrationValidators::MigrationValidatorsException, /at least one column validator should be defined/)
370
+ end
371
+
372
+ it "adds all specified validators to the validator table" do
373
+
374
+ db.validate_column :table_name,
375
+ :column_name,
376
+ :uniqueness => {:message => "unique"},
377
+ :inclusion => {:message => "inclusion"}
378
+ MigrationValidators::Core::DbValidator.commit
379
+
380
+ MigrationValidators::Core::DbValidator.column_validators("table_name", "column_name").length.should == 2
381
+ end
382
+
383
+ it "stores validator options if they defined as hash" do
384
+ db.validate_column :table_name,
385
+ :column_name,
386
+ :uniqueness => {:message => "unique"}
387
+ MigrationValidators::Core::DbValidator.commit
388
+
389
+
390
+ MigrationValidators::Core::DbValidator.table_validators("table_name").first.options[:message].should == "unique"
391
+ end
392
+
393
+ it "should treat true in validator options as empty options list" do
394
+ db.validate_column :table_name,
395
+ :column_name,
396
+ :uniqueness => true
397
+ MigrationValidators::Core::DbValidator.commit
398
+
399
+ MigrationValidators::Core::DbValidator.table_validators("table_name").first.options.should == {}
400
+ end
401
+
402
+ it "should treat false in validator option as validator removing request" do
403
+ db.validate_column :table_name,
404
+ :column_name,
405
+ :uniqueness => true
406
+ MigrationValidators::Core::DbValidator.commit
407
+
408
+ db.validate_column :table_name,
409
+ :column_name,
410
+ :uniqueness => false
411
+ MigrationValidators::Core::DbValidator.commit
412
+
413
+ MigrationValidators::Core::DbValidator.table_validators(:table_name).should be_blank
414
+ end
415
+
416
+ it "should not allow nil instead of false as validator parameter" do
417
+ lambda {
418
+ db.validate_column :table_name,
419
+ :column_name,
420
+ :uniqueness => nil
421
+ MigrationValidators::Core::DbValidator.commit
422
+ }.should raise_error(MigrationValidators::MigrationValidatorsException, /use false to remove column validator/)
423
+ end
424
+
425
+ it "takes name of the context table if blank table name specified as parameter" do
426
+ db.in_context_of_table :table_name do
427
+ db.validate_column nil,
428
+ :column_name,
429
+ :uniqueness => true
430
+ MigrationValidators::Core::DbValidator.commit
431
+ end
432
+
433
+ MigrationValidators::Core::DbValidator.column_validators("table_name", "column_name").length.should == 1
434
+ end
435
+ end
436
+
437
+ describe :validate_table, "supports" do
438
+ end
439
+ end
440
+ end