From 93ea3234dfaa43204c5f24d4010bbe5070d75c72 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Wed, 17 Jan 2018 11:30:25 +0000 Subject: Use the DatabaseCleaner 'deletion' strategy instead of 'truncation' --- spec/migrations/add_head_pipeline_for_each_merge_request_spec.rb | 2 +- spec/migrations/calculate_conv_dev_index_percentages_spec.rb | 2 +- spec/migrations/fix_wrongly_renamed_routes_spec.rb | 2 +- spec/migrations/migrate_issues_to_ghost_user_spec.rb | 6 +++--- .../migrate_user_activities_to_users_last_activity_on_spec.rb | 2 +- spec/migrations/migrate_user_project_view_spec.rb | 2 +- spec/migrations/remove_duplicate_mr_events_spec.rb | 2 +- spec/migrations/rename_more_reserved_project_names_spec.rb | 4 ++-- spec/migrations/rename_reserved_project_names_spec.rb | 4 ++-- spec/migrations/rename_users_with_renamed_namespace_spec.rb | 2 +- spec/migrations/update_retried_for_ci_build_spec.rb | 2 +- 11 files changed, 15 insertions(+), 15 deletions(-) (limited to 'spec/migrations') diff --git a/spec/migrations/add_head_pipeline_for_each_merge_request_spec.rb b/spec/migrations/add_head_pipeline_for_each_merge_request_spec.rb index 84c2e9f7e52..63defcb39bf 100644 --- a/spec/migrations/add_head_pipeline_for_each_merge_request_spec.rb +++ b/spec/migrations/add_head_pipeline_for_each_merge_request_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170508170547_add_head_pipeline_for_each_merge_request.rb') -describe AddHeadPipelineForEachMergeRequest, :truncate do +describe AddHeadPipelineForEachMergeRequest, :delete do include ProjectForksHelper let(:migration) { described_class.new } diff --git a/spec/migrations/calculate_conv_dev_index_percentages_spec.rb b/spec/migrations/calculate_conv_dev_index_percentages_spec.rb index 597d8eab51c..f3a46025376 100644 --- a/spec/migrations/calculate_conv_dev_index_percentages_spec.rb +++ b/spec/migrations/calculate_conv_dev_index_percentages_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170803090603_calculate_conv_dev_index_percentages.rb') -describe CalculateConvDevIndexPercentages, truncate: true do +describe CalculateConvDevIndexPercentages, :delete do let(:migration) { described_class.new } let!(:conv_dev_index) do create(:conversational_development_index_metric, diff --git a/spec/migrations/fix_wrongly_renamed_routes_spec.rb b/spec/migrations/fix_wrongly_renamed_routes_spec.rb index 78f8ab7512d..543cf55f076 100644 --- a/spec/migrations/fix_wrongly_renamed_routes_spec.rb +++ b/spec/migrations/fix_wrongly_renamed_routes_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170518231126_fix_wrongly_renamed_routes.rb') -describe FixWronglyRenamedRoutes, :truncate, :migration do +describe FixWronglyRenamedRoutes, :migration do let(:subject) { described_class.new } let(:namespaces_table) { table(:namespaces) } let(:projects_table) { table(:projects) } diff --git a/spec/migrations/migrate_issues_to_ghost_user_spec.rb b/spec/migrations/migrate_issues_to_ghost_user_spec.rb index cfd4021fbac..ff0d44e1ed2 100644 --- a/spec/migrations/migrate_issues_to_ghost_user_spec.rb +++ b/spec/migrations/migrate_issues_to_ghost_user_spec.rb @@ -8,10 +8,10 @@ describe MigrateIssuesToGhostUser, :migration do let(:users) { table(:users) } before do - projects.create!(name: 'gitlab') + project = projects.create!(name: 'gitlab') user = users.create(email: 'test@example.com') - issues.create(title: 'Issue 1', author_id: nil, project_id: 1) - issues.create(title: 'Issue 2', author_id: user.id, project_id: 1) + issues.create(title: 'Issue 1', author_id: nil, project_id: project.id) + issues.create(title: 'Issue 2', author_id: user.id, project_id: project.id) end context 'when ghost user exists' do diff --git a/spec/migrations/migrate_user_activities_to_users_last_activity_on_spec.rb b/spec/migrations/migrate_user_activities_to_users_last_activity_on_spec.rb index 063829be546..a17c9c72bde 100644 --- a/spec/migrations/migrate_user_activities_to_users_last_activity_on_spec.rb +++ b/spec/migrations/migrate_user_activities_to_users_last_activity_on_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170324160416_migrate_user_activities_to_users_last_activity_on.rb') -describe MigrateUserActivitiesToUsersLastActivityOn, :clean_gitlab_redis_shared_state, :truncate do +describe MigrateUserActivitiesToUsersLastActivityOn, :clean_gitlab_redis_shared_state, :delete do let(:migration) { described_class.new } let!(:user_active_1) { create(:user) } let!(:user_active_2) { create(:user) } diff --git a/spec/migrations/migrate_user_project_view_spec.rb b/spec/migrations/migrate_user_project_view_spec.rb index 5e16769d63a..31d16e17d7b 100644 --- a/spec/migrations/migrate_user_project_view_spec.rb +++ b/spec/migrations/migrate_user_project_view_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170406142253_migrate_user_project_view.rb') -describe MigrateUserProjectView, :truncate do +describe MigrateUserProjectView, :delete do let(:migration) { described_class.new } let!(:user) { create(:user, project_view: 'readme') } diff --git a/spec/migrations/remove_duplicate_mr_events_spec.rb b/spec/migrations/remove_duplicate_mr_events_spec.rb index e393374028f..e51872239ad 100644 --- a/spec/migrations/remove_duplicate_mr_events_spec.rb +++ b/spec/migrations/remove_duplicate_mr_events_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170815060945_remove_duplicate_mr_events.rb') -describe RemoveDuplicateMrEvents, truncate: true do +describe RemoveDuplicateMrEvents, :delete do let(:migration) { described_class.new } describe '#up' do diff --git a/spec/migrations/rename_more_reserved_project_names_spec.rb b/spec/migrations/rename_more_reserved_project_names_spec.rb index ae3a4cb9b29..75310075cc5 100644 --- a/spec/migrations/rename_more_reserved_project_names_spec.rb +++ b/spec/migrations/rename_more_reserved_project_names_spec.rb @@ -5,8 +5,8 @@ require Rails.root.join('db', 'post_migrate', '20170313133418_rename_more_reserv # This migration uses multiple threads, and thus different transactions. This # means data created in this spec may not be visible to some threads. To work -# around this we use the TRUNCATE cleaning strategy. -describe RenameMoreReservedProjectNames, truncate: true do +# around this we use the DELETE cleaning strategy. +describe RenameMoreReservedProjectNames, :delete do let(:migration) { described_class.new } let!(:project) { create(:project) } diff --git a/spec/migrations/rename_reserved_project_names_spec.rb b/spec/migrations/rename_reserved_project_names_spec.rb index 462f4c08d63..e6555b1fe6b 100644 --- a/spec/migrations/rename_reserved_project_names_spec.rb +++ b/spec/migrations/rename_reserved_project_names_spec.rb @@ -5,8 +5,8 @@ require Rails.root.join('db', 'post_migrate', '20161221153951_rename_reserved_pr # This migration uses multiple threads, and thus different transactions. This # means data created in this spec may not be visible to some threads. To work -# around this we use the TRUNCATE cleaning strategy. -describe RenameReservedProjectNames, truncate: true do +# around this we use the DELETE cleaning strategy. +describe RenameReservedProjectNames, :delete do let(:migration) { described_class.new } let!(:project) { create(:project) } diff --git a/spec/migrations/rename_users_with_renamed_namespace_spec.rb b/spec/migrations/rename_users_with_renamed_namespace_spec.rb index 1e9aab3d9a1..cbc0ebeb44d 100644 --- a/spec/migrations/rename_users_with_renamed_namespace_spec.rb +++ b/spec/migrations/rename_users_with_renamed_namespace_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170518200835_rename_users_with_renamed_namespace.rb') -describe RenameUsersWithRenamedNamespace, truncate: true do +describe RenameUsersWithRenamedNamespace, :delete do it 'renames a user that had their namespace renamed to the namespace path' do other_user = create(:user, username: 'kodingu') other_user1 = create(:user, username: 'api0') diff --git a/spec/migrations/update_retried_for_ci_build_spec.rb b/spec/migrations/update_retried_for_ci_build_spec.rb index 3742b4dafe5..ccb77766b84 100644 --- a/spec/migrations/update_retried_for_ci_build_spec.rb +++ b/spec/migrations/update_retried_for_ci_build_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170503004427_update_retried_for_ci_build.rb') -describe UpdateRetriedForCiBuild, truncate: true do +describe UpdateRetriedForCiBuild, :delete do let(:pipeline) { create(:ci_pipeline) } let!(:build_old) { create(:ci_build, pipeline: pipeline, name: 'test') } let!(:build_new) { create(:ci_build, pipeline: pipeline, name: 'test') } -- cgit v1.2.3 From b7d8098b3abcc0e13b55da0e78cca8464d921f42 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Fri, 2 Feb 2018 11:56:38 +0100 Subject: add migration spec --- .../remove_project_labels_group_id_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 spec/migrations/remove_project_labels_group_id_spec.rb (limited to 'spec/migrations') diff --git a/spec/migrations/remove_project_labels_group_id_spec.rb b/spec/migrations/remove_project_labels_group_id_spec.rb new file mode 100644 index 00000000000..ddab6cc29d3 --- /dev/null +++ b/spec/migrations/remove_project_labels_group_id_spec.rb @@ -0,0 +1,21 @@ +# encoding: utf-8 + +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20180202111106_remove_project_labels_group_id.rb') + +describe RemoveProjectLabelsGroupId, :delete do + let(:migration) { described_class.new } + let(:group) { create(:group) } + let!(:project_label) { create(:label, group_id: group.id) } + let!(:group_label) { create(:group_label) } + + describe '#up' do + it 'updates the project labels group ID' do + expect { migration.up }.to change { project_label.reload.group_id }.from(1).to(nil) + end + + it 'keeps the group labels group ID' do + expect { migration.up }.not_to change { group_label.reload.group_id } + end + end +end -- cgit v1.2.3 From ccc2d0ceb75eec2f43e70d7fa9779e0ceb20cf49 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Fri, 2 Feb 2018 13:18:16 +0100 Subject: fix spec --- spec/migrations/remove_project_labels_group_id_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/migrations') diff --git a/spec/migrations/remove_project_labels_group_id_spec.rb b/spec/migrations/remove_project_labels_group_id_spec.rb index ddab6cc29d3..d80d61af20b 100644 --- a/spec/migrations/remove_project_labels_group_id_spec.rb +++ b/spec/migrations/remove_project_labels_group_id_spec.rb @@ -11,7 +11,7 @@ describe RemoveProjectLabelsGroupId, :delete do describe '#up' do it 'updates the project labels group ID' do - expect { migration.up }.to change { project_label.reload.group_id }.from(1).to(nil) + expect { migration.up }.to change { project_label.reload.group_id }.to(nil) end it 'keeps the group labels group ID' do -- cgit v1.2.3 From d07addbf6e3841ae31a7e62ecbb29523f4d8c859 Mon Sep 17 00:00:00 2001 From: Andreas Brandl Date: Mon, 5 Feb 2018 12:26:33 +0100 Subject: Add foreign keys to todos table. Fixes #32282. --- spec/migrations/add_foreign_keys_to_todos_spec.rb | 65 +++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 spec/migrations/add_foreign_keys_to_todos_spec.rb (limited to 'spec/migrations') diff --git a/spec/migrations/add_foreign_keys_to_todos_spec.rb b/spec/migrations/add_foreign_keys_to_todos_spec.rb new file mode 100644 index 00000000000..4a22bd6f342 --- /dev/null +++ b/spec/migrations/add_foreign_keys_to_todos_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper' +require Rails.root.join('db', 'migrate', '20180201110056_add_foreign_keys_to_todos.rb') + +describe AddForeignKeysToTodos, :migration do + let(:todos) { table(:todos) } + + let(:project) { create(:project) } + let(:user) { create(:user) } + + context 'add foreign key on user_id' do + let!(:todo_with_user) { create_todo(user_id: user.id) } + let!(:todo_without_user) { create_todo(user_id: 4711) } + + it 'removes orphaned todos without corresponding user' do + expect { migrate! }.to change { Todo.count }.from(2).to(1) + end + + it 'does not remove entries with valid user_id' do + expect { migrate! }.not_to change { todo_with_user.reload } + end + end + + context 'add foreign key on author_id' do + let!(:todo_with_author) { create_todo(author_id: user.id) } + let!(:todo_with_invalid_author) { create_todo(author_id: 4711) } + + it 'removes orphaned todos by author_id' do + expect { migrate! }.to change { Todo.count }.from(2).to(1) + end + + it 'does not touch author_id for valid entries' do + expect { migrate! }.not_to change { todo_with_author.reload } + end + end + + context 'add foreign key on note_id' do + let(:note) { create(:note) } + let!(:todo_with_note) { create_todo(note_id: note.id) } + let!(:todo_with_invalid_note) { create_todo(note_id: 4711) } + let!(:todo_without_note) { create_todo(note_id: nil) } + + it 'deletes todo if note_id is set but does not exist in notes table' do + expect { migrate! }.to change { Todo.count }.from(3).to(2) + end + + it 'does not touch entry if note_id is nil' do + expect { migrate! }.not_to change { todo_without_note.reload } + end + + it 'does not touch note_id for valid entries' do + expect { migrate! }.not_to change { todo_with_note.reload } + end + end + + def create_todo(**opts) + todos.create!( + project_id: project.id, + user_id: user.id, + author_id: user.id, + target_type: '', + action: 0, + state: '', **opts + ) + end +end -- cgit v1.2.3