Class: Miteru::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/miteru/database.rb

Class Method Summary collapse

Class Method Details

.closeObject

Close DB connection(s)



60
61
62
63
64
# File 'lib/miteru/database.rb', line 60

def close
  return unless connected?

  ActiveRecord::Base.connection_handler.clear_active_connections!
end

.connectObject

Establish DB connection



43
44
45
46
47
48
# File 'lib/miteru/database.rb', line 43

def connect
  return if connected?

  ActiveRecord::Base.establish_connection Miteru.config.database_url.to_s
  ActiveRecord::Base.logger = Logger.new($stdout) if Miteru.development?
end

.connected?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/miteru/database.rb', line 53

def connected?
  ActiveRecord::Base.connected?
end

.migrate(direction) ⇒ Object

DB migration

Parameters:

  • direction (Symbol)


36
37
38
# File 'lib/miteru/database.rb', line 36

def migrate(direction)
  schemas.each { |schema| schema.migrate direction }
end

.with_db_connectionObject



66
67
68
69
70
71
72
73
# File 'lib/miteru/database.rb', line 66

def with_db_connection
  Miteru::Database.connect unless connected?
  yield
rescue ActiveRecord::StatementInvalid
  Miteru.logger.error("DB migration is not yet complete. Please run 'miteru db migrate'.")
ensure
  Miteru::Database.close
end