Module: DynamicMigrations::ActiveRecord::Migrators::Trigger
- Included in:
- DynamicMigrations::ActiveRecord::Migrators
- Defined in:
- lib/dynamic_migrations/active_record/migrators/trigger.rb
Defined Under Namespace
Classes: UnexpectedActionOrientationError, UnexpectedActionTimingError, UnexpectedConditionsError, UnexpectedEventManipulationError
Instance Method Summary collapse
-
#add_trigger(table_name, name:, action_timing:, event_manipulation:, action_orientation:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
create a postgres trigger.
- #after_delete(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
- #after_insert(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
- #after_update(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
- #before_delete(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
-
#before_insert(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
wrappers for add_trigger which provide more friendly syntax.
- #before_update(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
- #remove_trigger(table_name, trigger_name) ⇒ Object
- #remove_trigger_comment(table_name, trigger_name) ⇒ Object
- #set_trigger_comment(table_name, trigger_name, comment) ⇒ Object
Instance Method Details
#add_trigger(table_name, name:, action_timing:, event_manipulation:, action_orientation:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
create a postgres trigger
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 58 59 60 61 62 63 64 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 19 def add_trigger table_name, name:, action_timing:, event_manipulation:, action_orientation:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil unless [:insert, :delete, :update].include? event_manipulation raise UnexpectedEventManipulationError, event_manipulation end unless action_condition.nil? || action_condition.is_a?(String) raise UnexpectedConditionsError, "expected String but got `#{action_condition}`" end unless [:row, :statement].include? action_orientation raise UnexpectedActionOrientationError, action_orientation end unless [:before, :after, :instead_of].include? action_timing raise UnexpectedActionTimingError, action_timing end # "INSTEAD OF/BEFORE/AFTER" "INSERT/UPDATE/DELETE" timing_sql = "#{action_timing.to_s.sub("_", " ")} #{event_manipulation}".upcase condition_sql = action_condition.nil? ? "" : "WHEN (#{action_condition})" temp_tables = [] unless action_reference_old_table.nil? temp_tables << "OLD TABLE AS #{action_reference_old_table}" end unless action_reference_new_table.nil? temp_tables << "NEW TABLE AS #{action_reference_new_table}" end temp_tables_sql = temp_tables.any? ? "REFERENCING #{temp_tables.join(" ")}" : "" parameters_str = parameters.map { |p| quote(p) }.join(", ") # schema_name was not provided to this method, it comes from the migration class execute <<~SQL CREATE TRIGGER #{name} #{timing_sql} ON #{schema_name}.#{table_name} #{temp_tables_sql} FOR EACH #{action_orientation} #{condition_sql} EXECUTE FUNCTION #{function_schema_name}.#{function_name}(#{parameters_str}); SQL if comment.is_a? String set_trigger_comment table_name, name, comment end end |
#after_delete(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
87 88 89 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 87 def after_delete table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil add_trigger table_name, name: name, action_timing: :after, event_manipulation: :delete, action_orientation: :row, function_schema_name: function_schema_name, function_name: function_name, parameters: parameters, action_condition: action_condition, action_reference_old_table: action_reference_old_table, action_reference_new_table: action_reference_new_table, comment: comment end |
#after_insert(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
79 80 81 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 79 def after_insert table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil add_trigger table_name, name: name, action_timing: :after, event_manipulation: :insert, action_orientation: :row, function_schema_name: function_schema_name, function_name: function_name, parameters: parameters, action_condition: action_condition, action_reference_old_table: action_reference_old_table, action_reference_new_table: action_reference_new_table, comment: comment end |
#after_update(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
83 84 85 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 83 def after_update table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil add_trigger table_name, name: name, action_timing: :after, event_manipulation: :update, action_orientation: :row, function_schema_name: function_schema_name, function_name: function_name, parameters: parameters, action_condition: action_condition, action_reference_old_table: action_reference_old_table, action_reference_new_table: action_reference_new_table, comment: comment end |
#before_delete(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
75 76 77 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 75 def before_delete table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil add_trigger table_name, name: name, action_timing: :before, event_manipulation: :delete, action_orientation: :row, function_schema_name: function_schema_name, function_name: function_name, parameters: parameters, action_condition: action_condition, action_reference_old_table: action_reference_old_table, action_reference_new_table: action_reference_new_table, comment: comment end |
#before_insert(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
wrappers for add_trigger which provide more friendly syntax
67 68 69 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 67 def before_insert table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil add_trigger table_name, name: name, action_timing: :before, event_manipulation: :insert, action_orientation: :row, function_schema_name: function_schema_name, function_name: function_name, parameters: parameters, action_condition: action_condition, action_reference_old_table: action_reference_old_table, action_reference_new_table: action_reference_new_table, comment: comment end |
#before_update(table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil) ⇒ Object
71 72 73 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 71 def before_update table_name, name:, function_schema_name:, function_name:, parameters: [], action_condition: nil, action_reference_old_table: nil, action_reference_new_table: nil, comment: nil add_trigger table_name, name: name, action_timing: :before, event_manipulation: :update, action_orientation: :row, function_schema_name: function_schema_name, function_name: function_name, parameters: parameters, action_condition: action_condition, action_reference_old_table: action_reference_old_table, action_reference_new_table: action_reference_new_table, comment: comment end |
#remove_trigger(table_name, trigger_name) ⇒ Object
91 92 93 94 95 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 91 def remove_trigger table_name, trigger_name execute <<~SQL DROP TRIGGER #{trigger_name} ON #{schema_name}.#{table_name}; SQL end |
#remove_trigger_comment(table_name, trigger_name) ⇒ Object
103 104 105 106 107 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 103 def remove_trigger_comment table_name, trigger_name execute <<~SQL COMMENT ON TRIGGER #{trigger_name} ON #{schema_name}.#{table_name} IS NULL; SQL end |
#set_trigger_comment(table_name, trigger_name, comment) ⇒ Object
97 98 99 100 101 |
# File 'lib/dynamic_migrations/active_record/migrators/trigger.rb', line 97 def set_trigger_comment table_name, trigger_name, comment execute <<~SQL COMMENT ON TRIGGER #{trigger_name} ON #{schema_name}.#{table_name} IS #{quote comment}; SQL end |