Commit Graph

386 Commits

Author SHA1 Message Date
GitLab Bot 757d2fda7e Add latest changes from gitlab-org/gitlab@master 2020-06-26 21:08:51 +00:00
GitLab Bot 2a53c24c51 Add latest changes from gitlab-org/gitlab@master 2020-06-26 15:08:45 +00:00
GitLab Bot 5d6119a1a4 Add latest changes from gitlab-org/gitlab@master 2020-06-25 00:09:26 +00:00
GitLab Bot 082b24b03b Add latest changes from gitlab-org/gitlab@master 2020-06-24 00:08:43 +00:00
GitLab Bot d7b7232142 Add latest changes from gitlab-org/gitlab@master 2020-06-18 21:09:05 +00:00
GitLab Bot 16210ba9db Add latest changes from gitlab-org/gitlab@master 2020-06-18 00:08:35 +00:00
GitLab Bot 292d054661 Add latest changes from gitlab-org/gitlab@master 2020-06-16 00:08:33 +00:00
GitLab Bot ea054ec1c3 Add latest changes from gitlab-org/gitlab@master 2020-06-09 12:08:55 +00:00
GitLab Bot 2ade98b676 Add latest changes from gitlab-org/gitlab@master 2020-06-06 00:08:18 +00:00
GitLab Bot 963838b90c Add latest changes from gitlab-org/gitlab@master 2020-06-05 09:08:05 +00:00
GitLab Bot d8b32df644 Add latest changes from gitlab-org/gitlab@master 2020-05-29 21:08:35 +00:00
GitLab Bot c4c1fc5fe7 Add latest changes from gitlab-org/gitlab@master 2020-05-15 15:08:04 +00:00
GitLab Bot 674e7e2c3d Add latest changes from gitlab-org/gitlab@master 2020-05-14 12:08:21 +00:00
GitLab Bot e689e858ed Add latest changes from gitlab-org/gitlab@master 2020-05-13 18:08:47 +00:00
GitLab Bot 742a7f35ac Add latest changes from gitlab-org/gitlab@master 2020-05-12 18:07:54 +00:00
GitLab Bot ae69a88c2a Add latest changes from gitlab-org/gitlab@master 2020-05-08 03:09:54 +00:00
GitLab Bot 21be9646a9 Add latest changes from gitlab-org/gitlab@master 2020-05-04 03:09:50 +00:00
GitLab Bot 3795b229ab Add latest changes from gitlab-org/gitlab@master 2020-05-01 00:09:59 +00:00
GitLab Bot 028bb5dda7 Add latest changes from gitlab-org/gitlab@master 2020-04-30 18:09:38 +00:00
GitLab Bot 56df7f06f1 Add latest changes from gitlab-org/gitlab@master 2020-04-28 00:09:33 +00:00
GitLab Bot 45999bfdec Add latest changes from gitlab-org/gitlab@master 2020-04-27 12:09:41 +00:00
GitLab Bot db061f4432 Add latest changes from gitlab-org/gitlab@master 2020-04-22 18:09:52 +00:00
GitLab Bot e33f87ac0f Add latest changes from gitlab-org/gitlab@master 2020-04-21 15:21:10 +00:00
GitLab Bot 602ea42669 Add latest changes from gitlab-org/gitlab@master 2020-04-13 21:09:38 +00:00
GitLab Bot 7ba5b9baba Add latest changes from gitlab-org/gitlab@master 2020-04-07 03:09:15 +00:00
GitLab Bot eaea945e03 Add latest changes from gitlab-org/gitlab@master 2020-04-06 09:09:17 +00:00
GitLab Bot c08d9c2256 Add latest changes from gitlab-org/gitlab@master 2020-04-03 15:09:56 +00:00
GitLab Bot 6044caed20 Add latest changes from gitlab-org/gitlab@master 2020-03-31 09:08:16 +00:00
GitLab Bot 6ac4a6713e Add latest changes from gitlab-org/gitlab@master 2020-03-27 09:08:28 +00:00
GitLab Bot e80e0dd64f Add latest changes from gitlab-org/gitlab@master 2020-03-26 15:08:16 +00:00
GitLab Bot 5d75b2b9a9 Add latest changes from gitlab-org/gitlab@master 2020-03-25 18:08:10 +00:00
GitLab Bot 23bc19cb73 Add latest changes from gitlab-org/gitlab@master 2020-03-25 00:08:11 +00:00
GitLab Bot c4db541c1b Add latest changes from gitlab-org/gitlab@master 2020-03-24 21:07:54 +00:00
GitLab Bot f2dfd9ee81 Add latest changes from gitlab-org/gitlab@master 2020-03-22 15:09:49 +00:00
GitLab Bot 82fa8a3d1e Add latest changes from gitlab-org/gitlab@master 2020-03-10 09:08:10 +00:00
GitLab Bot afe2b98452 Add latest changes from gitlab-org/gitlab@master 2020-03-05 15:07:52 +00:00
GitLab Bot 42263d6451 Add latest changes from gitlab-org/gitlab@master 2020-03-02 00:07:41 +00:00
GitLab Bot a6c2be7cd2 Add latest changes from gitlab-org/gitlab@master 2020-02-21 21:08:57 +00:00
GitLab Bot a53d2c37c4 Add latest changes from gitlab-org/gitlab@master 2020-02-21 09:09:01 +00:00
GitLab Bot 33795139ea Add latest changes from gitlab-org/gitlab@master 2020-02-19 18:09:10 +00:00
GitLab Bot eb1f5a3e08 Add latest changes from gitlab-org/gitlab@master 2020-02-17 18:09:00 +00:00
GitLab Bot 6168721025 Add latest changes from gitlab-org/gitlab@master 2020-02-07 00:09:12 +00:00
GitLab Bot 0434f38ef1 Add latest changes from gitlab-org/gitlab@master 2020-01-31 18:09:11 +00:00
GitLab Bot c27acb1d37 Add latest changes from gitlab-org/gitlab@master 2020-01-31 15:08:42 +00:00
GitLab Bot 536aa3a1f4 Add latest changes from gitlab-org/gitlab@master 2020-01-30 15:09:15 +00:00
GitLab Bot 6b9d3a4e83 Add latest changes from gitlab-org/gitlab@master 2020-01-29 18:08:47 +00:00
GitLab Bot 23288f62da Add latest changes from gitlab-org/gitlab@master 2020-01-29 15:08:59 +00:00
GitLab Bot 6315ed9630 Add latest changes from gitlab-org/gitlab@master 2020-01-28 18:08:35 +00:00
GitLab Bot d65442b1d9 Add latest changes from gitlab-org/gitlab@master 2020-01-23 00:08:53 +00:00
GitLab Bot a7df1d8717 Add latest changes from gitlab-org/gitlab@master 2020-01-08 15:08:01 +00:00
GitLab Bot 015663b70f Add latest changes from gitlab-org/gitlab@master 2020-01-06 21:07:43 +00:00
GitLab Bot badb9c1dea Add latest changes from gitlab-org/gitlab@master 2019-12-17 12:08:11 +00:00
GitLab Bot 175b4fa261 Add latest changes from gitlab-org/gitlab@master 2019-12-11 18:08:10 +00:00
GitLab Bot bffcdf9bca Add latest changes from gitlab-org/gitlab@master 2019-12-02 12:06:45 +00:00
GitLab Bot 8644755363 Add latest changes from gitlab-org/gitlab@master 2019-12-01 06:06:11 +00:00
GitLab Bot 143f196f8b Add latest changes from gitlab-org/gitlab@master 2019-11-18 18:06:53 +00:00
GitLab Bot 69944ffb68 Add latest changes from gitlab-org/gitlab@master 2019-11-12 15:06:26 +00:00
GitLab Bot 4c464055fb Add latest changes from gitlab-org/gitlab@master 2019-11-05 15:06:17 +00:00
GitLab Bot 0a850868df Add latest changes from gitlab-org/gitlab@master 2019-10-09 12:06:13 +00:00
Peter Leitzen 50c647af51 Add rake task `db:obsolete_ignored_columns`
Show a list of obsolete `ignored_columns`
2019-09-11 16:23:42 +00:00
Reuben Pereira 43d33ef824 Refactor new undo_* methods
- Move code for creating a new column from old into a function so that
it can be reused.
- Also add comments above the methods.
2019-09-05 12:15:16 +00:00
dineshpanda e908e11776 Avoid calling freeze on already frozen strings in lib/gitlab 2019-09-04 09:52:02 +05:30
Reuben Pereira f855f9b815 Add helper to exactly undo cleanup_concurrent_column_rename
- Also add helper to undo rename_column_concurrently.
2019-08-25 20:14:52 +00:00
Reuben Pereira 599cc49973 Drop existing trigger before creating new one
- When renaming a column concurrently, drop any existing trigger before
attempting to create a new one.

