Class: ROM::SQL::Gateway
- Inherits:
-
Gateway
- Object
- Gateway
- ROM::SQL::Gateway
- Includes:
- Dry::Core::Constants, Migration
- Defined in:
- lib/rom/sql/gateway.rb
Overview
SQL gateway
Constant Summary collapse
- CONNECTION_EXTENSIONS =
{ postgres: i(pg_array pg_json pg_enum pg_hstore) }.freeze
Class Attribute Summary collapse
-
.instance ⇒ Object
FIXME: get rid of this and figure out a nicer way of handling migration DSL we want to have global access ONLY when running migration tasks.
Instance Attribute Summary collapse
- #logger ⇒ Object readonly
-
#migrator ⇒ Object
included
from Migration
readonly
Returns the value of attribute migrator.
- #options ⇒ Object readonly
Instance Method Summary collapse
-
#[](name) ⇒ Dataset
Return dataset with the given name.
-
#create_table(*args, &block) ⇒ Object
Create a table using the configured connection.
-
#dataset(name) ⇒ Dataset
Return dataset with the given name.
-
#dataset?(name) ⇒ Boolean
Check if a dataset exists.
-
#disconnect ⇒ Object
Disconnect from the gateway’s database.
-
#drop_table(*args, &block) ⇒ Object
Drops a table.
-
#extend_command_class(klass, dataset) ⇒ Object
Extend the command class with database-specific behavior.
-
#initialize(uri, options = EMPTY_HASH) ⇒ SQL::Gateway
constructor
Initialize an SQL gateway.
-
#migration(&block) ⇒ Object
included
from Migration
Migration DSL.
-
#pending_migrations? ⇒ Boolean
included
from Migration
Check if there are any pending migrations.
-
#run_migrations(options = {}) ⇒ Object
included
from Migration
Run migrations.
-
#schema ⇒ Array
Returns a list of datasets inferred from table names.
-
#use_logger(logger) ⇒ Object
Setup a logger.
Constructor Details
#initialize(uri) ⇒ SQL::Gateway #initialize(uri, options) ⇒ SQL::Gateway #initialize(connection) ⇒ SQL::Gateway
Initialize an SQL gateway
Gateways are typically initialized via ROM::Configuration object, gateway constructor arguments such as URI and options are passed directly to this constructor
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rom/sql/gateway.rb', line 92 def initialize(uri, = EMPTY_HASH) @connection = connect(uri, ) load_extensions(Array([:extensions])) = super self.class.instance = self end |
Class Attribute Details
.instance ⇒ Object
FIXME: get rid of this and figure out a nicer way of handling migration DSL we want to have global access ONLY when running migration tasks
23 24 25 |
# File 'lib/rom/sql/gateway.rb', line 23 def instance @instance end |
Instance Attribute Details
#logger ⇒ Object (readonly)
34 35 36 |
# File 'lib/rom/sql/gateway.rb', line 34 def logger @logger end |
#migrator ⇒ Object (readonly) Originally defined in module Migration
Returns the value of attribute migrator.
#options ⇒ Object (readonly)
38 39 40 |
# File 'lib/rom/sql/gateway.rb', line 38 def end |
Instance Method Details
#[](name) ⇒ Dataset
Return dataset with the given name
Thsi returns a raw Sequel database
119 120 121 |
# File 'lib/rom/sql/gateway.rb', line 119 def [](name) connection[name] end |
#create_table(*args, &block) ⇒ Object
Create a table using the configured connection
192 193 194 |
# File 'lib/rom/sql/gateway.rb', line 192 def create_table(*args, &block) connection.create_table(*args, &block) end |
#dataset(name) ⇒ Dataset
Return dataset with the given name
151 152 153 |
# File 'lib/rom/sql/gateway.rb', line 151 def dataset(name) connection[name] end |
#dataset?(name) ⇒ Boolean
Check if a dataset exists
160 161 162 |
# File 'lib/rom/sql/gateway.rb', line 160 def dataset?(name) schema.include?(name) end |
#disconnect ⇒ Object
Disconnect from the gateway’s database
106 107 108 |
# File 'lib/rom/sql/gateway.rb', line 106 def disconnect connection.disconnect end |
#drop_table(*args, &block) ⇒ Object
Drops a table
199 200 201 |
# File 'lib/rom/sql/gateway.rb', line 199 def drop_table(*args, &block) connection.drop_table(*args, &block) end |
#extend_command_class(klass, dataset) ⇒ Object
Extend the command class with database-specific behavior
Note: Currently, only postgres is supported.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rom/sql/gateway.rb', line 172 def extend_command_class(klass, dataset) type = dataset.db.database_type if type == :postgres ext = if klass < Commands::Create Commands::Postgres::Create elsif klass < Commands::Update Commands::Postgres::Update end klass.send(:include, ext) if ext end klass end |
#migration(&block) ⇒ Object Originally defined in module Migration
Migration DSL
#pending_migrations? ⇒ Boolean Originally defined in module Migration
Check if there are any pending migrations
#run_migrations(options = {}) ⇒ Object Originally defined in module Migration
Run migrations
#schema ⇒ Array
Returns a list of datasets inferred from table names
208 209 210 |
# File 'lib/rom/sql/gateway.rb', line 208 def schema @schema ||= connection.tables end |
#use_logger(logger) ⇒ Object
Setup a logger
139 140 141 142 |
# File 'lib/rom/sql/gateway.rb', line 139 def use_logger(logger) @logger = logger connection.loggers << logger end |