Module: DynamicMigrations::Postgres::Generator::Trigger
- Included in:
- DynamicMigrations::Postgres::Generator
- Defined in:
- lib/dynamic_migrations/postgres/generator/trigger.rb
Defined Under Namespace
Classes: TemplateAlreadyExistsError, UnexpectedTemplateError
Class Method Summary collapse
-
.add_template(name, template_class) ⇒ Object
install a template into the migration generator, this can be used from outside this library to clean up common migrations (replace common migrations with syntatic sugar).
- .has_template?(template_name) ⇒ Boolean
- .template(template_name) ⇒ Object
Instance Method Summary collapse
- #add_trigger(trigger, code_comment = nil) ⇒ Object
- #recreate_trigger(original_trigger, updated_trigger) ⇒ Object
- #remove_trigger(trigger, code_comment = nil) ⇒ Object
-
#remove_trigger_comment(trigger, code_comment = nil) ⇒ Object
remove the comment from a trigger.
-
#set_trigger_comment(trigger, code_comment = nil) ⇒ Object
add a comment to a trigger.
Class Method Details
.add_template(name, template_class) ⇒ Object
install a template into the migration generator, this can be used from outside this library to clean up common migrations (replace common migrations with syntatic sugar)
21 22 23 24 25 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 21 def self.add_template name, template_class @templates ||= {} raise TemplateAlreadyExistsError, name if @templates.key?(name) @templates[name] = template_class end |
.has_template?(template_name) ⇒ Boolean
15 16 17 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 15 def self.has_template? template_name @templates&.key?(template_name) || false end |
.template(template_name) ⇒ Object
11 12 13 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 11 def self.template template_name @templates && @templates[template_name] end |
Instance Method Details
#add_trigger(trigger, code_comment = nil) ⇒ Object
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 27 def add_trigger trigger, code_comment = nil # if we have a corresponding template, then use it if trigger.template unless (template_class = Trigger.template(trigger.template)) raise UnexpectedTemplateError, "Unrecognised template #{trigger.template}" end arguments = template_class.new(trigger, code_comment).fragment_arguments # if the template class returns nil, then we skip the creation of this migration # this is common for triggers where the template is respinsible for setting up # multiple triggers (a mix of on update/insert and before/after etc.) unless arguments.nil? # we only provide a dependent function if the function has more than one trigger # or is in a different schema, otherwise it is added to the same migration as this # trigger and we don't need to worry about dependencies if trigger.function && (trigger.function.triggers.count > 1 || trigger.table.schema != trigger.function.schema) add_fragment(dependent_function: trigger.function, **arguments) else add_fragment(**arguments) end end # no template, process this as a default trigger (takes all options) else = { name: ":#{trigger.name}" } # if the trigger is a row trigger and the action timing is before or after # then we can use some syntactic sugar to make the migration look nicer # we use method names like before_insert, after_update, etc. and drop the # unnessessary options if trigger.action_orientation == :row && [:before, :after].include?(trigger.action_timing) method_name = "#{trigger.action_timing}_#{trigger.event_manipulation}" else method_name = "add_trigger" [:action_timing] = ":#{trigger.action_timing}" [:event_manipulation] = ":#{trigger.event_manipulation}" [:action_orientation] = ":#{trigger.action_orientation}" end # added here because we want the timing and manipulation to visually appear first [:function_schema_name] = ":#{trigger.function.schema.name}" [:function_name] = ":#{trigger.function.name}" unless trigger.action_condition.nil? [:action_condition] = "\"#{trigger.action_condition}\"" end unless trigger.parameters.empty? [:parameters] = "[\"#{trigger.parameters.join('", "')}\"]" end unless trigger.action_reference_old_table.nil? [:action_reference_old_table] = ":#{trigger.action_reference_old_table}" end unless trigger.action_reference_new_table.nil? [:action_reference_new_table] = ":#{trigger.action_reference_new_table}" end unless trigger.description.nil? [:comment] = <<~RUBY <<~COMMENT #{indent trigger.description} COMMENT RUBY end = .map { |k, v| "#{k}: #{v}" }.join(", ") # we only provide a dependent function if the function has more than one trigger # or is in a different schema, otherwise it is added to the same migration as this # trigger and we don't need to worry about dependencies dependent_function = nil # trigger.function will be nil if the trigger is not an function if trigger.function && (trigger.function.triggers.count > 1 || trigger.table.schema != trigger.function.schema) dependent_function = trigger.function end add_fragment schema: trigger.table.schema, table: trigger.table, migration_method: :add_trigger, object: trigger, code_comment: code_comment, dependent_function: dependent_function, migration: <<~RUBY #{method_name} :#{trigger.table.name}, #{} RUBY end end |
#recreate_trigger(original_trigger, updated_trigger) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 131 def recreate_trigger original_trigger, updated_trigger fragments = [] # remove the original trigger fragments << remove_trigger(original_trigger, <<~CODE_COMMENT) Removing original trigger because it has changed (it is recreated below) Changes: #{indent original_trigger.differences_descriptions(updated_trigger).join("\n")} CODE_COMMENT # recrete the trigger with the new options recreation_fragment = add_trigger updated_trigger, <<~CODE_COMMENT Recreating this trigger CODE_COMMENT fragments << recreation_fragment unless recreation_fragment.nil? # return the new fragments (the main reason to return them here is for the specs) fragments end |
#remove_trigger(trigger, code_comment = nil) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 120 def remove_trigger trigger, code_comment = nil add_fragment schema: trigger.table.schema, table: trigger.table, migration_method: :remove_trigger, object: trigger, code_comment: code_comment, migration: <<~RUBY remove_trigger :#{trigger.table.name}, :#{trigger.name} RUBY end |
#remove_trigger_comment(trigger, code_comment = nil) ⇒ Object
remove the comment from a trigger
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 171 def remove_trigger_comment trigger, code_comment = nil add_fragment schema: trigger.table.schema, table: trigger.table, migration_method: :remove_trigger_comment, object: trigger, code_comment: code_comment, migration: <<~RUBY remove_trigger_comment :#{trigger.table.name}, :#{trigger.name} RUBY end |
#set_trigger_comment(trigger, code_comment = nil) ⇒ Object
add a comment to a trigger
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/dynamic_migrations/postgres/generator/trigger.rb', line 151 def set_trigger_comment trigger, code_comment = nil description = trigger.description if description.nil? raise MissingDescriptionError, "Missing required description for trigger `#{trigger.name}` on table `#{trigger.table.schema.name}.#{trigger.table.name}`" end add_fragment schema: trigger.table.schema, table: trigger.table, migration_method: :set_trigger_comment, object: trigger, code_comment: code_comment, migration: <<~RUBY set_trigger_comment :#{trigger.table.name}, :#{trigger.name}, <<~COMMENT #{indent description} COMMENT RUBY end |