When running migration specs multiple times (as it happens during
local development), the down method of previous migrations are called.
If any of the called methods contains a call to
rename_column_concurrently, a trigger will be created and not removed.
So, the next time a migration spec is run, if the same down method is
executed again, it will cause an error when attempting to create the
trigger (since it already exists). Dropping the trigger if it already
exists will prevent this problem.
2019-08-24 04:20:29 +00:00
Andreas Brandl 988dc80585
Further remove code branches by database type
We dropped MySQL support and a lot of mysql specific code has been
removed in https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/29608.

This comes in from the other direction and removes any `if postgresql?`
branches.
2019-07-29 12:47:06 +02:00
Andreas Brandl cb41470cdd Merge branch 'tc-revert-rails5-workaround' into 'master'
Revert "Workaround for Rails 5, where LIMIT..."

See merge request gitlab-org/gitlab-ce!30780
2019-07-25 11:03:43 +00:00
Andreas Brandl 259493bb4c Enable tablesample count strategy by default
https://gitlab.com/gitlab-org/gitlab-ce/issues/58792
2019-07-24 17:00:34 +00:00
Nick Thomas 4aa76dddec
Remove dead MySQL code
None of this code can be reached any more, so it can all be removed
2019-07-23 16:53:03 +01:00
Alex Kalderimis eda789c3c2 Improves add_timestamps_with_timezone helper
This improves the `add_timestamps_with_timezone` helper by allowing the
column names to be configured. This has the advantage that unnecessary
columns can be avoided, saving space.

A helper for removing the columns is also provided, to be used in the
`down` method of migrations.
2019-07-18 15:47:01 +00:00
Toon Claes d5080eb5ff Revert "Workaround for Rails 5, where LIMIT..."
This reverts commit 6823e7defb.

Originally implemented in
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21839, but an
error was reported in
https://gitlab.com/gitlab-org/gitlab-ce/issues/51729 resulting in a
workaround introduced in
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21879.

Now Rails 5.2 is used, this workaround no longer seems needed, so this
reverts it.
2019-07-16 14:09:11 +02:00
Heinrich Lee Yu aeb67dd489 Upgrade to Rails 5.2
Updates changed method names and fixes spec failures
2019-07-12 10:15:31 +08:00
Mayra Cabrera 0ab89d8e36 Add a rubocop for Rails.logger
Suggests to use a JSON structured log instead

