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, auto_create_user_fk_constraints: nil) ⇒ void
Add creator/updater/deleter stamp columns to an existing table.
-
#remove_whodunit_stamps(table_name, include_deleter: :auto, auto_create_user_fk_constraints: nil, **_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, auto_create_user_fk_constraints: 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, auto_create_user_fk_constraints: 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 92 93 94 95 96 97 |
# 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, auto_create_user_fk_constraints: nil) if Whodunit.soft_delete_enabled? && !column_exists?(table_name, Whodunit.soft_delete_column) add_column table_name, Whodunit.soft_delete_column, :datetime, null: true end 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) add_whodunit_foreign_keys(table_name, include_deleter, auto_create_user_fk_constraints) end |
#remove_whodunit_stamps(table_name, include_deleter: :auto, auto_create_user_fk_constraints: nil, **_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.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/whodunit/migration_helpers.rb', line 116 def remove_whodunit_stamps(table_name, include_deleter: :auto, auto_create_user_fk_constraints: nil, **) remove_whodunit_foreign_keys(table_name, include_deleter, auto_create_user_fk_constraints) 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 return unless Whodunit.soft_delete_enabled? && column_exists?(table_name, Whodunit.soft_delete_column) remove_column table_name, Whodunit.soft_delete_column end |
#whodunit_stamps(table_def = nil, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil, auto_create_user_fk_constraints: 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, include_deleter: :auto follows the configured soft-delete column.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/whodunit/migration_helpers.rb', line 167 def whodunit_stamps(table_def = nil, include_deleter: :auto, creator_type: nil, updater_type: nil, deleter_type: nil, auto_create_user_fk_constraints: nil) if table_def.nil? handle_migration_stamps(include_deleter, creator_type, updater_type, deleter_type, auto_create_user_fk_constraints) else handle_table_definition_stamps(table_def, include_deleter, creator_type, updater_type, deleter_type, auto_create_user_fk_constraints) end end |