Module: DynamicMigrations::Postgres::Generator::Enum

Included in:
DynamicMigrations::Postgres::Generator
Defined in:
lib/dynamic_migrations/postgres/generator/enum.rb

Defined Under Namespace

Classes: UnremovableEnumValuesError

Instance Method Summary collapse

Instance Method Details

#create_enum(enum, code_comment = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 8

def create_enum enum, code_comment = nil
  # we only provide a table if the enum has a single column and they
  # are in the same schema, otherwise we can't reliable handle dependencies
  # so the enum will be created in the schema's migration
  enum_table = nil
  if enum.columns.count == 1 && enum.schema == enum.columns.first&.table&.schema
    enum_table = enum.columns.first&.table
  end

  add_fragment schema: enum.schema,
    table: enum_table,
    migration_method: :create_enum,
    object: enum,
    code_comment: code_comment,
    migration: <<~RUBY
      create_enum :#{enum.name}, [
        "#{enum.values.join("\",\n  \"")}"
      ]
    RUBY
end

#drop_enum(enum, code_comment = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 48

def drop_enum enum, code_comment = nil
  add_fragment schema: enum.schema,
    migration_method: :drop_enum,
    object: enum,
    code_comment: code_comment,
    migration: <<~RUBY
      drop_enum :#{enum.name}
    RUBY
end

#remove_enum_comment(enum, code_comment = nil) ⇒ Object

remove the comment from a enum



72
73
74
75
76
77
78
79
80
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 72

def remove_enum_comment enum, code_comment = nil
  add_fragment schema: enum.schema,
    migration_method: :remove_enum_comment,
    object: enum,
    code_comment: code_comment,
    migration: <<~RUBY
      remove_enum_comment :#{enum.name}
    RUBY
end

#set_enum_comment(enum, code_comment = nil) ⇒ Object

add a comment to a enum



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 59

def set_enum_comment enum, code_comment = nil
  add_fragment schema: enum.schema,
    migration_method: :set_enum_comment,
    object: enum,
    code_comment: code_comment,
    migration: <<~RUBY
      set_enum_comment :#{enum.name}, <<~COMMENT
        #{indent enum.description || ""}
      COMMENT
    RUBY
end

#update_enum(original_enum, updated_enum, code_comment = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 29

def update_enum original_enum, updated_enum, code_comment = nil
  added_values = updated_enum.values - original_enum.values
  removed_values = original_enum.values - updated_enum.values

  if removed_values.any?
    raise UnremovableEnumValuesError, "You can not remove enum values from postgres. Tring to remove '#{removed_values.join("', ")}'"
  end

  add_fragment schema: updated_enum.schema,
    migration_method: :add_enum_values,
    object: updated_enum,
    code_comment: code_comment,
    migration: <<~RUBY
      add_enum_values :#{updated_enum.name}, [
        "#{added_values.join("\",\n  \"")}"
      ]
    RUBY
end