Module: Databases::Utilities
- Included in:
- Sequel::Postgres::Bootstrap
- Defined in:
- lib/runtime/database.rb
Overview
The Utilities module
Constant Summary collapse
- DatabaseURITemplate =
'%<schema>s://%<host>s%<port>s/%<database_name>s?' \ 'user=%<username>s&password=%<password>s&loggerLevel=%<logger_level>s'.freeze
Instance Method Summary collapse
-
#create_user(username = database) ⇒ Object
rubocop: disable Metrics/MethodLength.
-
#delete_user(username = database) ⇒ Object
rubocop: disable Metrics/MethodLength.
-
#switch_database(database_name = database) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity.
-
#switch_user(username = database, database_name = nil) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity.
Instance Method Details
#create_user(username = database) ⇒ Object
rubocop: disable Metrics/MethodLength
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/runtime/database.rb', line 367 def create_user(username = database) return false if user_exist?(username) begin log.debug "Creating user: #{username}" run "create user #{username}" run "alter role #{username} with password '#{username}'" return true rescue Sequel::DatabaseError => e log.error e.inspect return false rescue StandardError => e log.error e.inspect return false end end |
#delete_user(username = database) ⇒ Object
rubocop: disable Metrics/MethodLength
385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/runtime/database.rb', line 385 def delete_user(username = database) return false unless user_exist?(username) begin log.debug "Dropping user: #{username}" run "reassign owned by #{username} to postgres" run "drop user #{username}" rescue Sequel::DatabaseError => e log.error e.inspect return false rescue StandardError => e log.error e.inspect return false end end |
#switch_database(database_name = database) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/runtime/database.rb', line 312 def switch_database(database_name = database) uri = Sequel::Model.db.opts[:uri] || '' schema = uri.to_s.scan(/^(\w+(:\w+)?):\/\//).first&.first || 'jdbc:postgresql' host = uri.to_s.scan(/:\/\/([\w.\d]+)/).first&.first || localhost port = uri.to_s.scan(/:\/\/\w+(:(\d+))?/).first&.first username = database_name || uri.to_s.scan(/user=([^&]+)/).first password = uri.to_s.scan(/password=([^&]+)/).first&.first || username logger_level = 'OFF' url = format(DatabaseURITemplate, schema: schema, host: host, port: port.nil? || port.empty? ? '' : ':' + port.to_s, database_name: database_name, username: username, password: password, logger_level: logger_level ) reconnect_database(url) return true end |
#switch_user(username = database, database_name = nil) ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/MethodLength rubocop: disable Metrics/PerceivedComplexity
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/runtime/database.rb', line 341 def switch_user(username = database, database_name = nil) uri = Sequel::Model.db.opts[:uri] || '' schema = uri.to_s.scan(/^(\w+(:\w+)?):\/\//).first&.first || 'jdbc:postgresql' host = uri.to_s.scan(/:\/\/([\w.\d]+)/).first&.first || localhost port = uri.to_s.scan(/:\/\/\w+(:(\d+))?/).first&.first database_name = database_name || uri.to_s.scan(/\/(\w+)\??(\w+=\w+)*$/).first || database password = uri.to_s.scan(/password=([^&]+)/).first&.first || username logger_level = 'OFF' url = format(DatabaseURITemplate, schema: schema, host: host, port: port.nil? || port.empty? ? '' : ':' + port.to_s, database_name: database_name, username: username, password: password, logger_level: logger_level ) reconnect_database(url) return true end |