Module: Legion::Data::PartitionManager
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/data/partition_manager.rb
Constant Summary collapse
- NOT_POSTGRES =
{ skipped: true, reason: 'not_postgres' }.freeze
Class Method Summary collapse
- .drop_old_partitions(table:, retention_months: 24) ⇒ Object
- .ensure_partitions(table:, months_ahead: 3) ⇒ Object
- .list_partitions(table:) ⇒ Object
Methods included from Logging::Helper
Class Method Details
.drop_old_partitions(table:, retention_months: 24) ⇒ Object
49 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/partition_manager.rb', line 49 def drop_old_partitions(table:, retention_months: 24) return NOT_POSTGRES unless postgres? cutoff = advance_months(Date.today, -retention_months) dropped = [] retained = [] partition_names_for(table).each do |part| part_date = parse_partition_date(part) next unless part_date if part_date < cutoff Legion::Data.connection.run("DROP TABLE #{part}") log.info("Dropped partition #{part}") dropped << part else retained << part end end log.info "PartitionManager drop_old_partitions table=#{table} dropped=#{dropped.size} retained=#{retained.size}" { dropped: dropped, retained: retained } rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :drop_old_partitions, table: table, retention_months: retention_months) { dropped: [], retained: [], error: e. } end |
.ensure_partitions(table:, months_ahead: 3) ⇒ Object
13 14 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 |
# File 'lib/legion/data/partition_manager.rb', line 13 def ensure_partitions(table:, months_ahead: 3) return NOT_POSTGRES unless postgres? created = [] existing = [] base = Date.today months_ahead.times do |i| target = advance_months(base, i) partition = partition_name(table, target) from_str = target.strftime('%Y-%m-%d') to_str = advance_months(target, 1).strftime('%Y-%m-%d') ddl = "CREATE TABLE IF NOT EXISTS #{partition} " \ "PARTITION OF #{table} " \ "FOR VALUES FROM ('#{from_str}') TO ('#{to_str}')" before_count = partition_names_for(table).size Legion::Data.connection.run(ddl) after_count = partition_names_for(table).size if after_count > before_count log.info("Created partition #{partition}") created << partition else existing << partition end end log.info "PartitionManager ensure_partitions table=#{table} created=#{created.size} existing=#{existing.size}" { created: created, existing: existing } rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :ensure_partitions, table: table, months_ahead: months_ahead) { created: [], existing: [], error: e. } end |
.list_partitions(table:) ⇒ 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/partition_manager.rb', line 76 def list_partitions(table:) return NOT_POSTGRES unless postgres? sql = <<~SQL SELECT c.relname AS name, pg_get_expr(c.relpartbound, c.oid) AS bound FROM pg_inherits i JOIN pg_class p ON p.oid = i.inhparent JOIN pg_class c ON c.oid = i.inhrelid WHERE p.relname = '#{table}' ORDER BY c.relname SQL partitions = Legion::Data.connection.fetch(sql).map do |row| from_val, to_val = parse_bound(row[:bound]) { name: row[:name], from: from_val, to: to_val } end log.info "PartitionManager list_partitions table=#{table} count=#{partitions.size}" partitions rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: :list_partitions, table: table) [] end |