Commit Graph

44 Commits

Author SHA1 Message Date
Douglas Barbosa Alexandre a86f48c79b
Add helper method to return a human-friendly name for database adapter 2019-03-21 19:03:26 -03:00
Ash McKenzie 26134eeece Rename pg_stat_wal_receiver_supported? DB helper
From pg_stat_wal_receiver_supported? to
postgresql_minimum_supported_version?

Also add test coverage
2019-03-14 15:25:05 +00:00
Jasper Maes 56296f1eda Remove rails4 specific code 2018-12-16 10:48:41 +01:00
Douglas Barbosa Alexandre 5f8423dd85
CE port of 'Move EE specific code from Gitbab::Database into ee' 2018-11-26 17:14:05 -02:00
gfyoung c858f70d07 Enable frozen string for lib/gitlab/*.rb 2018-10-22 07:00:50 +00:00
DJ Mountney 76cfe4f1fd Ensure the schema is loaded with post_migrations included
If doing a schema load, the post_migrations should also be marked as up,
even if SKIP_POST_DEPLOYMENT_MIGRATIONS was set, otherwise future
migration runs will be broken.
2018-09-11 18:41:14 -07:00
Yorick Peterse 2ade035eb2
Backport various EE changes to Gitlab::Database
These changes are useful for CE as well. For example, the MR
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/20720 can make
use of some of the backported methods.
2018-07-24 13:25:01 +02:00
Brett Walker fd2ea94f60 Ability to check if underlying database is read only 2018-07-06 17:07:49 +00:00
Jarka Kadlecová fa36101a7f Use data_source_exists? instead of table_exists?
Use data_source_exists? where possible instead of table_exists? in order to be Rails5 compatible
2018-06-14 08:39:43 +02:00
Yorick Peterse 63c58a6dd0
Memoize Gitlab::Database.version
This removes the need for running a database query every time we want to
check the database version.
2018-05-17 21:23:42 +02:00
Stan Hu 272783be20 Cache table_exists?('application_settings') to reduce repeated schema reloads
Closes #43355
2018-03-15 13:13:00 -07:00
Stan Hu 4acbc9410d Cache column_exists? for application settings
This is most a backport of
https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/4987/diffs but includes
other columns that use column_exists? in a way that may cause unnecessary
schema loads.
2018-03-15 12:51:39 -07:00
Stan Hu 29e39e55c3 Merge branch 'mk-add-old-attachments-to-uploads-table' into 'master'
Add old files to uploads table

See merge request gitlab-org/gitlab-ce!15270
2017-12-07 02:34:58 +00:00
Brett Walker 02d97d4621 add Gitlab::Database.replication_slots_supported? 2017-12-05 16:43:47 +00:00
Michael Kozono e5cf23dfcf Ensure consistent column order 2017-12-01 15:26:42 -08:00
Michael Kozono 3694fe0f3d Don’t quote `NOW()` for created_at column
To fix for MySQL.
2017-12-01 15:26:42 -08:00
Yorick Peterse bda30182e0
Add returning IDs to Gitlab::Database.bulk_insert
This adds the keyword argument "return_ids" to
Gitlab::Database.bulk_insert. When set to `true` (and PostgreSQL is
used) this method will return an Array of the IDs of the inserted rows,
otherwise it will return an empty Array.
2017-11-07 22:28:57 +01:00
Alejandro Rodríguez 1796936abc Handle large values on `MergeRequestDiffCommit` dates 2017-10-30 21:13:39 -03:00
Toon Claes d13669716a Create idea of read-only database
In GitLab EE, a GitLab instance can be read-only (e.g. when it's a Geo
secondary node). But in GitLab CE it also might be useful to have the
"read-only" idea around. So port it back to GitLab CE.

Also having the principle of read-only in GitLab CE would hopefully
lead to less errors introduced, doing write operations when there
aren't allowed for read-only calls.

Closes gitlab-org/gitlab-ce#37534.
2017-10-06 22:37:40 +02:00
Yorick Peterse 5eab624d3c
Improve migrations using triggers
This adds a bunch of checks to migrations that may create or drop
triggers. Dropping triggers/functions is done using "IF EXISTS" so we
don't throw an error if the object in question has already been dropped.
We now also raise a custom error (message) when the user does not have
TRIGGER privileges. This should prevent the schema from entering an
inconsistent state while also providing the user with enough information
on how to solve the problem.

The recommendation of using SUPERUSER permissions is a bit extreme but
we require this anyway (Omnibus also configures users with this
permission).

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/36633
2017-08-29 13:02:44 +02:00
Yorick Peterse aac1de46c9
Use a specialized class for querying events
This changes various controllers to use the new EventCollection class
for retrieving events. This class uses a JOIN LATERAL query on
PostgreSQL to retrieve queries in a more efficient way, while falling
back to a simpler / less efficient query for MySQL.

The EventCollection class also includes a limit on the number of events
to display to prevent malicious users from cycling through all events,
as doing so could put a lot of pressure on the database.

JOIN LATERAL is only supported on PostgreSQL starting with version 9.3.0
and as such this optimisation is only used when using PostgreSQL 9.3 or
newer.
2017-08-10 17:45:49 +02:00
Sean McGivern d4a3474b9b Fix Gitlab::Database.bulk_insert for non-UTF-8 data 2017-06-26 17:21:40 +01:00
Sean McGivern 9a73b634ab Add table for files in merge request diffs
This adds an ID-less table containing one row per file, per merge request
diff. It has a column for each attribute on Gitlab::Git::Diff that is serialised
currently, with the advantage that we can easily query the attributes of this
new table.

It does not migrate existing data, so we have fallback code when the legacy
st_diffs column is present instead. For a merge request diff to be valid, it
should have at most one of:

* Rows in this new table, with the correct merge_request_diff_id.
* A non-NULL st_diffs column.

It may have neither, if the diff is empty.
2017-06-16 18:30:01 +01:00
Tiago Botelho c41df67676 removes redundant code from database.rb 2017-04-11 20:20:19 +01:00
Yorick Peterse 9a20ff1c99
Added Gitlab::Database.config
This returns the ActiveRecord configuration for the current environment.

While CE doesn't use this very often, EE will use it in a few places for
the database load balancing code. I'm adding this to CE so we don't end
up with merge conflicts in this file.
2017-03-17 17:08:10 +01:00
Tiago Botelho 53760bb836 removes redundant code from gitlab database file 2017-03-02 20:29:32 +00:00
Yorick Peterse cf521c9576
Allow setting of a custom connection pool host
This allows you to set a custom host when calling
Gitlab::Database.create_connection_pool. This is necessary for load
balancing as in this case we want to inherit all settings except for the
hostname.
2017-02-22 15:47:53 +01:00
Yorick Peterse f802ad370e Merge branch 'create-connection-pool' into 'master'
Introduce Gitlab::Database.with_connection_pool

See merge request !9192
2017-02-14 12:05:07 +00:00
Lin Jen-Shin 03f1abfcc3 Only ensure against yield so that pool should be available
Feedback:
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9192#note_23293693
2017-02-14 13:48:13 +08:00
Lin Jen-Shin 136dc79433 Have some simple way to create connection pool 2017-02-14 00:35:25 +08:00
Yorick Peterse 581cfbae24
Don't connect in Gitlab::Database.adapter_name
We don't need to connect when requesting the name of the database
adapter. This in turn should prevent us from requesting/leaking
connections just by asking whether we're using PostgreSQL or MySQL.
2017-02-13 17:13:02 +01:00
Douwe Maan c8b63a28af Improve performance of finding last deployed environment 2017-02-06 16:12:24 -06:00
Yorick Peterse 92b2c74ce1
Refresh project authorizations using a Redis lease
When I proposed using serializable transactions I was hoping we would be
able to refresh data of individual users concurrently. Unfortunately
upon closer inspection it was revealed this was not the case. This could
result in a lot of queries failing due to serialization errors,
overloading the database in the process (given enough workers trying to
update the target table).

To work around this we're now using a Redis lease that is cancelled upon
completion. This ensures we can update the data of different users
concurrently without overloading the database.

The code will try to obtain the lease until it succeeds, waiting at
least 1 second between retries. This is necessary as we may otherwise
end up _not_ updating the data which is not an option.
2016-11-25 13:35:01 +01:00
Ahmad Sherif fd05e26618 Precalculate user's authorized projects in database
Closes #23150
2016-11-18 20:25:45 +02:00
Grzegorz Bizon e15b63b34e Fix methods visibility in gitlab database module 2016-07-19 15:12:14 +02:00
Stan Hu 7c9eba8919 Fix RangeError exceptions when referring to issues or merge requests outside of max database values
When using #XYZ in Markdown text, if XYZ exceeds the maximum value of a signed 32-bit integer, we
get an exception when the Markdown render attempts to run `where(iids: XYZ)`. Introduce a method
that will throw out out-of-bounds values.

Closes #18777
2016-06-18 13:07:38 -07:00
Z.J. van de Weg b2df118561 Random selection now also works for MySQL 2016-06-15 12:10:41 +02:00
Rémy Coutable 499bb9f305 Improve Issuable.order_labels_priority
Signed-off-by: Rémy Coutable <remy@rymai.me>
2016-06-06 11:59:49 -05:00
Connor Shea 377583a361
Enable Rubocop Casecmp Performance Cop.
Also fixes the errors caused by enabling the cop. casecmp is more performant than `.downcase` and `==`.
2016-05-10 18:23:25 -06:00
Robert Speicher 447568d15f Fix undefined method `postgresql?` during migration 2016-02-15 14:13:47 -05:00
Robert Speicher 4105f292e1 Display database type and version in Administration dashboard
Closes #12900
2016-02-02 17:59:09 -05:00
Kamil Trzcinski d5c91bb9a6 Migrate CI WebHooks and Emails to new tables 2015-12-10 16:04:08 +01:00
Douwe Maan 05cb65dc1f Fix Gitlab::Database#mysql? 2015-10-22 10:18:44 +02:00
Yorick Peterse 85c6a3743a Added methods for detecting MySQL/PostgreSQL
These two methods remove the need for manually going into
ActiveRecord::Base.connection all over the place.
2015-10-07 23:32:14 +02:00