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
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 8

def create_enum enum, code_comment = nil
  add_fragment schema: enum.schema,
    table: optional_enum_table(enum),
    migration_method: :create_enum,
    object: enum,
    code_comment: code_comment,
    migration: "      create_enum :\#{enum.name}, [\n        \"\#{enum.values.join(\"\\\",\\n  \\\"\")}\"\n      ]\n    RUBY\nend\n"

#drop_enum(enum, code_comment = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 41

def drop_enum enum, code_comment = nil
  add_fragment schema: enum.schema,
    table: optional_enum_table(enum),
    migration_method: :drop_enum,
    object: enum,
    code_comment: code_comment,
    migration: "      drop_enum :\#{enum.name}\n    RUBY\nend\n"

#optional_enum_table(enum) ⇒ Object

we only provide a table to these migration fragments if the enum applies only to one table and that take is in the same schema as the enum



80
81
82
83
84
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 80

def optional_enum_table enum
  # all the tables which use this enum
  tables = enum.columns.map(&:table).uniq
  (tables.count == 1 && tables.first&.schema == enum.schema) ? tables.first : nil
end

#remove_enum_comment(enum, code_comment = nil) ⇒ Object

remove the comment from a enum



67
68
69
70
71
72
73
74
75
76
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 67

def remove_enum_comment enum, code_comment = nil
  add_fragment schema: enum.schema,
    table: optional_enum_table(enum),
    migration_method: :remove_enum_comment,
    object: enum,
    code_comment: code_comment,
    migration: "      remove_enum_comment :\#{enum.name}\n    RUBY\nend\n"

#set_enum_comment(enum, code_comment = nil) ⇒ Object

add a comment to a enum



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 53

def set_enum_comment enum, code_comment = nil
  add_fragment schema: enum.schema,
    table: optional_enum_table(enum),
    migration_method: :set_enum_comment,
    object: enum,
    code_comment: code_comment,
    migration: "      set_enum_comment :\#{enum.name}, <<~COMMENT\n        \#{indent enum.description || \"\"}\n      COMMENT\n    RUBY\nend\n"

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dynamic_migrations/postgres/generator/enum.rb', line 21

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,
    table: optional_enum_table(updated_enum),
    migration_method: :add_enum_values,
    object: updated_enum,
    code_comment: code_comment,
    migration: "      add_enum_values :\#{updated_enum.name}, [\n        \"\#{added_values.join(\"\\\",\\n  \\\"\")}\"\n      ]\n    RUBY\nend\n"