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 =
"CREATE TABLE IF NOT EXISTS events (\n  sequence_number BIGSERIAL PRIMARY KEY,\n  occurred_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n  event_type TEXT NOT NULL,\n  payload JSONB NOT NULL\n)\n".freeze
CREATE_EVENT_TYPE_INDEX =
"CREATE INDEX IF NOT EXISTS idx_events_type ON events(event_type)\n".freeze
CREATE_OCCURRED_AT_INDEX =
"CREATE INDEX IF NOT EXISTS idx_events_occurred_at ON events(occurred_at)\n".freeze
CREATE_PAYLOAD_GIN_INDEX =
"CREATE INDEX IF NOT EXISTS idx_events_payload_gin ON events USING gin(payload)\n".freeze

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