debian-mirror-gitlab/doc/development/verifying_database_capabilities.md

39 lines
1.4 KiB
Markdown
Raw Normal View History

2021-01-29 00:20:46 +05:30
---
stage: Enablement
group: Database
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2018-03-17 18:26:18 +05:30
# Verifying Database Capabilities
2019-10-12 21:52:04 +05:30
Sometimes certain bits of code may only work on a certain database
2018-03-17 18:26:18 +05:30
version. While we try to avoid such code as much as possible sometimes it is
2020-05-24 23:13:21 +05:30
necessary to add database (version) specific behavior.
2018-03-17 18:26:18 +05:30
To facilitate this we have the following methods that you can use:
2021-12-11 22:18:48 +05:30
- `ApplicationRecord.database.version`: returns the PostgreSQL version number as a string
2019-10-12 21:52:04 +05:30
in the format `X.Y.Z`.
2018-03-17 18:26:18 +05:30
This allows you to write code such as:
```ruby
2021-12-11 22:18:48 +05:30
if ApplicationRecord.database.version.to_f >= 11.7
2020-01-01 13:55:28 +05:30
run_really_fast_query
2018-03-17 18:26:18 +05:30
else
2020-01-01 13:55:28 +05:30
run_fast_query
2018-03-17 18:26:18 +05:30
end
```
2019-10-12 21:52:04 +05:30
## Read-only database
2018-03-17 18:26:18 +05:30
The database can be used in read-only mode. In this case we have to
make sure all GET requests don't attempt any write operations to the
database. If one of those requests wants to write to the database, it needs
to be wrapped in a `Gitlab::Database.read_only?` or `Gitlab::Database.read_write?`
guard, to make sure it doesn't for read-only databases.
We have a Rails Middleware that filters any potentially writing
operations (the CUD operations of CRUD) and prevent the user from trying
to update the database and getting a 500 error (see `Gitlab::Middleware::ReadOnly`).