Module: Whodunit::MigrationHelpers
- Defined in:
- lib/whodunit/migration_helpers.rb
Overview
Database migration helpers for adding whodunit stamp columns.
This module provides convenient methods for adding creator/updater/deleter tracking columns to database tables based on configuration. Uses the configured column names and data types, and adds appropriate indexes for performance.
The helpers respect column enabling/disabling configuration: - Only adds columns that are enabled (not nil) in configuration - Uses configured column names and data types - Deleter column inclusion based on soft_delete_column configuration
Instance Method Summary collapse
-
#add_whodunit_stamps(table_name, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil) ⇒ void
Add creator/updater/deleter stamp columns to an existing table.
-
#remove_whodunit_stamps(table_name, include_deleter: :auto, **_options) ⇒ void
Remove stamp columns from an existing table.
-
#whodunit_stamps(table_def = nil, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil) ⇒ void
Add stamp columns to a table definition or existing table.
Instance Method Details
#add_whodunit_stamps(table_name, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil) ⇒ void
This method returns an undefined value.
Add creator/updater/deleter stamp columns to an existing table.
This method adds the configured whodunit columns to an existing table based on the current configuration. Only adds columns that are enabled (not nil). The deleter column is included based on soft_delete_column configuration when include_deleter is :auto, or explicitly when include_deleter is true. Indexes are automatically added for performance.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/whodunit/migration_helpers.rb', line 77 def add_whodunit_stamps(table_name, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil) if Whodunit.creator_enabled? add_column table_name, Whodunit.creator_column, creator_type || Whodunit.creator_data_type, null: true end if Whodunit.updater_enabled? add_column table_name, Whodunit.updater_column, updater_type || Whodunit.updater_data_type, null: true end if should_include_deleter?(include_deleter) add_column table_name, Whodunit.deleter_column, deleter_type || Whodunit.deleter_data_type, null: true end add_whodunit_indexes(table_name, include_deleter) end |
#remove_whodunit_stamps(table_name, include_deleter: :auto, **_options) ⇒ void
This method returns an undefined value.
Remove stamp columns from an existing table.
This method removes the configured creator, updater, and optionally deleter columns from an existing table. Only removes columns that are enabled in configuration and actually exist in the database.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/whodunit/migration_helpers.rb', line 110 def remove_whodunit_stamps(table_name, include_deleter: :auto, **) if Whodunit.creator_enabled? && column_exists?(table_name, Whodunit.creator_column) remove_column table_name, Whodunit.creator_column end if Whodunit.updater_enabled? && column_exists?(table_name, Whodunit.updater_column) remove_column table_name, Whodunit.updater_column end if should_include_deleter?(include_deleter) && column_exists?(table_name, Whodunit.deleter_column) remove_column table_name, Whodunit.deleter_column end end |
#whodunit_stamps(table_def = nil, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil) ⇒ void
This method returns an undefined value.
Add stamp columns to a table definition or existing table.
This method can be used in two ways: 1. Inside a create_table block (pass table definition as first argument) 2. As a standalone method in migrations (attempts to infer table name)
Only adds columns that are enabled in configuration. For new tables, deleter column is only added when explicitly requested (include_deleter: true) to be conservative.
156 157 158 159 160 161 162 163 |
# File 'lib/whodunit/migration_helpers.rb', line 156 def whodunit_stamps(table_def = nil, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil) if table_def.nil? handle_migration_stamps(include_deleter, creator_type, updater_type, deleter_type) else handle_table_definition_stamps(table_def, include_deleter, creator_type, updater_type, deleter_type) end end |