Module: CheckedTypes::DataObjectsAdapter

Defined in:
lib/dm-checked-types/migrations.rb

Instance Method Summary collapse

Instance Method Details

#create_custom_type_statements(repository_name, model) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dm-checked-types/migrations.rb', line 3

def create_custom_type_statements(repository_name, model)
  model.properties.select {|x| x.type.ancestors.include?(CheckedInteger) }.each do |property|
    table_name      = model.storage_name(repository_name)
    property.type.range.each do |key, value|
      comparator = {
        :gt  => '>',
        :gte => '>=',
        :lt  => '<',
        :lte => '<='
      }[key] || raise(ArgumentError.new("Unsupported comparator: #{key}"))

      sql = <<-EOS.compress_lines
        ALTER TABLE #{quote_name(table_name)}
        ADD CONSTRAINT #{quote_name(check_constraint_name(table_name, property.name, key))}
        CHECK (#{quote_name(property.field)} #{comparator} #{value})
      EOS
      execute(sql)
    end
  end
end

#destroy_custom_type_statements(repository_name, model) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dm-checked-types/migrations.rb', line 24

def destroy_custom_type_statements(repository_name, model)
  model.properties.select {|x| x.type.ancestors.include?(CheckedInteger) }.each do |property|
    table_name = model.storage_name(repository_name)

    property.type.range.each do |key, value|
      constraint_name = check_constraint_name(table_name, property.name, key)

      next unless constraint_exists?(model.storage_name, constraint_name)

      sql = <<-EOS.compress_lines
        ALTER TABLE #{quote_name(model.storage_name(repository_name))}
        DROP CONSTRAINT #{quote_name(constraint_name)}
      EOS
      execute(sql)
    end
  end.compact
end