Class: App::Database

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/app-database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Ищем секцию настроек Cfg.db

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/app-database.rb', line 19

def initialize
  raise ArgumentError.new("Cfg not found!") if ! defined?( ::Cfg ) || ! Cfg.db || Cfg.db.empty?
  if (! defined? @db ) || ( @db.nil? ) || ( ! @db ) || ( ! @db.test_connection )
    Log.info{ "БД #{ Cfg.db.database }." }
    Sequel.extension :pg_array, :pg_inet, :pg_json, :pg_json_ops, :pg_array, :pg_array_ops, :pg_row, :pg_hstore, :pg_json_ops
    Sequel::Model.raise_on_save_failure = false
    Sequel::Model.plugin :validation_helpers
    Sequel::Database.extension :pg_inet, :pg_json, :pg_array, :pg_range, :pg_row, :pg_enum
    counter = Cfg.app.tmout.database_start
    begin
      @db = Sequel.connect Cfg.db.to_hash
    rescue Sequel::DatabaseConnectionError => e
      Log.error{"Шо-то с базой: #{ ( e.message.dup ).force_encoding('UTF-8') }"}
      ( counter -= 1 ) > 0 ? ( sleep(1); retry ) : raise
    end
    Sequel::Model.db = @db
    @db.tables.each{ |t| @db.reset_primary_key_sequence t } # Настоящие герои всегда идут в обход
    @db.freeze if Cfg.env == :production
    Kernel.const_set 'Db', @db
  end
  return @db
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



15
16
17
# File 'lib/app-database.rb', line 15

def db
  @db
end