legion-data 1.4.7 → 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: 6e05799c2c985b4411c44ca60b6fb3b830825e113084c04f9d82292fde323dbd
4
- data.tar.gz: 770898dd6f1675c1688ba9b2279d37faff1e440f86aeddaaa207797e8a4277cf
3
+ metadata.gz: 4ed390611755acbac0c9701c31ee783d322f0ade227aaf13b2c9c74802db412e
4
+ data.tar.gz: d2c0a758edca0c1a2add30ca3e86a3fa949fe7bd1f79d71cfb48f7f6fb97e77a
5
5
  SHA512:
6
- metadata.gz: 4d77e460cbbe665134a2c106cb7d75be7d1dc263233343b102b38df6585f0e5bb43f5f7b5ca2463a08ce9a4d234e11fce40dbdbb53813868273aa47ff074e88a
7
- data.tar.gz: 84f820d8da69d6bcbb9e8d97a5c601cac341d5791777b0a9262a084ad8be8925a6044c8b1b00abd4d58ebb152f7982951e6c74103511001ed6479e685a7ca6e0
6
+ metadata.gz: 3e6f7e9fe8bfb5d699b51409bf7feee684014fe90639e4ecc0cfd620d8ffff6c0004611ef1ef442406abdf003f410cc30f45d5187855c75634237ace91bc5b2f
7
+ data.tar.gz: fbca2c2acf2862fa51018c42b200c4056d2a4cf6108620a5991c878ce279b2df5945439b7397740dfef3f20ad4d56f0f41d6f7ac5bd99fc1480eecb0825200a1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Legion::Data Changelog
2
2
 
3
+ ## v1.4.8
4
+
5
+ ### Fixed
6
+ - Migration 033: adds `task_delay` column (Integer, nullable) to tasks table to resolve `PG::UndefinedColumn` error when lex-tasker queries `tasks.task_delay`
7
+
3
8
  ## v1.4.7
4
9
 
5
10
  ### Added
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ Sequel.migration do
4
+ up do
5
+ next unless table_exists?(:tasks)
6
+ next if schema(:tasks).any? { |col, _| col == :task_delay }
7
+
8
+ alter_table(:tasks) do
9
+ add_column :task_delay, Integer, null: true
10
+ end
11
+ end
12
+
13
+ down do
14
+ next unless table_exists?(:tasks)
15
+ next unless schema(:tasks).any? { |col, _| col == :task_delay }
16
+
17
+ alter_table(:tasks) do
18
+ drop_column :task_delay
19
+ end
20
+ end
21
+ end
@@ -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.7'
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.7
4
+ version: 1.4.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity
@@ -128,6 +128,7 @@ files:
128
128
  - lib/legion/data/migrations/030_add_approval_queue.rb
129
129
  - lib/legion/data/migrations/031_add_task_depth.rb
130
130
  - lib/legion/data/migrations/032_add_task_cancelled_at.rb
131
+ - lib/legion/data/migrations/033_add_task_delay.rb
131
132
  - lib/legion/data/model.rb
132
133
  - lib/legion/data/models/apollo_access_log.rb
133
134
  - lib/legion/data/models/apollo_entry.rb
@@ -146,6 +147,7 @@ files:
146
147
  - lib/legion/data/models/setting.rb
147
148
  - lib/legion/data/models/task.rb
148
149
  - lib/legion/data/models/task_log.rb
150
+ - lib/legion/data/retention.rb
149
151
  - lib/legion/data/settings.rb
150
152
  - lib/legion/data/spool.rb
151
153
  - lib/legion/data/storage_tiers.rb