Module: DynamicMigrations::Postgres::Server::Database::Differences::ToMigrations::Schemas::Enums

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

Instance Method Summary collapse

Instance Method Details

#process_enum(schema_name, enum_name, configuration_enum, database_enum) ⇒ Object



21
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
# File 'lib/dynamic_migrations/postgres/server/database/differences/to_migrations/schemas/enums.rb', line 21

def process_enum schema_name, enum_name, configuration_enum, database_enum
  # If the enum exists in the configuration but not in the database
  # then we have to create it.
  if configuration_enum[:exists] == true && !database_enum[:exists]
    log.debug "  Enum `#{enum_name}` exists in configuration but not in the database"

    # a migration to create the enum
    enum = @database.configured_schema(schema_name).enum(enum_name)
    @generator.create_enum enum
    # optionally add the description
    if enum.has_description?
      @generator.set_enum_comment enum
    end

  # If the schema exists in the database but not in the configuration
  # then we need to delete it.
  elsif database_enum[:exists] == true && !configuration_enum[:exists]
    log.debug "  Enum `#{enum_name}` exists in database but not in the configuration"

    # a migration to create the enum
    enum = @database.loaded_schema(schema_name).enum(enum_name)
    @generator.drop_enum enum

  # If the enum exists in both the configuration and database representations
  # but the values is different then we need to update the values.
  elsif configuration_enum[:values][:matches] == false
    log.debug "  Enum `#{enum_name}` exists in both configuration and the database"

    log.debug "    Enum `#{enum_name}` values are different"
    original_enum = @database.loaded_schema(schema_name).enum(enum_name)
    updated_enum = @database.configured_schema(schema_name).enum(enum_name)
    @generator.update_enum original_enum, updated_enum
    # does the description also need to be updated
    if configuration_enum[:description][:matches] == false
      # if the description was removed
      if configuration_enum[:description].nil?
        log.debug "    Enum `#{enum_name}` description exists in database but not in the configuration"
        @generator.remove_enum_comment updated_enum
      else
        log.debug "    Enum `#{enum_name}` description does not match"
        @generator.set_enum_comment updated_enum
      end
    end

  # If the enum exists in both the configuration and database representations
  # but the description is different then we need to update the description.
  elsif configuration_enum[:description][:matches] == false
    enum = @database.configured_schema(schema_name).enum(enum_name)
    # if the description was removed
    if configuration_enum[:description].nil?
      log.debug "    Enum `#{enum_name}` description exists in database but not in the configuration"
      @generator.remove_enum_comment enum
    else
      log.debug "    Enum `#{enum_name}` description does not match"
      @generator.set_enum_comment enum
    end
  end
end

#process_enums(schema_name, configuration_enums, database_enums) ⇒ Object



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

def process_enums schema_name, configuration_enums, database_enums
  # process all the enums
  log.debug "  Processing tables"
  enum_names = (configuration_enums.keys + database_enums.keys).uniq
  enum_names.each do |enum_name|
    log.debug "  Processing table #{enum_name}"
    process_enum schema_name, enum_name, configuration_enums[enum_name] || {}, database_enums[enum_name] || {}
  end
end