| Module | Sequel::ConstraintValidations |
| In: |
lib/sequel/extensions/constraint_validations.rb
|
| DEFAULT_CONSTRAINT_VALIDATIONS_TABLE | = | :sequel_constraint_validations | The default table name used for the validation metadata. | |
| OPERATORS | = | {:< => :lt, :<= => :lte, :> => :gt, :>= => :gte}.freeze | ||
| REVERSE_OPERATOR_MAP | = | {:str_lt => :<, :str_lte => :<=, :str_gt => :>, :str_gte => :>=, :int_lt => :<, :int_lte => :<=, :int_gt => :>, :int_gte => :>=}.freeze |
| constraint_validations_table | [RW] | 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 |
Set the default validation metadata table name if it has not already been set.
# File lib/sequel/extensions/constraint_validations.rb, line 146
146: def self.extended(db)
147: db.constraint_validations_table ||= DEFAULT_CONSTRAINT_VALIDATIONS_TABLE
148: end
Modify the default alter_table generator to include the constraint validation methods.
# File lib/sequel/extensions/constraint_validations.rb, line 309
309: def alter_table_generator(&block)
310: super do
311: extend AlterTableGeneratorMethods
312: @validations = []
313: instance_exec(&block) if block
314: end
315: end
Create the table storing the validation metadata for all of the constraints created by this extension.
# File lib/sequel/extensions/constraint_validations.rb, line 247
247: def create_constraint_validations_table
248: create_table(constraint_validations_table) do
249: String :table, :null=>false
250: String :constraint_name
251: String :validation_type, :null=>false
252: String :column, :null=>false
253: String :argument
254: String :message
255: TrueClass :allow_nil
256: end
257: end
Modify the default create_table generator to include the constraint validation methods.
# File lib/sequel/extensions/constraint_validations.rb, line 261
261: def create_table_generator(&block)
262: super do
263: extend CreateTableGeneratorMethods
264: @validations = []
265: instance_exec(&block) if block
266: end
267: 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, line 290
290: def drop_constraint_validations_for(opts=OPTS)
291: ds = from(constraint_validations_table)
292: if table = opts[:table]
293: ds = ds.where(:table=>constraint_validations_literal_table(table))
294: end
295: if column = opts[:column]
296: ds = ds.where(:column=>column.to_s)
297: end
298: if constraint = opts[:constraint]
299: ds = ds.where(:constraint_name=>constraint.to_s)
300: end
301: unless table || column || constraint
302: raise Error, "must specify :table, :column, or :constraint when dropping constraint validations"
303: end
304: ds.delete
305: end