Related to https://gitlab.com/gitlab-org/gitlab-ce/issues/54102
2019-07-10 19:26:47 +00:00
Małgorzata Ksionek 2ecad462a0 Add where condition to count in median class 2019-07-04 23:11:45 +02:00
Jason Goodman 96b116d099 Support jsonb Default Value in add_column_with_default Migration Helper 2019-06-28 04:48:57 +00:00
pjaspers 6f5daf49d9 Use correct arg name
So funny story, true story. I tried to run the test locally, but
didn't make it past setting up Gitaly.

Here's what I tried:

First attempt:

`git clone gitlab-ce`
`cd gitlab-ce && bundle install`
`be rspec`

This didn't work because I was missing the config/database.yml, I
didn't see a `script/bootstrap` so I looked in the readme which
redirected me to a webpage which redirected me to the
gitlab-development-kit.

Second attempt:

`gem install gitlab-development-kit`
cd gitlab-development-kit
gdk init
gdk isntall

This broke somwhere along the way because it couldn't install Gitaly
because my go version was too low. But it did clone the gitlab repo
again and this time it did have a config/database.yml.

So I tried to cd into it and `be rspec
spec/lib/gitlab/database/migration_helpers_spec.rb` which complained
about the database not being configured so I:

- Changed the socket to localhost (in the config/database.yml)
- `createdb <dev_db>` `createdb test_db`
- `be rake db:test:prepare`

Great success, it was doing things! But then failed when it came at
the Gitaly step.

Since I only want to change these three lines, at the point I gave up
and entrusted the pipeline to do its thing.

What I would have liked:

- A 'It's a Rails system, I know this' readme/docs (It's in there
  somewhere just couldn't find it)
