Commit ea792487 authored by bogdanvlviv's avatar bogdanvlviv
parent 44afe919
...@@ -11,15 +11,11 @@ class UsersNameLowerIndex < ActiveRecord::Migration[4.2] ...@@ -11,15 +11,11 @@ class UsersNameLowerIndex < ActiveRecord::Migration[4.2]
disable_ddl_transaction! disable_ddl_transaction!
def up def up
return unless Gitlab::Database.postgresql?
# On GitLab.com this produces an index with a size of roughly 60 MB. # On GitLab.com this produces an index with a size of roughly 60 MB.
execute "CREATE INDEX CONCURRENTLY #{INDEX_NAME} ON users (LOWER(name))" execute "CREATE INDEX CONCURRENTLY #{INDEX_NAME} ON users (LOWER(name))"
end end
def down def down
return unless Gitlab::Database.postgresql?
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME}" execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME}"
end end
end end
...@@ -18,7 +18,7 @@ class RescheduleCommitsCountForMergeRequestDiff < ActiveRecord::Migration[4.2] ...@@ -18,7 +18,7 @@ class RescheduleCommitsCountForMergeRequestDiff < ActiveRecord::Migration[4.2]
def up def up
say 'Populating the MergeRequestDiff `commits_count` (reschedule)' say 'Populating the MergeRequestDiff `commits_count` (reschedule)'
execute("SET statement_timeout TO '60s'") if Gitlab::Database.postgresql? execute("SET statement_timeout TO '60s'")
MergeRequestDiff.where(commits_count: nil).each_batch(of: BATCH_SIZE) do |relation, index| MergeRequestDiff.where(commits_count: nil).each_batch(of: BATCH_SIZE) do |relation, index|
start_id, end_id = relation.pluck('MIN(id), MAX(id)').first start_id, end_id = relation.pluck('MIN(id), MAX(id)').first
......
...@@ -11,16 +11,12 @@ class ProjectNameLowerIndex < ActiveRecord::Migration[4.2] ...@@ -11,16 +11,12 @@ class ProjectNameLowerIndex < ActiveRecord::Migration[4.2]
disable_ddl_transaction! disable_ddl_transaction!
def up def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout do disable_statement_timeout do
execute "CREATE INDEX CONCURRENTLY #{INDEX_NAME} ON projects (LOWER(name))" execute "CREATE INDEX CONCURRENTLY #{INDEX_NAME} ON projects (LOWER(name))"
end end
end end
def down def down
return unless Gitlab::Database.postgresql?
disable_statement_timeout do disable_statement_timeout do
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME}" execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME}"
end end
......
...@@ -5,34 +5,20 @@ class AddNotNullConstraintsToProjectAuthorizations < ActiveRecord::Migration[4.2 ...@@ -5,34 +5,20 @@ class AddNotNullConstraintsToProjectAuthorizations < ActiveRecord::Migration[4.2
DOWNTIME = false DOWNTIME = false
def up def up
if Gitlab::Database.postgresql?
# One-pass version for PostgreSQL
execute <<~SQL execute <<~SQL
ALTER TABLE project_authorizations ALTER TABLE project_authorizations
ALTER COLUMN user_id SET NOT NULL, ALTER COLUMN user_id SET NOT NULL,
ALTER COLUMN project_id SET NOT NULL, ALTER COLUMN project_id SET NOT NULL,
ALTER COLUMN access_level SET NOT NULL ALTER COLUMN access_level SET NOT NULL
SQL SQL
else
change_column_null :project_authorizations, :user_id, false
change_column_null :project_authorizations, :project_id, false
change_column_null :project_authorizations, :access_level, false
end
end end
def down def down
if Gitlab::Database.postgresql?
# One-pass version for PostgreSQL
execute <<~SQL execute <<~SQL
ALTER TABLE project_authorizations ALTER TABLE project_authorizations
ALTER COLUMN user_id DROP NOT NULL, ALTER COLUMN user_id DROP NOT NULL,
ALTER COLUMN project_id DROP NOT NULL, ALTER COLUMN project_id DROP NOT NULL,
ALTER COLUMN access_level DROP NOT NULL ALTER COLUMN access_level DROP NOT NULL
SQL SQL
else
change_column_null :project_authorizations, :user_id, true
change_column_null :project_authorizations, :project_id, true
change_column_null :project_authorizations, :access_level, true
end
end end
end end
...@@ -464,15 +464,12 @@ class BackportEnterpriseSchema < ActiveRecord::Migration[5.0] ...@@ -464,15 +464,12 @@ class BackportEnterpriseSchema < ActiveRecord::Migration[5.0]
end end
def update_environments def update_environments
return unless Gitlab::Database.postgresql?
return if index_exists?(:environments, :name, name: 'index_environments_on_name_varchar_pattern_ops') return if index_exists?(:environments, :name, name: 'index_environments_on_name_varchar_pattern_ops')
execute('CREATE INDEX CONCURRENTLY index_environments_on_name_varchar_pattern_ops ON environments (name varchar_pattern_ops);') execute('CREATE INDEX CONCURRENTLY index_environments_on_name_varchar_pattern_ops ON environments (name varchar_pattern_ops);')
end end
def revert_environments def revert_environments
return unless Gitlab::Database.postgresql?
remove_concurrent_index_by_name( remove_concurrent_index_by_name(
:environments, :environments,
'index_environments_on_name_varchar_pattern_ops' 'index_environments_on_name_varchar_pattern_ops'
......
...@@ -27,8 +27,6 @@ class CompositePrimaryKeysMigration < ActiveRecord::Migration[4.2] ...@@ -27,8 +27,6 @@ class CompositePrimaryKeysMigration < ActiveRecord::Migration[4.2]
disable_ddl_transaction! disable_ddl_transaction!
def up def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout do disable_statement_timeout do
TABLES.each do |index| TABLES.each do |index|
add_primary_key(index) add_primary_key(index)
...@@ -37,8 +35,6 @@ class CompositePrimaryKeysMigration < ActiveRecord::Migration[4.2] ...@@ -37,8 +35,6 @@ class CompositePrimaryKeysMigration < ActiveRecord::Migration[4.2]
end end
def down def down
return unless Gitlab::Database.postgresql?
disable_statement_timeout do disable_statement_timeout do
TABLES.each do |index| TABLES.each do |index|
remove_primary_key(index) remove_primary_key(index)
......
...@@ -14,12 +14,10 @@ class RemovePermanentFromRedirectRoutes < ActiveRecord::Migration[4.2] ...@@ -14,12 +14,10 @@ class RemovePermanentFromRedirectRoutes < ActiveRecord::Migration[4.2]
# These indexes were created on Postgres only in: # These indexes were created on Postgres only in:
# ReworkRedirectRoutesIndexes: # ReworkRedirectRoutesIndexes:
# https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/16211 # https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/16211
if Gitlab::Database.postgresql?
disable_statement_timeout do disable_statement_timeout do
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_PERM};" execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_PERM};"
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_TEMP};" execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_TEMP};"
end end
end
remove_column(:redirect_routes, :permanent) remove_column(:redirect_routes, :permanent)
end end
...@@ -27,11 +25,9 @@ class RemovePermanentFromRedirectRoutes < ActiveRecord::Migration[4.2] ...@@ -27,11 +25,9 @@ class RemovePermanentFromRedirectRoutes < ActiveRecord::Migration[4.2]
def down def down
add_column(:redirect_routes, :permanent, :boolean) add_column(:redirect_routes, :permanent, :boolean)
if Gitlab::Database.postgresql?
disable_statement_timeout do disable_statement_timeout do
execute("CREATE INDEX CONCURRENTLY #{INDEX_NAME_PERM} ON redirect_routes (lower(path) varchar_pattern_ops) where (permanent);") execute("CREATE INDEX CONCURRENTLY #{INDEX_NAME_PERM} ON redirect_routes (lower(path) varchar_pattern_ops) where (permanent);")
execute("CREATE INDEX CONCURRENTLY #{INDEX_NAME_TEMP} ON redirect_routes (lower(path) varchar_pattern_ops) where (not permanent or permanent is null) ;") execute("CREATE INDEX CONCURRENTLY #{INDEX_NAME_TEMP} ON redirect_routes (lower(path) varchar_pattern_ops) where (not permanent or permanent is null) ;")
end end
end end
end
end end
...@@ -16,8 +16,6 @@ class AddPathIndexToRedirectRoutes < ActiveRecord::Migration[4.2] ...@@ -16,8 +16,6 @@ class AddPathIndexToRedirectRoutes < ActiveRecord::Migration[4.2]
# This same index is also added in the `ReworkRedirectRoutesIndexes` so this # This same index is also added in the `ReworkRedirectRoutesIndexes` so this
# is a no-op in most cases. # is a no-op in most cases.
def up def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout do disable_statement_timeout do
unless index_exists_by_name?(:redirect_routes, INDEX_NAME) unless index_exists_by_name?(:redirect_routes, INDEX_NAME)
execute("CREATE UNIQUE INDEX CONCURRENTLY #{INDEX_NAME} ON redirect_routes (lower(path) varchar_pattern_ops);") execute("CREATE UNIQUE INDEX CONCURRENTLY #{INDEX_NAME} ON redirect_routes (lower(path) varchar_pattern_ops);")
......
...@@ -7,13 +7,13 @@ class PopulateSiteStatistics < ActiveRecord::Migration[4.2] ...@@ -7,13 +7,13 @@ class PopulateSiteStatistics < ActiveRecord::Migration[4.2]
def up def up
transaction do transaction do
execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql? # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967 execute('SET LOCAL statement_timeout TO 0') # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967
execute("UPDATE site_statistics SET repositories_count = (SELECT COUNT(*) FROM projects)") execute("UPDATE site_statistics SET repositories_count = (SELECT COUNT(*) FROM projects)")
end end
transaction do transaction do
execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql? # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967 execute('SET LOCAL statement_timeout TO 0') # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967
execute("UPDATE site_statistics SET wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)") execute("UPDATE site_statistics SET wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)")
end end
......
...@@ -20,7 +20,7 @@ class MigrateNullWikiAccessLevels < ActiveRecord::Migration[4.2] ...@@ -20,7 +20,7 @@ class MigrateNullWikiAccessLevels < ActiveRecord::Migration[4.2]
# We need to re-count wikis as previous attempt was not considering the NULLs. # We need to re-count wikis as previous attempt was not considering the NULLs.
transaction do transaction do
execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql? # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967 execute('SET LOCAL statement_timeout TO 0') # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967
execute("UPDATE site_statistics SET wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)") execute("UPDATE site_statistics SET wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)")
end end
......
...@@ -9,13 +9,13 @@ class RecalculateSiteStatistics < ActiveRecord::Migration[4.2] ...@@ -9,13 +9,13 @@ class RecalculateSiteStatistics < ActiveRecord::Migration[4.2]
def up def up
transaction do transaction do
execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql? # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967 execute('SET LOCAL statement_timeout TO 0') # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967
execute("UPDATE site_statistics SET repositories_count = (SELECT COUNT(*) FROM projects)") execute("UPDATE site_statistics SET repositories_count = (SELECT COUNT(*) FROM projects)")
end end
transaction do transaction do
execute('SET LOCAL statement_timeout TO 0') if Gitlab::Database.postgresql? # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967 execute('SET LOCAL statement_timeout TO 0') # see https://gitlab.com/gitlab-org/gitlab-foss/issues/48967
execute("UPDATE site_statistics SET wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)") execute("UPDATE site_statistics SET wikis_count = (SELECT COUNT(*) FROM project_features WHERE wiki_access_level != 0)")
end end
......
...@@ -108,15 +108,11 @@ class AddUsersLowerUsernameEmailIndexes < ActiveRecord::Migration[4.2] ...@@ -108,15 +108,11 @@ class AddUsersLowerUsernameEmailIndexes < ActiveRecord::Migration[4.2]
disable_ddl_transaction! disable_ddl_transaction!
def up def up
return unless Gitlab::Database.postgresql?
execute 'CREATE INDEX CONCURRENTLY index_on_users_lower_username ON users (LOWER(username));' execute 'CREATE INDEX CONCURRENTLY index_on_users_lower_username ON users (LOWER(username));'
execute 'CREATE INDEX CONCURRENTLY index_on_users_lower_email ON users (LOWER(email));' execute 'CREATE INDEX CONCURRENTLY index_on_users_lower_email ON users (LOWER(email));'
end end
def down def down
return unless Gitlab::Database.postgresql?
remove_index :users, :index_on_users_lower_username remove_index :users, :index_on_users_lower_username
remove_index :users, :index_on_users_lower_email remove_index :users, :index_on_users_lower_email
end end
......
...@@ -6,22 +6,16 @@ necessary to add database (version) specific behaviour. ...@@ -6,22 +6,16 @@ necessary to add database (version) specific behaviour.
To facilitate this we have the following methods that you can use: To facilitate this we have the following methods that you can use:
- `Gitlab::Database.postgresql?`: returns `true` if PostgreSQL is being used.
You can normally just assume this is the case.
- `Gitlab::Database.version`: returns the PostgreSQL version number as a string - `Gitlab::Database.version`: returns the PostgreSQL version number as a string
in the format `X.Y.Z`. in the format `X.Y.Z`.
This allows you to write code such as: This allows you to write code such as:
```ruby ```ruby
if Gitlab::Database.postgresql? if Gitlab::Database.version.to_f >= 9.6
if Gitlab::Database.version.to_f >= 9.6
run_really_fast_query run_really_fast_query
else
run_fast_query
end
else else
run_query run_fast_query
end end
``` ```
......
...@@ -123,8 +123,6 @@ module Gitlab ...@@ -123,8 +123,6 @@ module Gitlab
end end
def add_missing_db_timezone def add_missing_db_timezone
return '' unless Gitlab::Database.postgresql?
'at time zone \'UTC\'' 'at time zone \'UTC\''
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment