Commit e2529196 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg

Revert migration dropping columns in one release

During the review of gitlab-org/gitlab-ee!5389 it was noticed that the
docs mandated ignoring a column first, and a release later dropping it.

Given this migration is nearly finished, I've reverted the changes in
one commit so this commit can be reverted itself and picked on a new
branch.
parent 6782a18d
......@@ -1020,6 +1020,7 @@ ActiveRecord::Schema.define(version: 20180419031622) do
create_table "geo_hashed_storage_migrated_events", id: :bigserial, force: :cascade do |t|
t.integer "project_id", null: false
t.text "repository_storage_name", null: false
t.text "repository_storage_path", null: false
t.text "old_disk_path", null: false
t.text "new_disk_path", null: false
t.text "old_wiki_disk_path", null: false
......@@ -1125,6 +1126,7 @@ ActiveRecord::Schema.define(version: 20180419031622) do
create_table "geo_repository_created_events", id: :bigserial, force: :cascade do |t|
t.integer "project_id", null: false
t.text "repository_storage_name", null: false
t.text "repository_storage_path", null: false
t.text "repo_path", null: false
t.text "wiki_path"
t.text "project_name", null: false
......@@ -1135,6 +1137,7 @@ ActiveRecord::Schema.define(version: 20180419031622) do
create_table "geo_repository_deleted_events", id: :bigserial, force: :cascade do |t|
t.integer "project_id", null: false
t.text "repository_storage_name", null: false
t.text "repository_storage_path", null: false
t.text "deleted_path", null: false
t.text "deleted_wiki_path"
t.text "deleted_project_name", null: false
......@@ -1145,6 +1148,7 @@ ActiveRecord::Schema.define(version: 20180419031622) do
create_table "geo_repository_renamed_events", id: :bigserial, force: :cascade do |t|
t.integer "project_id", null: false
t.text "repository_storage_name", null: false
t.text "repository_storage_path", null: false
t.text "old_path_with_namespace", null: false
t.text "new_path_with_namespace", null: false
t.text "old_wiki_path_with_namespace", null: false
......
module Geo
class HashedStorageMigratedEvent < ActiveRecord::Base
include Geo::Model
include IgnorableColumn
ignore_column :repository_storage_path
belongs_to :project
......
module Geo
class RepositoryCreatedEvent < ActiveRecord::Base
include Geo::Model
include IgnorableColumn
ignore_column :repository_storage_path
belongs_to :project
......
module Geo
class RepositoryRenamedEvent < ActiveRecord::Base
include Geo::Model
include IgnorableColumn
ignore_column :repository_storage_path
belongs_to :project
......
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class DropRepositoryStorageEventsForGeoEvents < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
BATCH_SIZE = 5_000
TABLES = %i(geo_hashed_storage_migrated_events geo_repository_created_events
geo_repository_deleted_events geo_repository_renamed_events)
def up
TABLES.each { |t| remove_column(t, :repository_storage_path) }
end
def down
TABLES.each { |t| add_column(t, :repository_storage_path, :text) }
add_shard_path
end
private
def add_shard_path
TABLES.each do |t|
min_id = 0
loop do
newest = newest_entry(t)
break unless newest
break if newest['repository_storage_path'].present?
new_batch = batch(t, min_id)
update_batch(t, new_batch)
min_id = new_batch.last.to_i
end
change_column_null(t, :repository_storage_path, true)
end
end
def newest_entry(table)
execute(
<<~SQL
SELECT id, repository_storage_path
FROM #{table}
ORDER BY id DESC
LIMIT 1;
SQL
).first
end
def batch(table, min_id)
execute(
<<~SQL
SELECT id
FROM #{table}
WHERE id > #{min_id}
ORDER BY id ASC
LIMIT #{BATCH_SIZE};
SQL
).map { |row| row['id'] }
end
def update_batch(table, ids)
execute(
<<~SQL
UPDATE #{table}
SET repository_storage_path =
CASE repository_storage_name
#{case_statements}
END
WHERE id IN (#{ids.join(',')})
SQL
)
end
def case_statements
statement = ""
Gitlab.config.repositories.storages.each do |shard, data|
statement << "WHEN '#{shard}' THEN '#{data.legacy_disk_path}'\n"
end
statement
end
end
# encoding: utf-8
require 'spec_helper'
require Rails.root.join('ee', 'db', 'post_migrate', '20180417102933_drop_repository_storage_events_for_geo_events.rb')
describe DropRepositoryStorageEventsForGeoEvents, :migration do
let(:migration) { described_class.new }
describe '#up' do
before do
migration.up
end
it 'dropped the repository_storage_path column' do
described_class::TABLES.each do |table_name|
columns = table(table_name).columns.map(&:name)
expect(columns).not_to include("repository_storage_path")
end
end
end
describe '#down' do
let(:event_name) { :geo_repository_created_event }
before do
described_class.const_set(:BATCH_SIZE, 1)
Gitlab.config.repositories.storages.each do |name, _|
described_class.execute(<<~SQL
INSERT INTO #{event_name}s (project_id, project_name, repository_storage_name, repo_path)
VALUES (1, 'mepmep', '#{name}', 'path/to/gitlab-org')
SQL
)
end
migration.down
reset_column_in_all_models
end
it 'created the repository_storage_path column' do
described_class::TABLES.each do |table_name|
columns = table(table_name).columns.map(&:name)
expect(columns).to include("repository_storage_path")
end
null_columns = described_class
.execute("SELECT COUNT(*) FROM #{event_name}s WHERE repository_storage_path IS NULL;")
.first['count']
expect(null_columns.to_i).to be(0)
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