Module: DynamicMigrations::Postgres::Server::Database::Differences::ToMigrations::Schemas::Tables::Validations

Included in:
DynamicMigrations::Postgres::Server::Database::Differences::ToMigrations
Defined in:
lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/validations.rb

Instance Method Summary collapse

Instance Method Details

#process_validation(schema_name, table_name, validation_name, configuration_validation, database_validation) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/validations.rb', line 22

def process_validation schema_name, table_name, validation_name, configuration_validation, database_validation
  # If the validation exists in the configuration but not in the database
  # then we have to create it.
  if configuration_validation[:exists] == true && !database_validation[:exists]
    log.debug "    Validation `#{validation_name}` exists in configuration but not in the database"

    # a migration to create the validation
    validation = @database.configured_schema(schema_name).table(table_name).validation(validation_name)
    @generator.add_validation validation

  # If the schema exists in the database but not in the configuration
  # then we need to delete it.
  elsif database_validation[:exists] == true && !configuration_validation[:exists]
    log.debug "    Validation `#{validation_name}` exists in database but not in the configuration"

    # a migration to create the validation
    validation = @database.loaded_schema(schema_name).table(table_name).validation(validation_name)
    @generator.remove_validation validation

  # If the validation exists in both the configuration and database representations
  # but the definition (except description, which is handled seeprately below) is different
  # then we need to update the definition.
  elsif configuration_validation.except(:exists, :description).filter { |name, attributes| attributes[:matches] == false }.any?
    log.debug "    Validation `#{validation_name}` exists in both configuration and the database"

    log.debug "      Validation `#{validation_name}` is different"
    # recreate the validation
    original_validation = @database.loaded_schema(schema_name).table(table_name).validation(validation_name)
    updated_validation = @database.configured_schema(schema_name).table(table_name).validation(validation_name)
    @generator.recreate_validation original_validation, updated_validation
    # does the description also need to be updated
    if configuration_validation[:description][:matches] == false
      # if the description was removed
      if configuration_validation[:description].nil?
        log.debug "      Validation `#{validation_name}` description exists in database but not in the configuration"
        @generator.remove_validation_comment updated_validation
      else
        log.debug "      Validation `#{validation_name}` does not match"
        @generator.set_validation_comment updated_validation
      end
    end

  # If the validation exists in both the configuration and database representations
  # but the description is different then we need to update the description.
  elsif configuration_validation[:description][:matches] == false
    log.debug "    Validation `#{validation_name}` exists in both configuration and the database"

    validation = @database.configured_schema(schema_name).table(table_name).validation(validation_name)
    # if the description was removed
    if configuration_validation[:description].nil?
      log.debug "      Validation `#{validation_name}` description exists in database but not in the configuration"
      @generator.remove_validation_comment validation
    else
      log.debug "      Validation `#{validation_name}` does not match"
      @generator.set_validation_comment validation
    end
  end
end

#process_validations(schema_name, table_name, configuration_validations, database_validations) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/tables/validations.rb', line 12

def process_validations schema_name, table_name, configuration_validations, database_validations
  # process all the validations
  log.debug "    Processing Validations"
  validation_names = (configuration_validations.keys + database_validations.keys).uniq
  validation_names.each do |validation_name|
    log.debug "    Processing Validation #{validation_name}"
    process_validation schema_name, table_name, validation_name, configuration_validations[validation_name] || {}, database_validations[validation_name] || {}
  end
end