module Sequel::ConstraintValidations
Constants
- DEFAULT_CONSTRAINT_VALIDATIONS_TABLE
The default table name used for the validation metadata.
- OPERATORS
- REVERSE_OPERATOR_MAP
Attributes
The name of the table storing the validation metadata. If modifying this from the default, this should be changed directly after loading the extension into the database
Public Class Methods
Set the default validation metadata table name if it has not already been set.
# File lib/sequel/extensions/constraint_validations.rb 150 def self.extended(db) 151 db.constraint_validations_table ||= DEFAULT_CONSTRAINT_VALIDATIONS_TABLE 152 end
Public Instance Methods
Modify the default alter_table generator to include the constraint validation methods.
# File lib/sequel/extensions/constraint_validations.rb 321 def alter_table_generator(&block) 322 super do 323 extend AlterTableGeneratorMethods 324 @validations = [] 325 instance_exec(&block) if block 326 end 327 end
Create the table storing the validation metadata for all of the constraints created by this extension.
# File lib/sequel/extensions/constraint_validations.rb 249 def create_constraint_validations_table 250 create_table(constraint_validations_table) do 251 String :table, :null=>false 252 String :constraint_name 253 String :validation_type, :null=>false 254 String :column, :null=>false 255 String :argument 256 String :message 257 TrueClass :allow_nil 258 end 259 end
Modify the default create_table generator to include the constraint validation methods.
# File lib/sequel/extensions/constraint_validations.rb 263 def create_table_generator(&block) 264 super do 265 extend CreateTableGeneratorMethods 266 @validations = [] 267 instance_exec(&block) if block 268 end 269 end
Delete validation metadata for specific constraints. At least one of the following options should be specified:
- :table
-
The table containing the constraint
- :column
-
The column affected by the constraint
- :constraint
-
The name of the related constraint
The main reason for this method is when dropping tables or columns. If you have previously defined a constraint validation on the table or column, you should delete the related metadata when dropping the table or column. For a table, this isn’t a big issue, as it will just result in some wasted space, but for columns, if you don’t drop the related metadata, it could make it impossible to save rows, since a validation for a nonexistent column will be created.
# File lib/sequel/extensions/constraint_validations.rb 302 def drop_constraint_validations_for(opts=OPTS) 303 ds = from(constraint_validations_table) 304 if table = opts[:table] 305 ds = ds.where(:table=>constraint_validations_literal_table(table)) 306 end 307 if column = opts[:column] 308 ds = ds.where(:column=>column.to_s) 309 end 310 if constraint = opts[:constraint] 311 ds = ds.where(:constraint_name=>constraint.to_s) 312 end 313 unless table || column || constraint 314 raise Error, "must specify :table, :column, or :constraint when dropping constraint validations" 315 end 316 ds.delete 317 end
Drop the constraint validations table.
# File lib/sequel/extensions/constraint_validations.rb 282 def drop_constraint_validations_table 283 drop_table(constraint_validations_table) 284 end
Drop all constraint validations for a table if dropping the table.
# File lib/sequel/extensions/constraint_validations.rb 272 def drop_table(*names) 273 names.each do |name| 274 if !name.is_a?(Hash) && table_exists?(constraint_validations_table) 275 drop_constraint_validations_for(:table=>name) 276 end 277 end 278 super 279 end