class CQL::Postgres::EngineSource

Overview

Runs a CQL::Schema on an AsyncEngine's pool: models, queries, migrations, schema.transaction and schema.with_connection use the engine's fiber-safe pool, statement cache and health checks without using(engine).

SOURCE = CQL::Postgres::EngineSource.connect("postgres://app@db/app?pool_size=10")

AppDB = CQL::Schema.define(:app, "postgres://app@db/app", CQL::Adapter::Postgres, source: SOURCE) do
  table :users do
    primary :id, Int64, auto_increment: true
    text :name
  end
end

SOURCE.engine.pipeline([AppDB.query.from(:users).select(:name)]) # the engine's own API stays reachable

A connection that cannot be opened raises DB::ConnectionRefused and a saturated pool DB::PoolTimeout (the engine itself raises the underlying IO::Error and CQL::Postgres::PoolTimeout, kept as cause). crystal-db's pool raises DB::ConnectionRefused only for the first connect and DB::PoolRetryAttemptsExceeded for a lazy one, so code rescuing that class will not match here. A closed pool raises CQL::Postgres::PoolClosed. The engine does not retry connects. Closing the source closes the engine for every schema that shares it.

Defined in:

postgres/engine_source.cr

Constructors

Instance Method Summary

Instance methods inherited from class CQL::ConnectionSource

adapter : Adapter | Nil adapter, close : Nil close, database_uri(uri : String) : String database_uri, pool_stats : PoolStats pool_stats, transaction(& : DB::Transaction -> _) transaction, using_connection(& : DB::Connection -> _) using_connection

Constructor Detail

def self.connect(url : String, adapter : CQL::Adapter | Nil = nil, setup : Proc(Connection, Nil) | Nil = nil, reset : String | Nil = nil) : EngineSource #

[View source]
def self.new(engine : AsyncEngine) #

[View source]

Instance Method Detail

def adapter : CQL::Adapter | Nil #
Description copied from class CQL::ConnectionSource

The adapter this source speaks, when it is bound to one (a Schema refuses a different one).


[View source]
def close : Nil #

[View source]
def database_uri(uri : String) : String #

The URL without the engine's pool options (pool_size, max_queries, ...), for code that opens a second connection from Schema#uri through crystal-db (SchemaDump). Connection settings (sslmode, max_protocol_version, binary_params, ...) stay: that driver honours them too. It must name the same server as the engine.


[View source]
def engine : AsyncEngine #

[View source]
def pool_stats : PoolStats #

[View source]
def transaction(& : DB::Transaction -> _) #
Description copied from class CQL::ConnectionSource

Runs the block in a transaction on one connection: committed when the block returns, rolled back when it raises. DB::Rollback rolls back quietly and makes this return nil.


[View source]
def using_connection(& : DB::Connection -> _) #
Description copied from class CQL::ConnectionSource

Lends one connection to the block and takes it back afterwards.


[View source]