legion-data 1.4.8 → 1.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7011c9bd3da46657e885ed8839ad6fa3537b9a06dd7dc638cf646a1c8976a7c2
4
- data.tar.gz: e1bbdc2cebfb93204dbf7ec9fcdc2b9a95703209089b832df33bb35bbe0fda9a
3
+ metadata.gz: 4ed390611755acbac0c9701c31ee783d322f0ade227aaf13b2c9c74802db412e
4
+ data.tar.gz: d2c0a758edca0c1a2add30ca3e86a3fa949fe7bd1f79d71cfb48f7f6fb97e77a
5
5
  SHA512:
6
- metadata.gz: f5a67c78d378273a6ef9e155851239acd0076c92d15b2f5c0d7b4e7bbf3a46ef0abeb6ba911b69de6d3a1e59095f9a68f2be11d117654b8536cff7fe2575a19d
7
- data.tar.gz: e186cb29ff161fe778347ac23007b1ab44ee27b7213ca1540324d680ba2c6c38cb069ebaf17d87801996aa3203c48b711422dca42368907a1d03c5fd49e809ad
6
+ metadata.gz: 3e6f7e9fe8bfb5d699b51409bf7feee684014fe90639e4ecc0cfd620d8ffff6c0004611ef1ef442406abdf003f410cc30f45d5187855c75634237ace91bc5b2f
7
+ data.tar.gz: fbca2c2acf2862fa51018c42b200c4056d2a4cf6108620a5991c878ce279b2df5945439b7397740dfef3f20ad4d56f0f41d6f7ac5bd99fc1480eecb0825200a1
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Data
5
+ module Retention
6
+ DEFAULT_RETENTION_YEARS = 7
7
+ DEFAULT_ARCHIVE_AFTER_DAYS = 90
8
+
9
+ class << self
10
+ def archive_old_records(table:, date_column: :created_at, archive_after_days: DEFAULT_ARCHIVE_AFTER_DAYS)
11
+ db = Legion::Data.connection
12
+ return { archived: 0, table: table } unless db
13
+
14
+ cutoff = Time.now - (archive_after_days * 86_400)
15
+ archive_table = archive_table_name(table)
16
+
17
+ ensure_archive_table!(db, table, archive_table)
18
+
19
+ count = 0
20
+ db.transaction do
21
+ records = db[table].where(Sequel.lit("#{date_column} < ?", cutoff))
22
+ count = records.count
23
+ if count.positive?
24
+ db[archive_table].multi_insert(records.all)
25
+ records.delete
26
+ end
27
+ end
28
+
29
+ { archived: count, table: table }
30
+ end
31
+
32
+ def purge_expired_records(table:, date_column: :created_at, retention_years: DEFAULT_RETENTION_YEARS)
33
+ db = Legion::Data.connection
34
+ archive_table = archive_table_name(table)
35
+ return { purged: 0, table: table } unless db&.table_exists?(archive_table)
36
+
37
+ cutoff = Time.now - (retention_years * 365 * 86_400)
38
+ expired = db[archive_table].where(Sequel.lit("#{date_column} < ?", cutoff))
39
+ count = expired.count
40
+ expired.delete if count.positive?
41
+
42
+ { purged: count, table: table }
43
+ end
44
+
45
+ def retention_status(table:, date_column: :created_at)
46
+ db = Legion::Data.connection
47
+ archive_table = archive_table_name(table)
48
+
49
+ active_count = db&.table_exists?(table) ? db[table].count : 0
50
+ archived_count = db&.table_exists?(archive_table) ? db[archive_table].count : 0
51
+
52
+ oldest_active = (db[table].order(Sequel.asc(date_column)).get(date_column) if db&.table_exists?(table) && active_count.positive?)
53
+
54
+ oldest_archived = (db[archive_table].order(Sequel.asc(date_column)).get(date_column) if db&.table_exists?(archive_table) && archived_count.positive?)
55
+
56
+ {
57
+ table: table,
58
+ active_count: active_count,
59
+ archived_count: archived_count,
60
+ oldest_active: oldest_active,
61
+ oldest_archived: oldest_archived
62
+ }
63
+ end
64
+
65
+ def archive_table_name(table)
66
+ :"#{table}_archive"
67
+ end
68
+
69
+ private
70
+
71
+ def ensure_archive_table!(db, source_table, archive_table)
72
+ return if db.table_exists?(archive_table)
73
+
74
+ source_schema = db.schema(source_table).to_h
75
+
76
+ db.create_table(archive_table) do
77
+ source_schema.each do |col_name, col_info|
78
+ column col_name, col_info[:db_type]
79
+ end
80
+ DateTime :archived_at, default: Sequel::CURRENT_TIMESTAMP
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Legion
4
4
  module Data
5
- VERSION = '1.4.8'
5
+ VERSION = '1.4.9'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: legion-data
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.8
4
+ version: 1.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity
@@ -147,6 +147,7 @@ files:
147
147
  - lib/legion/data/models/setting.rb
148
148
  - lib/legion/data/models/task.rb
149
149
  - lib/legion/data/models/task_log.rb
150
+ - lib/legion/data/retention.rb
150
151
  - lib/legion/data/settings.rb
151
152
  - lib/legion/data/spool.rb
152
153
  - lib/legion/data/storage_tiers.rb