Module: EventStoreRuby::Postgres

Defined in:
lib/eventstore_ruby/postgres/query.rb,
lib/eventstore_ruby/postgres/insert.rb,
lib/eventstore_ruby/postgres/schema.rb,
lib/eventstore_ruby/postgres/transform.rb

Defined Under Namespace

Modules: InsertBuilder, QueryBuilder, Transform

Constant Summary collapse

CREATE_EVENTS_TABLE =
<<~SQL.freeze
  CREATE TABLE IF NOT EXISTS events (
    sequence_number BIGSERIAL PRIMARY KEY,
    occurred_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    event_type TEXT NOT NULL,
    payload JSONB NOT NULL
  )
SQL
CREATE_EVENT_TYPE_INDEX =
<<~SQL.freeze
  CREATE INDEX IF NOT EXISTS idx_events_type ON events(event_type)
SQL
CREATE_OCCURRED_AT_INDEX =
<<~SQL.freeze
  CREATE INDEX IF NOT EXISTS idx_events_occurred_at ON events(occurred_at)
SQL
CREATE_PAYLOAD_GIN_INDEX =
<<~SQL.freeze
  CREATE INDEX IF NOT EXISTS idx_events_payload_gin ON events USING gin(payload)
SQL

Class Method Summary collapse

Class Method Details

.change_database_in_connection_string(conn_str, new_db_name) ⇒ Object

Replace DB name in a postgres connection url. naive but effective.



36
37
38
39
40
# File 'lib/eventstore_ruby/postgres/schema.rb', line 36

def change_database_in_connection_string(conn_str, new_db_name)
  uri = URI.parse(conn_str)
  uri.path = "/#{new_db_name}"
  uri.to_s
end

.create_database_query(db_name) ⇒ Object



29
30
31
32
33
# File 'lib/eventstore_ruby/postgres/schema.rb', line 29

def create_database_query(db_name)
  # Safe quote identifier without needing PG C-extension helpers
  quoted = '"' + db_name.gsub('"', '""') + '"'
  "CREATE DATABASE #{quoted}"
end

.get_database_name_from_connection_string(conn_str) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/eventstore_ruby/postgres/schema.rb', line 42

def get_database_name_from_connection_string(conn_str)
  uri = URI.parse(conn_str)
  dbname = uri.path.start_with?('/') ? uri.path[1..] : uri.path
  dbname.empty? ? nil : dbname
rescue URI::InvalidURIError
  warn 'eventstore-stores-postgres-err01: Invalid connection string'
  nil
end