Commit Graph

286 Commits

Author SHA1 Message Date
Gabriel Mazetto f6d47d0dee Fix migration_helpers_spec 2018-08-10 20:40:00 +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
Lin Jen-Shin 4ee08b77bc Updates from `rubocop -a` 2018-07-09 21:13:08 +08: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
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 20695052db [Rails5] Update `type_cast_*_database` methods 2018-04-06 10:57:19 +00: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
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
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
Nick Thomas 6b0c6e69e1
Use hashed storage in the specs 2018-02-07 13:40:46 +00:00
Nick Thomas 93ea3234df
Use the DatabaseCleaner 'deletion' strategy instead of 'truncation' 2018-01-24 17:25:55 +00: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
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
Douwe Maan 1e6ca3c41e Consistently schedule Sidekiq jobs 2017-12-05 11:59:39 +01: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
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
Jacopo 0ce6785851 Replaces `tag: true` into `:tag` in the specs
Replaces all the explicit include metadata syntax in the specs (tag:
true) into the implicit one (:tag).
Added a cop to prevent future errors and handle autocorrection.
2017-10-07 13:57:54 +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 3366e38cc1 Fix indentation in migration helpers 2017-08-22 14:57:54 +02:00
Grzegorz Bizon 193b199672 Add Sidekiq migration helpers for migrating queues 2017-08-22 12:47:20 +02:00
Robert Speicher 72a7b30c9f Change all `:empty_project` to `:project` 2017-08-02 17:47:31 -04:00
Robert Speicher 9513bd18c4 Ensure all project factories use `:repository` trait or `:empty_project` 2017-08-01 14:51:52 -04:00
Rémy Coutable cddc5cacfb Use described_class when possible
Signed-off-by: Rémy Coutable <remy@rymai.me>
2017-07-27 14:31:53 +02:00
Rémy Coutable ddccd24c13 Remove superfluous lib: true, type: redis, service: true, models: true, services: true, no_db: true, api: true
Signed-off-by: Rémy Coutable <remy@rymai.me>
2017-07-27 14:31:53 +02: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 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 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 019b4d3465 Fix Rubocop offense in migration helpers specs 2017-06-22 13:55:35 +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 3a3dd53920 Disable transaction in specs for some migrations 2017-06-22 12:35:47 +02:00
Grzegorz Bizon 3447f97b44 Fix specs for database migration helpers with transactions 2017-06-22 12:34:16 +02:00
Grzegorz Bizon 4419289bff Fix migration specs that can not run within transaction 2017-06-21 16:21:12 +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
blackst0ne bc00806a4e Add database helpers 'add_timestamps_with_timezone' and 'timestamps_with_timezone' 2017-06-13 22:44:13 +11:00
Douwe Maan e20eb71203 Merge branch '32851-fix-postgres-9-1' into 'master'
Resolve "GitLab 9.0 to 9.2 is incompatible with PostgreSQL 9.1"

See merge request !11709
2017-05-25 19:43:17 +00:00
Nick Thomas eb21f93267 Only use DROP INDEX CONCURRENTLY on postgreql 9.2+ 2017-05-25 18:12:42 +01:00
Douwe Maan 43b1750892 Revert "Remove changes that are not absolutely necessary"
This reverts commit b0498c176f
2017-05-24 20:59:26 +00:00
Lin Jen-Shin b3095251c4 Make sure that Arel.sql would work for update_column_in_batches 2017-05-23 00:25:26 +08: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
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 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 99a03fd6e9 Move ReservedPathsMigration into V1 namespace 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 0369ef1452 Add a WIP spec for clearing the cache 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
blackst0ne 9997c58fc0 Add remove_concurrent_index to database helper 2017-04-06 09:53:57 +11:00
Yorick Peterse 79696f5b7a
Hash concurrent foreign key names similar to Rails
This was initially not implemented simply because I forgot about the
size limit of constraint names in PostgreSQL (63 bytes). Using the old
technique we can't add foreign keys for certain tables. For example,
adding a foreign key on
protected_branch_merge_access_levels.protected_branch_id would lead to
the following key name:

    fk_protected_branch_merge_access_levels_protected_branches_protected_branch_id

This key is 78 bytes long, thus violating the PostgreSQL size
requirements.

The hashing strategy is copied from Rails' foreign_key_name() method,
which unfortunately is private and subject to change without notice.
2017-02-21 15:07:02 +01:00
Yorick Peterse a97dcc077c
Add method for creating foreign keys concurrently
This method allows one to create foreign keys without blocking access to
the source table, but only on PostgreSQL.

When creating a regular foreign key the "ALTER TABLE" statement used for
this won't return until all data has been validated. This statement in
turn will acquire a lock on the source table. As a result this lock can
be held for quite a long amount of time, depending on the number of rows
and system load.

By breaking up the foreign key creation process in two steps (creation,
and validation) we can reduce the amount of locking to a minimum.
Locking is still necessary for the "ALTER TABLE" statement that adds the
constraint, but this is a fast process and so will only block access for
a few milliseconds.
2017-02-10 21:51:09 +01:00
Drew Blessing 1f399fe437 fix 2016-09-15 21:59:59 -05:00
Drew Blessing e000f02bd3 Add support for column limits in add_column_with_default 2016-09-15 21:59:58 -05:00
Stan Hu 3fd304c1a5 Fix spec Don't attempt to disable statement timeout on a MySQL DB 2016-07-14 20:55:33 -07:00
Yorick Peterse 816c453558
Don't update columns in batches in a transaction
This ensures that whatever locks are acquired aren't held onto until the
end of the transaction (= after _all_ rows have been updated). Timing
wise there's also no difference between using a transaction and not
using one.
2016-06-15 17:04:07 +02:00
Yorick Peterse 8966263e0c
Customizing of update_column_in_batches queries
By passing a block to update_column_in_batches() one can now customize
the queries executed. This in turn can be used to only update a specific
set of rows instead of simply all the rows in the table.
2016-06-15 16:44:44 +02:00
Yorick Peterse b33b7be53e
Handle NULL migration errors in migration helpers
This ensures that whenever changing the NULL constraint of a column
fails we still drop the column.
2016-06-13 11:22:58 +02:00
Felipe Artur 9264203103 change add_concurrent_index function arguments 2016-06-06 13:06:21 -03:00
Grzegorz Bizon 9e92b0eead Add specs for migration helpers and boolean values 2016-05-22 22:41:14 +02:00
Grzegorz Bizon 13bf612600 Test migration helpers using a migration class 2016-05-22 22:40:43 +02:00
Yorick Peterse 36083b4d0e
Fixed styling per Rubocop pedantics 2016-05-12 13:03:55 +02:00
Yorick Peterse e867099091
Added helper methods for database migrations
These helpers can be used to perform migrations without taking down the
entire application.

For example, the method "add_column_with_default" can be used to add a
new column with a default value without locking the entire table.
2016-05-12 12:59:40 +02:00