Commit c41df676 authored by Tiago Botelho's avatar Tiago Botelho

removes redundant code from database.rb

parent 069c54a7
---
title: Turns true value and false value database methods from instance to class methods
merge_request: 10583
author:
# rubocop:disable all # rubocop:disable all
class ConvertClosedToStateInIssue < ActiveRecord::Migration class ConvertClosedToStateInIssue < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
execute "UPDATE #{table_name} SET state = 'closed' WHERE closed = #{true_value}" execute "UPDATE #{table_name} SET state = 'closed' WHERE closed = #{true_value}"
......
# rubocop:disable all # rubocop:disable all
class ConvertClosedToStateInMergeRequest < ActiveRecord::Migration class ConvertClosedToStateInMergeRequest < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
execute "UPDATE #{table_name} SET state = 'merged' WHERE closed = #{true_value} AND merged = #{true_value}" execute "UPDATE #{table_name} SET state = 'merged' WHERE closed = #{true_value} AND merged = #{true_value}"
......
# rubocop:disable all # rubocop:disable all
class ConvertClosedToStateInMilestone < ActiveRecord::Migration class ConvertClosedToStateInMilestone < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
execute "UPDATE #{table_name} SET state = 'closed' WHERE closed = #{true_value}" execute "UPDATE #{table_name} SET state = 'closed' WHERE closed = #{true_value}"
......
# rubocop:disable all # rubocop:disable all
class UserColorScheme < ActiveRecord::Migration class UserColorScheme < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
add_column :users, :color_scheme_id, :integer, null: false, default: 1 add_column :users, :color_scheme_id, :integer, null: false, default: 1
......
# rubocop:disable all # rubocop:disable all
class AddVisibilityLevelToProjects < ActiveRecord::Migration class AddVisibilityLevelToProjects < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def self.up def self.up
add_column :projects, :visibility_level, :integer, :default => 0, :null => false add_column :projects, :visibility_level, :integer, :default => 0, :null => false
......
# rubocop:disable all # rubocop:disable all
class MigrateAlreadyImportedProjects < ActiveRecord::Migration class MigrateAlreadyImportedProjects < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
execute("UPDATE projects SET import_status = 'finished' WHERE imported = #{true_value}") execute("UPDATE projects SET import_status = 'finished' WHERE imported = #{true_value}")
......
# rubocop:disable all # rubocop:disable all
class AddVisibilityLevelToSnippet < ActiveRecord::Migration class AddVisibilityLevelToSnippet < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
add_column :snippets, :visibility_level, :integer, :default => 0, :null => false add_column :snippets, :visibility_level, :integer, :default => 0, :null => false
......
# rubocop:disable all # rubocop:disable all
class MigrateCiWebHooks < ActiveRecord::Migration class MigrateCiWebHooks < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
execute( execute(
......
# rubocop:disable all # rubocop:disable all
class MigrateCiEmails < ActiveRecord::Migration class MigrateCiEmails < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
# This inserts a new service: BuildsEmailService # This inserts a new service: BuildsEmailService
......
# rubocop:disable all # rubocop:disable all
class MigrateCiSlackService < ActiveRecord::Migration class MigrateCiSlackService < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
properties_query = 'SELECT properties FROM ci_services ' \ properties_query = 'SELECT properties FROM ci_services ' \
......
# rubocop:disable all # rubocop:disable all
class MigrateCiHipChatService < ActiveRecord::Migration class MigrateCiHipChatService < ActiveRecord::Migration
include Gitlab::Database include Gitlab::Database::MigrationHelpers
def up def up
# From properties strip `hipchat_` key # From properties strip `hipchat_` key
......
class MigrateBuildEventsToPipelineEvents < ActiveRecord::Migration class MigrateBuildEventsToPipelineEvents < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
include Gitlab::Database
DOWNTIME = false DOWNTIME = false
......
...@@ -57,16 +57,16 @@ module Gitlab ...@@ -57,16 +57,16 @@ module Gitlab
postgresql? ? "RANDOM()" : "RAND()" postgresql? ? "RANDOM()" : "RAND()"
end end
def true_value def self.true_value
if Gitlab::Database.postgresql? if postgresql?
"'t'" "'t'"
else else
1 1
end end
end end
def false_value def self.false_value
if Gitlab::Database.postgresql? if postgresql?
"'f'" "'f'"
else else
0 0
......
...@@ -114,6 +114,14 @@ module Gitlab ...@@ -114,6 +114,14 @@ module Gitlab
execute('SET statement_timeout TO 0') if Database.postgresql? execute('SET statement_timeout TO 0') if Database.postgresql?
end end
def true_value
Database.true_value
end
def false_value
Database.false_value
end
# Updates the value of a column in batches. # Updates the value of a column in batches.
# #
# This method updates the table in batches of 5% of the total row count. # This method updates the table in batches of 5% of the total row count.
......
...@@ -175,6 +175,50 @@ describe Gitlab::Database::MigrationHelpers, lib: true do ...@@ -175,6 +175,50 @@ describe Gitlab::Database::MigrationHelpers, lib: true do
end end
end end
describe '#true_value' do
context 'using PostgreSQL' do
before do
expect(Gitlab::Database).to receive(:postgresql?).and_return(true)
end
it 'returns the appropriate value' do
expect(model.true_value).to eq("'t'")
end
end
context 'using MySQL' do
before do
expect(Gitlab::Database).to receive(:postgresql?).and_return(false)
end
it 'returns the appropriate value' do
expect(model.true_value).to eq(1)
end
end
end
describe '#false_value' do
context 'using PostgreSQL' do
before do
expect(Gitlab::Database).to receive(:postgresql?).and_return(true)
end
it 'returns the appropriate value' do
expect(model.false_value).to eq("'f'")
end
end
context 'using MySQL' do
before do
expect(Gitlab::Database).to receive(:postgresql?).and_return(false)
end
it 'returns the appropriate value' do
expect(model.false_value).to eq(0)
end
end
end
describe '#update_column_in_batches' do describe '#update_column_in_batches' do
before do before do
create_list(:empty_project, 5) create_list(:empty_project, 5)
......
...@@ -150,13 +150,13 @@ describe Gitlab::Database, lib: true do ...@@ -150,13 +150,13 @@ describe Gitlab::Database, lib: true do
it 'returns correct value for PostgreSQL' do it 'returns correct value for PostgreSQL' do
expect(described_class).to receive(:postgresql?).and_return(true) expect(described_class).to receive(:postgresql?).and_return(true)
expect(MigrationTest.new.true_value).to eq "'t'" expect(described_class.true_value).to eq "'t'"
end end
it 'returns correct value for MySQL' do it 'returns correct value for MySQL' do
expect(described_class).to receive(:postgresql?).and_return(false) expect(described_class).to receive(:postgresql?).and_return(false)
expect(MigrationTest.new.true_value).to eq 1 expect(described_class.true_value).to eq 1
end end
end end
...@@ -164,13 +164,13 @@ describe Gitlab::Database, lib: true do ...@@ -164,13 +164,13 @@ describe Gitlab::Database, lib: true do
it 'returns correct value for PostgreSQL' do it 'returns correct value for PostgreSQL' do
expect(described_class).to receive(:postgresql?).and_return(true) expect(described_class).to receive(:postgresql?).and_return(true)
expect(MigrationTest.new.false_value).to eq "'f'" expect(described_class.false_value).to eq "'f'"
end end
it 'returns correct value for MySQL' do it 'returns correct value for MySQL' do
expect(described_class).to receive(:postgresql?).and_return(false) expect(described_class).to receive(:postgresql?).and_return(false)
expect(MigrationTest.new.false_value).to eq 0 expect(described_class.false_value).to eq 0
end end
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