Module: DynamicMigrations::Postgres::Generator::Index

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

Instance Method Summary collapse

Instance Method Details

#add_index(index, code_comment = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/dynamic_migrations/postgres/generator/index.rb', line 5

def add_index index, code_comment = nil
  # the migration accepts either a single column name or an array of column names
  # we use the appropriate syntax just to make the migration prettier and easier
  # to understand
  column_names = (index.column_names.count == 1) ? ":#{index.column_names.first}" : "[:#{index.column_names.join(", :")}]"

  options = {
    name: ":#{index.name}",
    unique: index.unique,
    using: ":#{index.type}",
    # todo: support custom sorting, it requires refactoring the index class because the ordering is actually on a column by column basis, not the index itself
    sort: ":#{index.order}"
  }

  # :first is the default when :desc is specified, :last is the default when :asc is specified
  if (index.order == :desc && index.nulls_position == :last) || (index.order == :asc && index.nulls_position == :first)
    # todo: support nulls_position, it requires writing our own migrator because rails does not provide this option
    raise "custom nulls_position is not currently supported"
  end

  unless index.where.nil?
    options[:where] = "\"#{index.where}\""
  end

  unless index.description.nil?
    options[:comment] = "      <<~COMMENT\n        \#{indent index.description}\n      COMMENT\n    RUBY\n  end\n\n  where_sql = \"\"\n  unless index.where.nil?\n    options[:where] = \"\#{index.name}_where_sql\"\n    where_sql = <<~RUBY\n      \#{index.name}_where_sql = <<~SQL\n        \#{indent index.where}\n      SQL\n    RUBY\n  end\n\n  options_syntax = options.map { |k, v| \"\#{k}: \#{v}\" }.join(\", \")\n\n  add_fragment schema: index.table.schema,\n    table: index.table,\n    migration_method: :add_index,\n    object: index,\n    code_comment: code_comment,\n    migration: where_sql + <<~RUBY\n      add_index :\#{index.table.name}, \#{column_names}, \#{options_syntax}\n    RUBY\nend\n"

#recreate_index(original_index, updated_index) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dynamic_migrations/postgres/generator/index.rb', line 70

def recreate_index original_index, updated_index
  # remove the original index
  removal_fragment = remove_index original_index, "    Removing original index because it has changed (it is recreated below)\n    Changes:\n      \#{indent original_index.differences_descriptions(updated_index).join(\"\\n\")}\n  CODE_COMMENT\n\n  # recrete the index with the new options\n  recreation_fragment = add_index updated_index, <<~CODE_COMMENT\n    Recreating this index\n  CODE_COMMENT\n\n  # return the new fragments (the main reason to return them here is for the specs)\n  [removal_fragment, recreation_fragment]\nend\n"

#remove_index(index, code_comment = nil) ⇒ Object



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

def remove_index index, code_comment = nil
  add_fragment schema: index.table.schema,
    table: index.table,
    migration_method: :remove_index,
    object: index,
    code_comment: code_comment,
    migration: "      remove_index :\#{index.table.name}, :\#{index.name}\n    RUBY\nend\n"

#remove_index_comment(index, code_comment = nil) ⇒ Object

remove the comment from a index



108
109
110
111
112
113
114
115
116
117
# File 'lib/dynamic_migrations/postgres/generator/index.rb', line 108

def remove_index_comment index, code_comment = nil
  add_fragment schema: index.table.schema,
    table: index.table,
    migration_method: :remove_index_comment,
    object: index,
    code_comment: code_comment,
    migration: "      remove_index_comment :\#{index.table.name}, :\#{index.name}\n    RUBY\nend\n"

#set_index_comment(index, code_comment = nil) ⇒ Object

add a comment to a index



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dynamic_migrations/postgres/generator/index.rb', line 88

def set_index_comment index, code_comment = nil
  description = index.description

  if description.nil?
    raise MissingDescriptionError, "Missing required description for index `#{index.name}` in table `#{index.table.schema.name}.#{index.table.name}`"
  end

  add_fragment schema: index.table.schema,
    table: index.table,
    migration_method: :set_index_comment,
    object: index,
    code_comment: code_comment,
    migration: "      set_index_comment :\#{index.table.name}, :\#{index.name}, <<~COMMENT\n        \#{indent description}\n      COMMENT\n    RUBY\nend\n"