Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-10-21 10:08:36 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-10-21 10:08:36 +0300
commit48aff82709769b098321c738f3444b9bdaa694c6 (patch)
treee00c7c43e2d9b603a5a6af576b1685e400410dee /spec/support/migrations_helpers
parent879f5329ee916a948223f8f43d77fba4da6cd028 (diff)
Add latest changes from gitlab-org/gitlab@13-5-stable-eev13.5.0-rc42
Diffstat (limited to 'spec/support/migrations_helpers')
-rw-r--r--spec/support/migrations_helpers/cluster_helpers.rb10
-rw-r--r--spec/support/migrations_helpers/namespaces_helper.rb2
-rw-r--r--spec/support/migrations_helpers/schema_version_finder.rb34
3 files changed, 40 insertions, 6 deletions
diff --git a/spec/support/migrations_helpers/cluster_helpers.rb b/spec/support/migrations_helpers/cluster_helpers.rb
index b54af15c29e..03104e22bcf 100644
--- a/spec/support/migrations_helpers/cluster_helpers.rb
+++ b/spec/support/migrations_helpers/cluster_helpers.rb
@@ -4,7 +4,7 @@ module MigrationHelpers
module ClusterHelpers
# Creates a list of cluster projects.
def create_cluster_project_list(quantity)
- group = namespaces_table.create(name: 'gitlab-org', path: 'gitlab-org')
+ group = namespaces_table.create!(name: 'gitlab-org', path: 'gitlab-org')
quantity.times do |id|
create_cluster_project(group, id)
@@ -25,14 +25,14 @@ module MigrationHelpers
namespace_id: group.id
)
- cluster = clusters_table.create(
+ cluster = clusters_table.create!(
name: 'test-cluster',
cluster_type: 3,
provider_type: :gcp,
platform_type: :kubernetes
)
- cluster_projects_table.create(project_id: project.id, cluster_id: cluster.id)
+ cluster_projects_table.create!(project_id: project.id, cluster_id: cluster.id)
provider_gcp_table.create!(
gcp_project_id: "test-gcp-project-#{id}",
@@ -43,7 +43,7 @@ module MigrationHelpers
zone: 'us-central1-a'
)
- platform_kubernetes_table.create(
+ platform_kubernetes_table.create!(
cluster_id: cluster.id,
api_url: 'https://kubernetes.example.com',
encrypted_token: 'a' * 40,
@@ -58,7 +58,7 @@ module MigrationHelpers
project = projects_table.find(cluster_project.project_id)
namespace = "#{project.path}-#{project.id}"
- cluster_kubernetes_namespaces_table.create(
+ cluster_kubernetes_namespaces_table.create!(
cluster_project_id: cluster_project.id,
cluster_id: cluster.id,
project_id: cluster_project.project_id,
diff --git a/spec/support/migrations_helpers/namespaces_helper.rb b/spec/support/migrations_helpers/namespaces_helper.rb
index 4ca01c87568..c62ef6a4620 100644
--- a/spec/support/migrations_helpers/namespaces_helper.rb
+++ b/spec/support/migrations_helpers/namespaces_helper.rb
@@ -3,7 +3,7 @@
module MigrationHelpers
module NamespacesHelpers
def create_namespace(name, visibility, options = {})
- table(:namespaces).create({
+ table(:namespaces).create!({
name: name,
path: name,
type: 'Group',
diff --git a/spec/support/migrations_helpers/schema_version_finder.rb b/spec/support/migrations_helpers/schema_version_finder.rb
new file mode 100644
index 00000000000..b677db7ea26
--- /dev/null
+++ b/spec/support/migrations_helpers/schema_version_finder.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+# Sometimes data migration specs require adding invalid test data in order to test
+# the migration (e.g. adding a row with null foreign key). Certain db migrations that
+# add constraints (e.g. NOT NULL constraint) prevent invalid records from being added
+# and data migration from being tested. For this reason, SchemaVersionFinder can be used
+# to find and use schema prior to specified one.
+#
+# @example
+# RSpec.describe CleanupThings, :migration, schema: MigrationHelpers::SchemaVersionFinder.migration_prior(AddNotNullConstraint) do ...
+#
+# SchemaVersionFinder returns schema version prior to the one specified, which allows to then add
+# invalid records to the database, which in return allows to properly test data migration.
+module MigrationHelpers
+ class SchemaVersionFinder
+ def self.migrations_paths
+ ActiveRecord::Migrator.migrations_paths
+ end
+
+ def self.migration_context
+ ActiveRecord::MigrationContext.new(migrations_paths, ActiveRecord::SchemaMigration)
+ end
+
+ def self.migrations
+ migration_context.migrations
+ end
+
+ def self.migration_prior(migration_klass)
+ migrations.each_cons(2) do |previous, migration|
+ break previous.version if migration.name == migration_klass.name
+ end
+ end
+ end
+end