- A way to run tests without having to use Gitaly
- Not having too install all the things for a small fix (I get why'd
  you want this, but to me it's overkill)
2019-06-19 13:55:47 +02:00
pjaspers 31438ddd8b Fix syntax coloring on emacs
The rather cryptic:

      "fk_#{Digest::SHA256.hexdigest("#{table}_#{column}_fk").first(10)}"

Was too much for emacs too handle*, since it was coming from the Rails
codebase I took their way of doing the same thing and applied it here.

I think it's easier to read and it also makes emacs render the
migration helpers pretty again.

* not true, emacs can handle anything, leave emacs alone!
2019-06-19 12:36:32 +02:00
Yorick Peterse 0f777a8d49
Allow custom names for concurrent foreign keys
This is necessary for backporting the EE schema to ensure backported
foreign keys use the same key names.
2019-06-17 17:06:20 +02:00
Lin Jen-Shin 24eff5e083 Add remove_foreign_key_if_exists 2019-05-07 15:38:43 +08:00
Heinrich Lee Yu a8a4518099 Clean up `noteable_id` for notes on commits
This was incorrectly set by a bug in:
https://gitlab.com/gitlab-org/gitlab-ce/issues/54924

Also adds a `batch_size` option to `update_column_in_batches`
2019-04-06 08:29:51 +08:00
Nick Thomas 9f05e97aad
Run rubocop -a 2019-03-13 13:42:43 +00:00
Andreas Brandl ded299c05b
Seed tablesample strategy
This produces stable approximate counts among invocations unless the
table changed.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/58791.
2019-03-12 20:03:52 +01:00
Stan Hu b35a6880b9 Fix counting of groups in admin dashboard
1. Ignore tables that use STI in reltuples count strategy.

   Models that use Rails' single-type inheritance, such as `Group` and
   `CiService`, need an additional WHERE clause to count the total
   properly, which isn't supported by the reltuples strategy.  For now,
   we just omit these from the statistics sampling and rely on the other
   strategies to get this data.

2. Fix tablesample count strategy not counting groups properly.

   Models such as `Group` needs a WHERE clause to distinguish it from
   namespaces. We now add in the WHERE clause if STI is in use.

Closes https://gitlab.com/gitlab-org/gitlab-ee/issues/7435
2019-03-11 23:50:47 -07:00
Andrew Newdigate 3288e1a874 Adds the Rubocop ReturnNil cop
This style change enforces `return if ...` instead of
`return nil if ...` to save maintainers a few minor review points
2019-03-06 17:51:56 +02:00
Jasper Maes 56296f1eda Remove rails4 specific code 2018-12-16 10:48:41 +01:00
Rubén Dávila b2cf4b9525 Backports some changes from gitlab-ee!7885 2018-12-07 08:29:53 -05:00
Jan Provaznik 3720d02b8b Use approximate counts for big tables 2018-12-06 16:07:14 +00:00
Andreas Brandl 9c059a026d
Lower the tablesample thresholds/targets.
The numbers in
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/22650#note_117645395
are quite promising with that target.
2018-12-03 21:26:54 +01:00
Andreas Brandl 474fd9138c
Move strategies in their own files
This improves readability quite a bit.
2018-12-03 21:26:53 +01:00
Andreas Brandl ff35cb45e9
Fix comment. 2018-12-03 21:26:53 +01:00
Andreas Brandl df6166f4b1
Refactor estimate query 2018-12-03 21:26:52 +01:00
Andreas Brandl 9bb7c690c4
Fix rubocop offenses. 2018-12-03 21:26:51 +01:00
Andreas Brandl 3266642739
Add feature flag for tablesample counting. 2018-12-03 21:26:51 +01:00
Andreas Brandl 3a7d9b4b02
Implement TablesampleCountStrategy.
A tablesample count executes in two phases:
* Estimate table sizes based on reltuples.
* Based on the estimate:
  * If the table is considered 'small', execute an exact relation count.
  * Otherwise, count on a sample of the table using TABLESAMPLE.

The size of the sample is chosen in a way that we always roughly scan
the same amount of rows (see TABLESAMPLE_ROW_TARGET).
2018-12-03 21:26:50 +01:00
Andreas Brandl 72347448db
More specs for fallback testing. 2018-12-03 21:26:49 +01:00
Andreas Brandl 01c7cb90da
Refactor specs to separate concerns. 2018-12-03 21:26:49 +01:00
Andreas Brandl c5fb468255
Flexible approximate counts with fallback strategies. 2018-12-03 21:26:48 +01:00
Andreas Brandl b6a530c9b1
Simplify to use models instead of tables. 2018-12-03 21:26:48 +01:00
Andreas Brandl f2ec9d2d34
Add ExactCountStrategy. 2018-12-03 21:26:48 +01:00
Andreas Brandl f3a9315177
Extract ReltuplesCountStrategy. 2018-12-03 21:26:47 +01:00
gfyoung 7e6f6e1603 Enable even more frozen string in lib/gitlab
Enables frozens string for the following:

* lib/gitlab/conflict/**/*.rb
* lib/gitlab/cross_project_access/**/*.rb
* lib/gitlab/cycle_analytics/**/*.rb
* lib/gitlab/data_builder/**/*.rb
* lib/gitlab/database/**/*.rb
* lib/gitlab/dependency_linker/**/*.rb
* lib/gitlab/diff/**/*.rb
* lib/gitlab/downtime_check/**/*.rb
* lib/gitlab/email/**/*.rb
* lib/gitlab/etag_caching/**/*.rb

Partially addresses gitlab-org/gitlab-ce#47424.
2018-11-06 22:47:32 -08:00
George Tsiolis 733ae94921 Fix typos in comments and specs 2018-11-01 08:59:20 +02:00
Andreas Brandl a84469a345 Merge branch 'mysql-migration-trigger-permissions' into 'master'
Add dbname to MySQL migration GRANT command

See merge request gitlab-org/gitlab-ce!21719
2018-10-23 12:30:14 +00:00
Dmitriy Zaporozhets ab6b448803 Merge branch 'dz-migration-index-helper' into 'master'
Add mysql_compatible_index_length to migration helpers

Closes #50253

See merge request gitlab-org/gitlab-ce!21852
2018-09-28 14:09:31 +00:00
Toon Claes 6823e7defb Work around a bug in Rails 5, where LIMIT causes trouble
The original code caused Rails to generate invalid SQL. The problem
lays in the `.arel` method in `ActiveRecord::Relation`. When there was
a `limit` on the relation, the `LIMIT` statement was taken over to
Arel, but the value wasn't.

```ruby
relation = Event.limit(2)
relation.to_sql
#=> "SELECT  `events`.* FROM `events` LIMIT 2"
relation.arel.to_sql
#=> "SELECT  `events`.* FROM `events` LIMIT ?"
```

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/51729
2018-09-25 11:49:57 +02:00
Dmitriy Zaporozhets 9ab3e6093c
Add mysql_compatible_index_length to migration helpers
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
2018-09-21 13:23:29 +03:00
Toon Claes ce830d3c60 Add Gitlab::Database::Subquery.self_join to delete_all with limit
`delete_all` doesn't support limit, so you'd need to subquery
that. And instead of subquerying with `where(id: query)`, it's better
to use an `INNER JOIN`. This method also works with MySQL, while
subquerying doesn't (without another layer of subquerying)

Reference:
https://stackoverflow.com/questions/17892762/mysql-this-version-of-mysql-doesnt-yet-support-limit-in-all-any-some-subqu/17892886#17892886
2018-09-20 16:27:09 +02:00
Yorick Peterse 8a72f5c427
Added FromUnion to easily select from a UNION
This commit adds the module `FromUnion`, which provides the class method
`from_union`. This simplifies the process of selecting data from the
result of a UNION, and reduces the likelihood of making mistakes. As a
result, instead of this:

    union = Gitlab::SQL::Union.new([foo, bar])

    Foo.from("(#{union.to_sql}) #{Foo.table_name}")

We can now write this instead:

    Foo.from_union([foo, bar])

This commit also includes some changes to make this new setup work
properly. For example, a bug in Rails 4
(https://github.com/rails/rails/issues/24193) would break the use of
`from("sub-query-here").includes(:relation)` in certain cases. There was
also a CI query which appeared to repeat a lot of conditions from an
outer query on an inner query, which isn't necessary.

Finally, we include a RuboCop cop to ensure developers use this new
module, instead of using Gitlab::SQL::Union directly.

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/51307
2018-09-17 12:39:43 +02:00
Ryan Hefner 2906e09f3a Add dbname to MySQL migration GRANT command 2018-09-13 16:27:57 +02:00
Gabriel Mazetto f21e655b61 disable_statement_timeout doesn't require any argument anymore
it will decide the method for disable statement_timeout upon
per transaction or per session, based on how it's called.

When calling with a block, block will be executed and it will use
session based statement_timeout, otherwise will default to existing
behavior.
2018-08-11 04:15:59 +02:00
Gabriel Mazetto 09e7c75d1b MigrationHelper `disable_statement_timeout` accepts `transaction: false`
By default statement_timeout will only be enabled during transaction
lifetime, therefore not leaking outside of it.

With `transaction: false` it will set for entire session, but requires
a block to passed. It yields control and cleans up session after block
finishes, also preventing leaking outside of it.
2018-08-10 20:40:00 +02:00
Yorick Peterse 91b752dce6
Respond to DB health in background migrations
This changes the BackgroundMigration worker so it checks for the health
of the DB before performing a background migration. This in turn allows
us to reduce the minimum interval, without having to worry about blowing
things up if we schedule too many migrations.

In this setup, the BackgroundMigration worker will reschedule jobs as
long as the database is considered to be in an unhealthy state. Once the
database has recovered, the migration can be performed.

To determine if the database is in a healthy state, we look at the
replication lag of any replication slots defined on the primary. If the
lag is deemed to great (100 MB by default) for too many slots, the
migration is rescheduled for a later point in time.

The health checking code is hidden behind a feature flag, allowing us to
disable it if necessary.
2018-08-06 15:20:36 +02:00
Yorick Peterse 2440b7b6f1 Merge branch 'add-rename-column-background-helper' into 'master'
Add a helper to rename a column using a background migration

Closes #47591

See merge request gitlab-org/gitlab-ce!20180
2018-06-26 14:38:52 +00:00
Sean McGivern d3d9077830 Add a helper to rename a column using a background migration
This works the same way as change_column_type_using_background_migration, but
for renaming a column. It takes a table, not a relation, to match its concurrent
counterpart.

Also, generalise the cleanup migrations to reduce code duplication.
2018-06-26 13:54:52 +01:00
Jasper Maes faf94535fb Rails5 fix arel from in mysql_median_datetime_sql 2018-06-26 00:35:47 +02:00
blackst0ne 75946b4035 [Rails5] Disable inheritance_column for Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::Namespace 2018-06-16 13:11:50 +11:00
Jasper Maes a91999e4fa Rails5 Fix arel from 2018-06-02 15:00:51 +02:00
Stan Hu b6125f7045 Fix fast admin counters not working when PostgreSQL has secondaries
This commit does a number of things:

1. Reduces the number of queries needed by perform a single query to get all
the tuples for the relevant rows.

2. Uses a transaction to query the tuple counts to ensure that the data
is retrieved from the primary.

Closes #46742
2018-05-25 22:58:43 -07:00
Stan Hu e38938b332 Fix Error 500 viewing admin page due to statement timeouts
Uses PostgreSQL tuple estimates to provide a much faster yet approximate
count. See https://wiki.postgresql.org/wiki/Slow_Counting for more details.
We only use this fast method if the table has been analyzed or vacuumed
within the last hour.

Closes #46255
2018-05-16 08:27:48 -07:00
blackst0ne 54124fc608 [Rails5] Fix Arel::UpdateManager
In Arel 7.0.0 (Arel 7.1.4 is used in Rails 5.0) the `engine` parameter
of `Arel::UpdateManager#initializer` was removed.

This commit makes the gitlab database helpers work both in rails 4 and
rails 5.
Fixes errors like this one:

```
1) Gitlab::Database::MigrationHelpers#update_column_in_batches when running outside of a transaction updates all the rows in a table
    Failure/Error:
      update_arel = Arel::UpdateManager.new(ActiveRecord::Base)
        .table(table)
        .set([[table[column], value]])
        .where(table[:id].gteq(start_id))

    ArgumentError:
      wrong number of arguments (given 1, expected 0)
    # ./lib/gitlab/database/migration_helpers.rb:317:in `new'
    # ./lib/gitlab/database/migration_helpers.rb:317:in `block in update_column_in_batches'
    # ./lib/gitlab/database/migration_helpers.rb:307:in `loop'
    # ./lib/gitlab/database/migration_helpers.rb:307:in `update_column_in_batches'
    # ./spec/lib/gitlab/database/migration_helpers_spec.rb:367:in `block (4 levels) in <top (required)>'
```
2018-04-29 12:54:58 +11:00
Zeger-Jan van de Weg ec4423665c
Gitlab::Shell works on shard name, not path
Direct disk access is done through Gitaly now, so the legacy path was
deprecated. This path was used in Gitlab::Shell however. This required
the refactoring in this commit.

Added is the removal of direct path access on the project model, as that
lookup wasn't needed anymore is most cases.

Closes https://gitlab.com/gitlab-org/gitaly/issues/1111
2018-04-25 13:36:22 +02:00
blackst0ne 20695052db [Rails5] Update `type_cast_*_database` methods 2018-04-06 10:57:19 +00:00
Douwe Maan 52967b107b
Merge branch 'jej/mattermost-notification-confidentiality-10-6' into 'security-10-6'
[10.6] Prevent notes on confidential issues from being sent to chat

See merge request gitlab/gitlabhq!2366
# Conflicts:
#	app/helpers/services_helper.rb
2018-04-05 08:41:56 +02:00
Bob Van Landuyt f2b20e0531 New migration helper for finding custom indexes
This will use the same query as `\di` to find an index on postgresql.
2018-03-28 14:12:08 +02:00
Sean McGivern bb9d360c0a Merge branch 'mark-legacy-git-access' into 'master'
Route path lookups through legacy_disk_path

See merge request gitlab-org/gitlab-ce!17743
2018-03-23 10:20:45 +00:00
Jacob Vosmaer 008a6a6ce6 Route path lookups through legacy_disk_path 2018-03-21 18:18:32 +01:00
Andreas Brandl c914883a2b Shortcut concurrent foreign key creation if already exists.
Closes #43887.
2018-03-20 18:44:00 +01:00
Andreas Brandl 1362d9fe13 Shortcut concurrent index creation/removal if no effect.
Index creation does not have an effect if the index is present already.
Index removal does not have an affect if the index is not present.

This helps to avoid patterns like this in migrations:
```
if index_exists?(...)
  remove_concurrent_index(...)
end
```
2018-03-20 18:44:00 +01:00
Andreas Brandl 5f35ea146a Fix concurrency issue with migration for user_interacted_projects table.
The concurrency issue originates from inserts on
`user_interacted_projects` from the app while running the post-deploy
migration.

This change comes with a strategy to lock the table while removing
duplicates and creating the unique index (and similar for FK
constraints).

Also, we'll have a non-unique index until the post-deploy migration is
finished to speed up queries during that time.

Closes #44205.
2018-03-14 17:51:47 +01:00
Tiago Botelho 522f4b2c6a Adapt cycle analytics spec helper and cycle analytics usage data spec 2018-03-01 16:43:26 +00:00
Tiago Botelho 335ee79a73 Refactors median code to work with both single and multiple projects 2018-02-28 10:46:20 +00:00
Tiago Botelho 4fcbcce364 Add BatchLoader as a way to refactor the base stage code 2018-02-28 10:46:20 +00:00
Tiago Botelho a7b3f11edd Adds get all medians to Cycle Analytics model 2018-02-28 10:46:19 +00:00
Yorick Peterse 15b92e7cc2
Use has_table_privilege for TRIGGER on PostgreSQL
This fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/38634.
2018-01-22 13:43:38 +01:00
Yorick Peterse 0054d3838c
Reduce UPDATEs for background column type changes
Prior to this commit we would essentially update all rows in a table,
even those where the source column (e.g. `issues.closed_at`) was NULL.
This in turn could lead to statement timeouts when using the default
batch size of 10 000 rows per job.

To work around this we don't schedule jobs for rows where the source
value is NULL. We also don't update rows where the source column is NULL
(as an extra precaution) or the target column already has a non-NULL
value. Using this approach it should be possible to update 10 000 rows
in the "issues" table in about 7.5 - 8 seconds.

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/42158
2018-01-18 14:22:41 +01:00
🙈 jacopo beschi 🙉 729f05f0e3 Adds Rubocop rule for line break around conditionals 2018-01-11 16:34:01 +00:00
Yorick Peterse 7f30bb9c29
Run background migrations with a minimum interval
This adds a minimum interval to BackgroundMigrationWorker, ensuring
background migrations of the same class only run once every 5 minutes.
This prevents a thundering herd problem where scheduled migrations all
run at once due to their delays having been expired (e.g. as the result
of a queue being paused for a long time).

If a job was recently executed it's rescheduled with a delay that equals
the remaining time of the job's lease. This means that if the lease
expires in two minutes we only need to wait two minutes, instead of
five.

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/41624
2018-01-05 16:23:25 +01:00
Yorick Peterse 78d22fb20d
Use a background migration for issues.closed_at
In a previous attempt (rolled back in
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/16021) we tried
to migrate `issues.closed_at` from timestamp to timestamptz using a
regular migration. This has a bad impact on GitLab.com and as such was
rolled back.

This commit re-implements the original migrations using generic
background migrations, allowing us to still migrate the data in a single
release but without a negative impact on availability.

To ensure the database schema is up to date the background migrations
are performed inline in development and test environments. We also make
sure to not migrate that that doesn't need migrating in the first place
or has already been migrated.
2018-01-03 12:28:00 +01:00
Lin Jen-Shin 59ac184fcf Merge remote-tracking branch 'upstream/master' into no-ivar-in-modules
* upstream/master: (671 commits)
  Make rubocop happy
  Use guard clause
  Improve language
  Prettify
  Use temp branch
  Pass info about who started the job and which job triggered it
  Docs: add indexes for monitoring and performance monitoring
  clearer-documentation-on-inline-diffs
  Add docs for commit diff discussion in merge requests
  sorting for tags api
  Clear BatchLoader after each spec to prevent holding onto records longer than necessary
  Include project in BatchLoader key to prevent returning blobs for the wrong project
  moved lfs_blob_ids method into ExtractsPath module
  Converted JS modules into exported modules
  spec fixes
  Bump gitlab-shell version to 5.10.3
  Clear caches before updating MR diffs
  Use new Ruby version 2.4 in GitLab QA images
  moved lfs blob fetch from extractspath file
  Update GitLab QA dependencies
  ...
2017-12-15 17:14:26 +08:00
Douwe Maan 1e6ca3c41e Consistently schedule Sidekiq jobs 2017-12-05 11:59:39 +01:00
Lin Jen-Shin 663593e87e Merge remote-tracking branch 'upstream/master' into no-ivar-in-modules
* upstream/master: (126 commits)
  Update VERSION to 10.3.0-pre
  Update CHANGELOG.md for 10.2.0
  default fill color for SVGs
  ignore hashed repos (for now) when using `rake gitlab:cleanup:repos`
  Use Redis cache for branch existence checks
  Update CONTRIBUTING.md: Link definition of done to criteria
  Use `make install` for Gitaly setups in non-test environments
  FileUploader should check for hashed_storage?(:attachments) to use disk_path
  Set the default gitlab-shell timeout to 3 hours
  Update composite pipelines index to include "id"
  Use arrays in Pipeline#latest_builds_with_artifacts
  Fix blank states using old css
  Skip confirmation user api
  Custom issue tracker
  Revert "check for `read_only?` first before seeing if request is disallowed"
  add `#with_metadata` scope to remove a N+1 from the notes' API
  Fix promoting milestone updating all issuables without milestone
  Batchload blobs for diff generation
  check for `read_only?` first before seeing if request is disallowed
  use `Gitlab::Routing.url_helpers` instead of `Rails.application.routes.url_helpers`
  ...
2017-11-22 17:07:01 +08:00
Lin Jen-Shin 07d3d44775 Move ModuleWithInstanceVariables to Gitlab namespace
And use .rubocop.yml to exclude paths we don't care,
rather than using the cop itself to exclude.
2017-11-22 17:06:57 +08:00
Bob Van Landuyt fa39e8a09c Don't move project repository/attachments when using hashed storage
When a project is using hashed storage, the repositories and
attachments wouldn't be saved on disk using the `full_path`. So the
migration would not do anything.

However: best to just skip moving when hashed storage is enabled.
2017-11-20 13:46:53 +01:00
Lin Jen-Shin 9ac0c76b78 Use StrongMemoize and enable/disable cops properly 2017-11-18 01:01:53 +08:00
Sean McGivern 4d367dd400 Add computed update docs for update_column_in_batches 2017-11-17 16:56:50 +00:00
Lin Jen-Shin 0af35d7e30 Merge remote-tracking branch 'upstream/master' into no-ivar-in-modules
* upstream/master: (507 commits)
  Add dropdowns documentation
  Convert migration to populate latest merge request ID into a background migration
  Set 0.69.0 instead of latest for codeclimate image
  De-duplicate background migration matchers defined in spec/support/migrations_helpers.rb
  Update database_debugging.md
  Update database_debugging.md
  Move installation of apps higher
  Change to Google Kubernetes Cluster and add internal links
  Add Ingress description from official docs
  Add info on creating your own k8s cluster from the cluster page
  Add info about the installed apps in the Cluster docs
  Resolve "lock/confidential issuable sidebar custom svg icons iteration"
  Update HA README.md to clarify GitLab support does not troubleshoot DRBD.
  Update license_finder to 3.1.1
  Make sure NotesActions#noteable returns a Noteable in the update action
  Cache the number of user SSH keys
  Adjust openid_connect_spec to use `raise_error`
  Resolve "Clicking on GPG verification badge jumps to top of the page"
  Add changelog for container repository path update
  Update container repository path reference
  ...
2017-11-17 19:19:06 +08:00
Yorick Peterse 60526a5291
Fix TRIGGER checks for MySQL
This ensures we can check if the user has TRIGGER permissions without
querying restricted tables. Thanks to Steve Norman
(https://gitlab.com/stevenorman) for helping out with this merge
request.

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/38372
2017-11-06 23:34:07 +01:00
Lin Jen-Shin 9ae92b8caa Add cop to make sure we don't use ivar in a module 2017-09-18 21:23:45 +08:00
Douwe Maan 36ad91d75a Merge branch 'mk-delete-conflicting-redirects-mysql' into 'master'
Clean up redirect routes that conflict with regular routes

Closes #36229

See merge request gitlab-org/gitlab-ce!13783
2017-09-18 08:24:52 +00:00
Yorick Peterse ac702af822
Fix setting share_with_group_lock
Prior to this commit running
Namespace#force_share_with_group_lock_on_descendants would result in
updating _all_ namespaces in the namespaces table, not just the
descendants. This is the result of ActiveRecord::Relation#update_all not
taking into account the CTE. To work around this we use the CTE query as
a sub-query instead of directly calling #update_all.

To prevent this from happening the relations returned by
Gitlab::GroupHierarchy are now marked as read-only, resulting in an
error being raised when methods such as #update_all are used.

Fortunately on GitLab.com our statement timeouts appear to have
prevented this query from actually doing any damage other than causing
a very large amount of dead tuples.

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/37916
2017-09-15 17:38:46 +02:00
Michael Kozono c923208721 Spread out the work a little 2017-09-14 14:17:23 -07:00
Michael Kozono ee4f73916f Extract helper for queuing background jobs 2017-09-14 14:17:23 -07: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
Grzegorz Bizon faf92651aa Fix invalid default argument in migration helpers 2017-08-23 08:58:55 +02:00
Grzegorz Bizon 193b199672 Add Sidekiq migration helpers for migrating queues 2017-08-22 12:47:20 +02:00
Stan Hu 718ecd4eb5 Fix Error 500s when attempting to destroy a protected tag
Due to a missing `on_delete: :cascade`, users would hit the error that
looked like:

```
PG::ForeignKeyViolation: ERROR: update or delete on table "protected_tags"
violates foreign key constraint "fk_rails_f7dfda8c51" on table
"protected_tag_create_access_levels" DETAIL: Key (id)=(1385) is still
referenced from table "protected_tag_create_access_levels". : DELETE FROM
"protected_tags" WHERE "protected_tags"."id" = 1385
```

Closes #36013
2017-08-20 03:32:31 -07:00
Grzegorz Bizon 5cd6f8c76e Add a foreign key to `merge_requests.head_pipeline_id` 2017-07-18 10:51:19 +02:00
Paul Charlton cb3b4a15e6 Support multiple Redis instances based on queue type 2017-07-11 03:35:47 +00:00
Yorick Peterse af1f6844c9
Added code for defining SHA attributes
These attributes are stored in binary in the database, but exposed as
strings. This allows one to query/create data using plain SHA1 hashes as
Strings, while storing them more efficiently as binary.
2017-06-29 15:37:37 +02:00
Bob Van Landuyt 7c53fcf11f Adjust for new static-analysis failures 2017-06-27 13:05:02 +02:00
Bob Van Landuyt 397d3fd7d0 Only do one query for updating routes 2017-06-27 12:26:52 +02:00
Bob Van Landuyt 60561aca22 Update routes directly by ID instead of filtering by path 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 66ba0b0a34 Clear the cache for projects one-by-one 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 8c850380a9 Add punctuation to log messages 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 171f2d97dc Keep failed renames in redis 2017-06-26 17:49:30 +02:00
Bob Van Landuyt d6a0c288c8 Use the migration name as a key in redis 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 229ac39a4c Don't break rolling back when a namespace or project was renamed 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 1ebb225536 More logging so we know we have the rename in redis 2017-06-26 17:49:30 +02:00
Bob Van Landuyt c98ed42d01 Revert renames from a migration 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 152cba56e4 Revert namespace renames 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 0faff42d7c Add methods to revert project renames 2017-06-26 17:49:30 +02:00
Bob Van Landuyt 3e84b6336f Track all renames in redis 2017-06-26 17:48:41 +02:00
Grzegorz Bizon 5331a49d0b Merge branch 'master' into fix/gb/improve-updating-column-in-batches-helper
* master: (56 commits)
  File view buttons
  Don't reset the session when the example failed, because we need capybara-screenshot to have access to it
  Resolve "MR comment + system note highlight don't have the same width"
  Add feature spec for dashboard state filter tabs
  Wording of Mysql support.
  a new feature checklist and more elaborate documentation requirements
  Filter archived project in API v3 only if param present
  Revert to using links instead of buttons in Issuable Index tabs.
  Do not run the codeclimate job on docs-only changes
  Only show gray footer space if environment actions exist
  Migrate Gitlab::Git::Blob.find to Gitaly
  Backport filtered search lazy token consistent state fix
  Add a comment explaining how the branch clean up happens
  Fix Github::Representation::PullRequest#source_branch_exists?
  Add CHANGELOG
  Fix GitHub importer performance on branch existence check
  Rebuild the dynamic path before validating it
  Rename stage ref migration specs to match a class name
  Enable Style/DotPosition Rubocop 👮
  Revert "Merge branch 'winh-merge-request-related-issues' into 'master'"
  ...

Conflicts:
	db/post_migrate/20170526185921_migrate_build_stage_reference.rb
2017-06-22 12:37:38 +02:00
Grzegorz Bizon 0430b76441 Enable Style/DotPosition Rubocop 👮 2017-06-21 13:48:12 +00:00
Grzegorz Bizon e4d42a62d9 Raise if updating columns in batches within a transaction 2017-06-21 14:31:49 +02:00
Yorick Peterse 9a3bea6707
Put an upper limit on update batches
When using update_column_in_batches the upper limit on the batch size is
now 1000. This ensures that for very large tables we don't lock tens of
thousands of rows during the update. This in turn should reduce the
likelyhood of running into deadlocks.
2017-06-19 14:18:41 +02:00
blackst0ne bc00806a4e Add database helpers 'add_timestamps_with_timezone' and 'timestamps_with_timezone' 2017-06-13 22:44:13 +11:00
Nick Thomas eb21f93267 Only use DROP INDEX CONCURRENTLY on postgreql 9.2+ 2017-05-25 18:12:42 +01:00
Bob Van Landuyt f62f0c258d Fixes for the rename reserved paths helpers 2017-05-18 20:08:32 +00:00
Douwe Maan beec97f6cf Disallow NULL on renamed column after default has been set 2017-05-15 16:01:09 -05:00
Rémy Coutable ae8511df13 Merge branch 'rename-column-concurrently-defaults' into 'master'
Fix adding defaults for concurrent column renames

See merge request !11335
2017-05-15 07:50:44 +00:00
James Lopez 48ffec9e74 Update rename_base.rb 2017-05-12 16:00:18 +00:00
Bob Van Landuyt 3760a7c230 Only rename namespaces and projects with an invalid path
For exact matches, not namespaces that end with an invalid path
2017-05-12 17:46:16 +02:00
Yorick Peterse 3fc20c0117
Fix adding defaults for concurrent column renames
By adding the default value _after_ adding the column we avoid updating
all rows in a table, saving a lot of time and unnecessary work in the
process.
2017-05-12 16:27:30 +02:00
Bob Van Landuyt 49fb31db41 Add a new column before creating rename triggers
MySQL doesn't allow us to create a trigger for a column that doesn't
exist yet. Failing with this error:

```
Mysql2::Error: Unknown column 'build_events' in 'NEW': CREATE TRIGGER trigger_6a80c097c862_insert
BEFORE INSERT
ON `services`
FOR EACH ROW
SET NEW.`build_events` = NEW.`job_events`
```

Creating the new column before creating the trigger avoids this.
2017-05-11 16:29:42 +02:00
Bob Van Landuyt 29f2001102 Update comments 2017-05-02 13:49:09 +02:00
Bob Van Landuyt 2e2a63c866 Rename child namespaces in migrationhelpers 2017-05-01 11:14:24 +02:00
Bob Van Landuyt b85f2fa4c7 Clear html cache for a projects milestones 2017-05-01 11:14:24 +02:00
Bob Van Landuyt ce0102d8b7 Remove dependecy on `User` 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 99a03fd6e9 Move ReservedPathsMigration into V1 namespace 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 12735eefcd Minor style adjustments 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 39efd0c030 Clear cached markdown after renaming projects 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 389057f001 Rename Projects & Namespaces based on entire paths 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 27f54bebb2 Use objects for renaming namespaces and projects 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 579d8891d5 Rename projects in a migrationhelper 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 7508ee5667 Make renaming records in the database reusable
So we can use it for projects
2017-05-01 11:14:24 +02:00
Bob Van Landuyt 58bc628d30 Rename namespace-paths in a migration helper 2017-05-01 11:14:24 +02:00
Bob Van Landuyt 2f95e6a0c4 Move `replace_sql` into `Database::MigrationHelpers` 2017-05-01 11:14:24 +02:00
Douwe Maan 3584f7dd52 Merge branch 'zero-downtime-migrations' into 'master'
Prepare for zero downtime migrations

See merge request !9976
2017-04-12 20:43:12 +00:00
Yorick Peterse 223d8a3d26
Prepare for zero downtime migrations
Starting with GitLab 9.1.0 we will no longer allow downtime migrations
unless absolutely necessary. This commit updates the various developer
guides and adds code that is necessary to make zero downtime migrations
less painful.
2017-04-12 18:15:19 +02:00
Tiago Botelho c41df67676 removes redundant code from database.rb 2017-04-11 20:20:19 +01:00