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) }.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
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rom/sql/gateway.rb', line 90 def initialize(uri, = EMPTY_HASH) @connection = connect(uri, ) load_extensions(Array([:extensions])) @options = 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)
32 33 34 |
# File 'lib/rom/sql/gateway.rb', line 32 def logger @logger end |
#migrator ⇒ Object (readonly) Originally defined in module Migration
Returns the value of attribute migrator.
#options ⇒ Object (readonly)
36 37 38 |
# File 'lib/rom/sql/gateway.rb', line 36 def @options end |
Instance Method Details
#[](name) ⇒ Dataset
Return dataset with the given name
Thsi returns a raw Sequel database
117 118 119 |
# File 'lib/rom/sql/gateway.rb', line 117 def [](name) connection[name] end |
#create_table(*args, &block) ⇒ Object
Create a table using the configured connection
190 191 192 |
# File 'lib/rom/sql/gateway.rb', line 190 def create_table(*args, &block) connection.create_table(*args, &block) end |
#dataset(name) ⇒ Dataset
Return dataset with the given name
149 150 151 |
# File 'lib/rom/sql/gateway.rb', line 149 def dataset(name) connection[name] end |
#dataset?(name) ⇒ Boolean
Check if a dataset exists
158 159 160 |
# File 'lib/rom/sql/gateway.rb', line 158 def dataset?(name) schema.include?(name) end |
#disconnect ⇒ Object
Disconnect from the gateway’s database
104 105 106 |
# File 'lib/rom/sql/gateway.rb', line 104 def disconnect connection.disconnect end |
#drop_table(*args, &block) ⇒ Object
Drops a table
197 198 199 |
# File 'lib/rom/sql/gateway.rb', line 197 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.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/rom/sql/gateway.rb', line 170 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
206 207 208 |
# File 'lib/rom/sql/gateway.rb', line 206 def schema @schema ||= connection.tables end |
#use_logger(logger) ⇒ Object
Setup a logger
137 138 139 140 |
# File 'lib/rom/sql/gateway.rb', line 137 def use_logger(logger) @logger = logger connection.loggers << logger end |