Module: Legion::Data::Retention
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/data/retention.rb
Constant Summary collapse
- DEFAULT_RETENTION_YEARS =
7- DEFAULT_ARCHIVE_AFTER_DAYS =
90
Class Method Summary collapse
- .archive_old_records(table:, date_column: nil, archive_after_days: DEFAULT_ARCHIVE_AFTER_DAYS) ⇒ Object
- .archive_table_name(table) ⇒ Object
- .purge_expired_records(table:, date_column: nil, retention_years: DEFAULT_RETENTION_YEARS) ⇒ Object
- .retention_status(table:, date_column: nil) ⇒ Object
Methods included from Logging::Helper
Class Method Details
.archive_old_records(table:, date_column: nil, archive_after_days: DEFAULT_ARCHIVE_AFTER_DAYS) ⇒ Object
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 |
# File 'lib/legion/data/retention.rb', line 15 def archive_old_records(table:, date_column: nil, archive_after_days: DEFAULT_ARCHIVE_AFTER_DAYS) db = Legion::Data.connection return { archived: 0, table: table } unless db date_column = resolve_date_column(table, date_column) cutoff = Time.now - (archive_after_days * 86_400) archive_table = archive_table_name(table) ensure_archive_table!(db, table, archive_table) count = 0 db.transaction do records = db[table].where(Sequel.identifier(date_column) < cutoff) count = records.count if count.positive? db[archive_table].multi_insert(records.all) records.delete end end log.info "Archived #{count} row(s) from #{table}" if count.positive? { archived: count, table: table } rescue StandardError => e handle_exception( e, level: :error, handled: false, operation: :archive_old_records, table: table, date_column: date_column, archive_after_days: archive_after_days ) raise end |
.archive_table_name(table) ⇒ Object
100 101 102 |
# File 'lib/legion/data/retention.rb', line 100 def archive_table_name(table) :"#{table}_archive" end |
.purge_expired_records(table:, date_column: nil, retention_years: DEFAULT_RETENTION_YEARS) ⇒ Object
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 |
# File 'lib/legion/data/retention.rb', line 50 def purge_expired_records(table:, date_column: nil, retention_years: DEFAULT_RETENTION_YEARS) db = Legion::Data.connection archive_table = archive_table_name(table) return { purged: 0, table: table } unless db&.table_exists?(archive_table) date_column = resolve_date_column(table, date_column) cutoff = Time.now - (retention_years * 365 * 86_400) expired = db[archive_table].where(Sequel.identifier(date_column) < cutoff) count = expired.count expired.delete if count.positive? log.info "Purged #{count} expired row(s) from #{archive_table}" if count.positive? { purged: count, table: table } rescue StandardError => e handle_exception( e, level: :error, handled: false, operation: :purge_expired_records, table: table, date_column: date_column, retention_years: retention_years ) raise end |
.retention_status(table:, date_column: nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/legion/data/retention.rb', line 76 def retention_status(table:, date_column: nil) db = Legion::Data.connection archive_table = archive_table_name(table) date_column = resolve_date_column(table, date_column) active_count = db&.table_exists?(table) ? db[table].count : 0 archived_count = db&.table_exists?(archive_table) ? db[archive_table].count : 0 oldest_active = (db[table].order(Sequel.asc(date_column)).get(date_column) if db&.table_exists?(table) && active_count.positive?) oldest_archived = (db[archive_table].order(Sequel.asc(date_column)).get(date_column) if db&.table_exists?(archive_table) && archived_count.positive?) { table: table, active_count: active_count, archived_count: archived_count, oldest_active: oldest_active, oldest_archived: oldest_archived } rescue StandardError => e handle_exception(e, level: :warn, handled: false, operation: :retention_status, table: table, date_column: date_column) raise end |