summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-05-22 15:59:12 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-05-22 15:59:12 +0200
commita1744c3042d7b898e86842bd82e2ee3ccfcc1a7f (patch)
tree1a89730b4a373931ab8f9abd666b627808d3ec91
parente853ec0a351ae65a4a59bda6fb6d105a2464207f (diff)
downloadgitlab-ce-poc/gb/background-migrations.tar.gz
Add resource specific migrations to pipeline modelpoc/gb/background-migrations
-rw-r--r--app/models/ci/pipeline.rb3
-rw-r--r--app/serializers/pipeline_serializer.rb5
-rw-r--r--app/workers/resource_background_migration_worker.rb10
-rw-r--r--lib/gitlab/database/migratable.rb8
-rw-r--r--spec/lib/gitlab/database/migratable_spec.rb8
-rw-r--r--spec/workers/resource_background_migration_worker_spec.rb14
6 files changed, 34 insertions, 14 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 81c30b0e077..57ea366cb8b 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -5,6 +5,9 @@ module Ci
include Importable
include AfterCommitQueue
include Presentable
+ include Gitlab::Database::Migratable
+
+ migrate 2014052201, Migration::PipelineExample
belongs_to :project
belongs_to :user
diff --git a/app/serializers/pipeline_serializer.rb b/app/serializers/pipeline_serializer.rb
index e37af63774c..7f8de8fb7ca 100644
--- a/app/serializers/pipeline_serializer.rb
+++ b/app/serializers/pipeline_serializer.rb
@@ -12,6 +12,11 @@ class PipelineSerializer < BaseSerializer
end
def represent(resource, opts = {})
+ unless resource.migrated?
+ resource.migrate!
+ return { status: 'background migration' }
+ end
+
if resource.is_a?(ActiveRecord::Relation)
resource = resource.preload([
:retryable_builds,
diff --git a/app/workers/resource_background_migration_worker.rb b/app/workers/resource_background_migration_worker.rb
index 7de49b6e50e..c2c77aff9d8 100644
--- a/app/workers/resource_background_migration_worker.rb
+++ b/app/workers/resource_background_migration_worker.rb
@@ -2,11 +2,13 @@ class ResourceBackgroundMigrationWorker
include Sidekiq::Worker
# include MigrationsQueue TODO
- def perform(resource_class, records)
- Array(records).each do |id, version|
+ def perform(resource, records)
+ Array(records).each do |id, record_version|
ActiveRecord::Base.transaction do
- resource_class.migrations(version).each do |migration|
- migration.perform(id, version, resource_class)
+ resource.constantize.tap do |model|
+ model.migrations(record_version).each do |version, migration|
+ migration.perform(id, version, model)
+ end
end
end
end
diff --git a/lib/gitlab/database/migratable.rb b/lib/gitlab/database/migratable.rb
index fafef028cb4..7697ee5121c 100644
--- a/lib/gitlab/database/migratable.rb
+++ b/lib/gitlab/database/migratable.rb
@@ -6,7 +6,11 @@ module Gitlab
NotMigratedError = Class.new(StandardError)
included do
- after_initialize do
+ after_initialize do |record|
+ if record.new_record?
+ self.schema_version = self.class.latest_schema_version
+ end
+
if self.schema_version.to_i < self.class.latest_schema_version
raise Migratable::NotMigratedError
end
@@ -44,7 +48,7 @@ module Gitlab
def migrate!
all.in_batches(of: 1000) do |relation|
ResourceBackgroundMigrationWorker
- .perform_async(self, relation.pluck(:id, :schema_version))
+ .perform_async(self.name, relation.pluck(:id, :schema_version))
end
end
diff --git a/spec/lib/gitlab/database/migratable_spec.rb b/spec/lib/gitlab/database/migratable_spec.rb
index 54f10b8ab05..7fad0044018 100644
--- a/spec/lib/gitlab/database/migratable_spec.rb
+++ b/spec/lib/gitlab/database/migratable_spec.rb
@@ -33,14 +33,18 @@ describe Gitlab::Database::Migratable do
# TODO, these tests should be changed, PoC mode only.
#
context 'when there are not migrated records' do
- before { create(:ci_empty_pipeline) }
+ let!(:pipeline) { create(:ci_empty_pipeline) }
+
+ before do
+ pipeline.update_column(:schema_version, 0)
+ end
it 'exposes a method that checks if migrations are done' do
expect(subject.migrated?).to eq false
end
it 'should raise an error when instantiated' do
- expect { subject.new }
+ expect { Ci::Pipeline.find(pipeline.id) } # PoC workaround
.to raise_error Gitlab::Database::Migratable::NotMigratedError
end
end
diff --git a/spec/workers/resource_background_migration_worker_spec.rb b/spec/workers/resource_background_migration_worker_spec.rb
index 0733c082f95..35c49cd8296 100644
--- a/spec/workers/resource_background_migration_worker_spec.rb
+++ b/spec/workers/resource_background_migration_worker_spec.rb
@@ -2,20 +2,22 @@ require 'spec_helper'
describe ResourceBackgroundMigrationWorker do
describe '#perform' do
- let(:resource_class) { double('class') }
+ let(:resource) { spy('class') }
let(:migration) { double('migration') }
let(:records) { [[1, 1234], [2, 2345]] }
before do
- allow(resource_class).to receive(:migrations)
- .and_return([migration, migration])
+ allow(resource).to receive(:migrations)
+ .and_return({ 2222 => migration, 2233 => migration })
end
it 'executes migrations for given records' do
- expect(migration).to receive(:perform).with(1).twice
- expect(migration).to receive(:perform).with(2).twice
+ expect(migration).to receive(:perform).with(1, 2222, resource).once
+ expect(migration).to receive(:perform).with(1, 2233, resource).once
+ expect(migration).to receive(:perform).with(2, 2222, resource).once
+ expect(migration).to receive(:perform).with(2, 2233, resource).once
- subject.perform(resource_class, records)
+ described_class.new.perform(resource, records)
end
end
end