Module: Legion::Data::Retention
- 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: :created_at, archive_after_days: DEFAULT_ARCHIVE_AFTER_DAYS) ⇒ Object
- .archive_table_name(table) ⇒ Object
- .purge_expired_records(table:, date_column: :created_at, retention_years: DEFAULT_RETENTION_YEARS) ⇒ Object
- .retention_status(table:, date_column: :created_at) ⇒ Object
Class Method Details
.archive_old_records(table:, date_column: :created_at, archive_after_days: DEFAULT_ARCHIVE_AFTER_DAYS) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/legion/data/retention.rb', line 10 def archive_old_records(table:, date_column: :created_at, archive_after_days: DEFAULT_ARCHIVE_AFTER_DAYS) db = Legion::Data.connection return { archived: 0, table: table } unless db 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.lit("#{date_column} < ?", cutoff)) count = records.count if count.positive? db[archive_table].multi_insert(records.all) records.delete end end Legion::Logging.info "Archived #{count} row(s) from #{table}" if defined?(Legion::Logging) && count.positive? { archived: count, table: table } end |
.archive_table_name(table) ⇒ Object
67 68 69 |
# File 'lib/legion/data/retention.rb', line 67 def archive_table_name(table) :"#{table}_archive" end |
.purge_expired_records(table:, date_column: :created_at, retention_years: DEFAULT_RETENTION_YEARS) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/legion/data/retention.rb', line 33 def purge_expired_records(table:, date_column: :created_at, 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) cutoff = Time.now - (retention_years * 365 * 86_400) expired = db[archive_table].where(Sequel.lit("#{date_column} < ?", cutoff)) count = expired.count expired.delete if count.positive? Legion::Logging.info "Purged #{count} expired row(s) from #{archive_table}" if defined?(Legion::Logging) && count.positive? { purged: count, table: table } end |
.retention_status(table:, date_column: :created_at) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/legion/data/retention.rb', line 47 def retention_status(table:, date_column: :created_at) db = Legion::Data.connection archive_table = archive_table_name(table) 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 } end |