Skip to content

Add pg to active_context

Dmitry Gruzd requested to merge 508641-active-context-pg-query into master

What does this MR do and why?

This MR adds PostgreSQL support to the gitlab-active-context gem by introducing a modular database adapter system. Specifically, it:

  1. Implements a base adapter architecture with three main concerns:

    • Adapter - Base interface for database adapters
    • Client - Database client abstraction
    • QueryResult - Enumerable result set wrapper
  2. Adds the first concrete implementation for PostgreSQL, including:

    • Postgresql::Adapter - Main adapter implementation
    • Postgresql::Client - PostgreSQL-specific client with PG gem integration
    • Postgresql::QueryResult - Result set implementation for PostgreSQL queries
  3. Updates the configuration system to properly handle database adapters and adds the pg gem dependency

Why is this needed?

This change provides a foundation for database integrations in the active-context system, starting with PostgreSQL support. The modular adapter design allows for:

  • Clean separation of database-specific code
  • Consistent interface across different database implementations
  • Easy addition of new database adapters in the future

Technical Details

Usage Example

docker run --rm -e "POSTGRES_PASSWORD=password" -p5432:5432 postgres
ActiveContext.configure do |config|
  config.enabled = true
  config.databases = {
    pg1: {
      adapter: 'ActiveContext::Databases::Postgresql::Adapter',
      options: {port: 5432, host: 'localhost', user: 'postgres', password: 'password'}
    }
  }
end
[6] pry(main)> ActiveContext.adapter
=> #<ActiveContext::Databases::Postgresql::Adapter:0x000000017e6d35b0
 @client=
  #<ActiveContext::Databases::Postgresql::Client:0x0000000305997958
   @conn=#<PG::Connection:0x000000017e67df70 host=localhost port=5432 user=postgres>,
   @options={:port=>5432, :host=>"localhost", :user=>"postgres", :password=>"password"}>>
[7] pry(main)> ActiveContext.adapter.search(nil).first
=> {"datid"=>"5",
 "datname"=>"postgres",
 "pid"=>"260",
 "leader_pid"=>nil,
 "usesysid"=>"10",
 "usename"=>"postgres",
 "application_name"=>"bin/rails",
 "client_addr"=>"192.168.65.1",
 "client_hostname"=>nil,
 "client_port"=>"40909",
 "backend_start"=>"2024-12-27 14:05:35.732894+00",
 "xact_start"=>"2024-12-27 14:15:22.279428+00",
 "query_start"=>"2024-12-27 14:15:22.279428+00",
 "state_change"=>"2024-12-27 14:15:22.27944+00",
 "wait_event_type"=>nil,
 "wait_event"=>nil,
 "state"=>"active",
 "backend_xid"=>nil,
 "backend_xmin"=>"750",
 "query_id"=>nil,
 "query"=>"SELECT * FROM pg_stat_activity",
 "backend_type"=>"client backend"}

References

Please include cross links to any resources that are relevant to this MR. This will give reviewers and future readers helpful context to give an efficient review of the changes introduced.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Related to #508641 (closed)

Edited by Dmitry Gruzd

Merge request reports

Loading