For one of my applications, I recently created a status report page which shows crucial information for debugging, including software versions, service connectivity and filesystem path writability. One of the items was a database version string, and the standard ActiveRecord interface does not support this concept. While version methods usually exist in the connection adapters, they are usually protected/private and have different naming conventions.
As the application can be run against multiple backends as well as on MRI or JRuby, I needed to check what adapter was in use, then query the database:
adapter = ActiveRecord::Base.connection.adapter_name sql = case adapter when 'MSSQL' 'SELECT @@VERSION' when 'MySQL', 'Mysql2', 'PostgreSQL' 'SELECT VERSION()' when 'OracleEnhanced' 'SELECT * FROM V$VERSION' when 'SQLite' 'SELECT SQLITE_VERSION()' end ActiveRecord::Base.connection.select_value(sql)
Leave a Reply