diff --git a/db/docs/batched_background_migrations/backfill_snippet_repositories_snippet_organization_id.yml b/db/docs/batched_background_migrations/backfill_snippet_repositories_snippet_organization_id.yml new file mode 100644 index 0000000000000000000000000000000000000000..121472bfeeeb7085a3ca6c724bb3aa9287af9c99 --- /dev/null +++ b/db/docs/batched_background_migrations/backfill_snippet_repositories_snippet_organization_id.yml @@ -0,0 +1,8 @@ +--- +migration_job_name: BackfillSnippetRepositoriesSnippetOrganizationId +description: Backfills sharding key `snippet_repositories.snippet_organization_id` from `snippets`. +feature_category: source_code_management +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/175410 +milestone: '17.10' +queued_migration_version: 20241211134715 +finalized_by: # version of the migration that finalized this BBM diff --git a/db/docs/batched_background_migrations/backfill_snippet_repositories_snippet_project_id.yml b/db/docs/batched_background_migrations/backfill_snippet_repositories_snippet_project_id.yml new file mode 100644 index 0000000000000000000000000000000000000000..84acd05fa7c39dc6fed6c14687d5cbce822f9cd0 --- /dev/null +++ b/db/docs/batched_background_migrations/backfill_snippet_repositories_snippet_project_id.yml @@ -0,0 +1,8 @@ +--- +migration_job_name: BackfillSnippetRepositoriesSnippetProjectId +description: Backfills sharding key `snippet_repositories.snippet_project_id` from `snippets`. +feature_category: source_code_management +introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/175410 +milestone: '17.10' +queued_migration_version: 20241211134710 +finalized_by: # version of the migration that finalized this BBM diff --git a/db/docs/snippet_repositories.yml b/db/docs/snippet_repositories.yml index 3d96ecc4ab24a2cdd491eaaf7df1ceed4ae14006..40d10b5236b16609707b290badd9cca09fc0dd7c 100644 --- a/db/docs/snippet_repositories.yml +++ b/db/docs/snippet_repositories.yml @@ -28,3 +28,6 @@ desired_sharding_key: table: snippets sharding_key: organization_id belongs_to: snippet +desired_sharding_key_migration_job_name: +- BackfillSnippetRepositoriesSnippetProjectId +- BackfillSnippetRepositoriesSnippetOrganizationId diff --git a/db/migrate/20241211134706_add_snippet_project_id_to_snippet_repositories.rb b/db/migrate/20241211134706_add_snippet_project_id_to_snippet_repositories.rb new file mode 100644 index 0000000000000000000000000000000000000000..17a81f99077f6b94f116c0c022990bd469449d9c --- /dev/null +++ b/db/migrate/20241211134706_add_snippet_project_id_to_snippet_repositories.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddSnippetProjectIdToSnippetRepositories < Gitlab::Database::Migration[2.2] + milestone '17.10' + + def change + add_column :snippet_repositories, :snippet_project_id, :bigint + end +end diff --git a/db/migrate/20241211134711_add_snippet_organization_id_to_snippet_repositories.rb b/db/migrate/20241211134711_add_snippet_organization_id_to_snippet_repositories.rb new file mode 100644 index 0000000000000000000000000000000000000000..bcc8b3eb7647eaf28aec14f95426b5a0c8a4e7bb --- /dev/null +++ b/db/migrate/20241211134711_add_snippet_organization_id_to_snippet_repositories.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddSnippetOrganizationIdToSnippetRepositories < Gitlab::Database::Migration[2.2] + milestone '17.10' + + def change + add_column :snippet_repositories, :snippet_organization_id, :bigint + end +end diff --git a/db/post_migrate/20241211134707_index_snippet_repositories_on_snippet_project_id.rb b/db/post_migrate/20241211134707_index_snippet_repositories_on_snippet_project_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..0b9d3814883b00e5ae3b349d1c74a23fa93736ce --- /dev/null +++ b/db/post_migrate/20241211134707_index_snippet_repositories_on_snippet_project_id.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class IndexSnippetRepositoriesOnSnippetProjectId < Gitlab::Database::Migration[2.2] + milestone '17.10' + disable_ddl_transaction! + + INDEX_NAME = 'index_snippet_repositories_on_snippet_project_id' + + def up + add_concurrent_index :snippet_repositories, :snippet_project_id, name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :snippet_repositories, INDEX_NAME + end +end diff --git a/db/post_migrate/20241211134708_add_snippet_repositories_snippet_project_id_fk.rb b/db/post_migrate/20241211134708_add_snippet_repositories_snippet_project_id_fk.rb new file mode 100644 index 0000000000000000000000000000000000000000..50428f826383d997966377e1cd3c4eede120f74e --- /dev/null +++ b/db/post_migrate/20241211134708_add_snippet_repositories_snippet_project_id_fk.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class AddSnippetRepositoriesSnippetProjectIdFk < Gitlab::Database::Migration[2.2] + milestone '17.10' + disable_ddl_transaction! + + def up + add_concurrent_foreign_key :snippet_repositories, :projects, column: :snippet_project_id, on_delete: :cascade + end + + def down + with_lock_retries do + remove_foreign_key :snippet_repositories, column: :snippet_project_id + end + end +end diff --git a/db/post_migrate/20241211134709_add_snippet_repositories_snippet_project_id_trigger.rb b/db/post_migrate/20241211134709_add_snippet_repositories_snippet_project_id_trigger.rb new file mode 100644 index 0000000000000000000000000000000000000000..26ae122606fbed3b5edb08e81fa3d805d224fd21 --- /dev/null +++ b/db/post_migrate/20241211134709_add_snippet_repositories_snippet_project_id_trigger.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class AddSnippetRepositoriesSnippetProjectIdTrigger < Gitlab::Database::Migration[2.2] + milestone '17.10' + + def up + install_sharding_key_assignment_trigger( + table: :snippet_repositories, + sharding_key: :snippet_project_id, + parent_table: :snippets, + parent_sharding_key: :project_id, + foreign_key: :snippet_id + ) + end + + def down + remove_sharding_key_assignment_trigger( + table: :snippet_repositories, + sharding_key: :snippet_project_id, + parent_table: :snippets, + parent_sharding_key: :project_id, + foreign_key: :snippet_id + ) + end +end diff --git a/db/post_migrate/20241211134710_queue_backfill_snippet_repositories_snippet_project_id.rb b/db/post_migrate/20241211134710_queue_backfill_snippet_repositories_snippet_project_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..2b16f78a37f7957a1379530dd53057990ae286d0 --- /dev/null +++ b/db/post_migrate/20241211134710_queue_backfill_snippet_repositories_snippet_project_id.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +class QueueBackfillSnippetRepositoriesSnippetProjectId < Gitlab::Database::Migration[2.2] + milestone '17.10' + restrict_gitlab_migration gitlab_schema: :gitlab_main_cell + + MIGRATION = "BackfillSnippetRepositoriesSnippetProjectId" + DELAY_INTERVAL = 2.minutes + BATCH_SIZE = 1000 + SUB_BATCH_SIZE = 100 + + def up + queue_batched_background_migration( + MIGRATION, + :snippet_repositories, + :snippet_id, + :snippet_project_id, + :snippets, + :project_id, + :snippet_id, + job_interval: DELAY_INTERVAL, + batch_size: BATCH_SIZE, + sub_batch_size: SUB_BATCH_SIZE + ) + end + + def down + delete_batched_background_migration( + MIGRATION, + :snippet_repositories, + :snippet_id, + [ + :snippet_project_id, + :snippets, + :project_id, + :snippet_id + ] + ) + end +end diff --git a/db/post_migrate/20241211134712_index_snippet_repositories_on_snippet_organization_id.rb b/db/post_migrate/20241211134712_index_snippet_repositories_on_snippet_organization_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..b6023a7272bb3d76c7d7a4d8da2567649d356c6d --- /dev/null +++ b/db/post_migrate/20241211134712_index_snippet_repositories_on_snippet_organization_id.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class IndexSnippetRepositoriesOnSnippetOrganizationId < Gitlab::Database::Migration[2.2] + milestone '17.10' + disable_ddl_transaction! + + INDEX_NAME = 'index_snippet_repositories_on_snippet_organization_id' + + def up + add_concurrent_index :snippet_repositories, :snippet_organization_id, name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :snippet_repositories, INDEX_NAME + end +end diff --git a/db/post_migrate/20241211134713_add_snippet_repositories_snippet_organization_id_fk.rb b/db/post_migrate/20241211134713_add_snippet_repositories_snippet_organization_id_fk.rb new file mode 100644 index 0000000000000000000000000000000000000000..8c821c32bd3315b512459ab735a07d1eebcb16cb --- /dev/null +++ b/db/post_migrate/20241211134713_add_snippet_repositories_snippet_organization_id_fk.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddSnippetRepositoriesSnippetOrganizationIdFk < Gitlab::Database::Migration[2.2] + milestone '17.10' + disable_ddl_transaction! + + def up + add_concurrent_foreign_key :snippet_repositories, :organizations, column: :snippet_organization_id, + on_delete: :cascade + end + + def down + with_lock_retries do + remove_foreign_key :snippet_repositories, column: :snippet_organization_id + end + end +end diff --git a/db/post_migrate/20241211134714_add_snippet_repositories_snippet_organization_id_trigger.rb b/db/post_migrate/20241211134714_add_snippet_repositories_snippet_organization_id_trigger.rb new file mode 100644 index 0000000000000000000000000000000000000000..7a9cd4d0f5610ab8a77b18fb8ba0e2422791d882 --- /dev/null +++ b/db/post_migrate/20241211134714_add_snippet_repositories_snippet_organization_id_trigger.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class AddSnippetRepositoriesSnippetOrganizationIdTrigger < Gitlab::Database::Migration[2.2] + milestone '17.10' + + def up + install_sharding_key_assignment_trigger( + table: :snippet_repositories, + sharding_key: :snippet_organization_id, + parent_table: :snippets, + parent_sharding_key: :organization_id, + foreign_key: :snippet_id + ) + end + + def down + remove_sharding_key_assignment_trigger( + table: :snippet_repositories, + sharding_key: :snippet_organization_id, + parent_table: :snippets, + parent_sharding_key: :organization_id, + foreign_key: :snippet_id + ) + end +end diff --git a/db/post_migrate/20241211134715_queue_backfill_snippet_repositories_snippet_organization_id.rb b/db/post_migrate/20241211134715_queue_backfill_snippet_repositories_snippet_organization_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..cdba4aa6b35ee3552346e420c15ee530054af03b --- /dev/null +++ b/db/post_migrate/20241211134715_queue_backfill_snippet_repositories_snippet_organization_id.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +class QueueBackfillSnippetRepositoriesSnippetOrganizationId < Gitlab::Database::Migration[2.2] + milestone '17.10' + restrict_gitlab_migration gitlab_schema: :gitlab_main_cell + + MIGRATION = "BackfillSnippetRepositoriesSnippetOrganizationId" + DELAY_INTERVAL = 2.minutes + BATCH_SIZE = 1000 + SUB_BATCH_SIZE = 100 + + def up + queue_batched_background_migration( + MIGRATION, + :snippet_repositories, + :snippet_id, + :snippet_organization_id, + :snippets, + :organization_id, + :snippet_id, + job_interval: DELAY_INTERVAL, + batch_size: BATCH_SIZE, + sub_batch_size: SUB_BATCH_SIZE + ) + end + + def down + delete_batched_background_migration( + MIGRATION, + :snippet_repositories, + :snippet_id, + [ + :snippet_organization_id, + :snippets, + :organization_id, + :snippet_id + ] + ) + end +end diff --git a/db/schema_migrations/20241211134706 b/db/schema_migrations/20241211134706 new file mode 100644 index 0000000000000000000000000000000000000000..dbd9892d81124bdd49863d36c2bf3541b286a5ad --- /dev/null +++ b/db/schema_migrations/20241211134706 @@ -0,0 +1 @@ +48f5f852380a7a3e179c913568219ae8799b32c9b59c0f3219be6dc31a45332b \ No newline at end of file diff --git a/db/schema_migrations/20241211134707 b/db/schema_migrations/20241211134707 new file mode 100644 index 0000000000000000000000000000000000000000..3666178c5dccddbc0c6d0e66f7501cdf581f7410 --- /dev/null +++ b/db/schema_migrations/20241211134707 @@ -0,0 +1 @@ +4a3a2de5da48f40751d6b7bacb5a4081954eda13a31f535e0e65c17c821c2e3a \ No newline at end of file diff --git a/db/schema_migrations/20241211134708 b/db/schema_migrations/20241211134708 new file mode 100644 index 0000000000000000000000000000000000000000..3f17f0a8f49b4aeb459a85ad755ac09482499881 --- /dev/null +++ b/db/schema_migrations/20241211134708 @@ -0,0 +1 @@ +d3b3fd1a860be5e1fcf4b1ed8a645d350c9527601db2bf9d0cc5cf8872382026 \ No newline at end of file diff --git a/db/schema_migrations/20241211134709 b/db/schema_migrations/20241211134709 new file mode 100644 index 0000000000000000000000000000000000000000..ed08f8c43a59f9de74cc6e70a49c4b3112f5a1aa --- /dev/null +++ b/db/schema_migrations/20241211134709 @@ -0,0 +1 @@ +035aea2280dca568860cf636727798b9f2a0d98669d8302d12ed8183f07035c7 \ No newline at end of file diff --git a/db/schema_migrations/20241211134710 b/db/schema_migrations/20241211134710 new file mode 100644 index 0000000000000000000000000000000000000000..c87cf892cf935709f7ced0094ff03a25033a76dd --- /dev/null +++ b/db/schema_migrations/20241211134710 @@ -0,0 +1 @@ +38438dced134d7c7bf46dc0bb955f566c4edaebe6bd47b9a4b7d39027c2f0e98 \ No newline at end of file diff --git a/db/schema_migrations/20241211134711 b/db/schema_migrations/20241211134711 new file mode 100644 index 0000000000000000000000000000000000000000..8c107f5215d58d502e24ca223e9fd995016d4b5f --- /dev/null +++ b/db/schema_migrations/20241211134711 @@ -0,0 +1 @@ +7bcb615592724e6a9b499293e5e4eb48daab5a867499a707735fbe336c46b78e \ No newline at end of file diff --git a/db/schema_migrations/20241211134712 b/db/schema_migrations/20241211134712 new file mode 100644 index 0000000000000000000000000000000000000000..a6ccbf74662ca7be5d5964249478fb8a71c6cc6e --- /dev/null +++ b/db/schema_migrations/20241211134712 @@ -0,0 +1 @@ +e53745adeacf8ec8b3ca1f343927942093bdfbccb76d56ac90b4728f967bf7c8 \ No newline at end of file diff --git a/db/schema_migrations/20241211134713 b/db/schema_migrations/20241211134713 new file mode 100644 index 0000000000000000000000000000000000000000..18e4111c5bc57fb5e0382ef5fecbbd32eeb0b415 --- /dev/null +++ b/db/schema_migrations/20241211134713 @@ -0,0 +1 @@ +22d6304d4124f9353db4c409c7a30c7b8bc4536233cb7f388706111f0ca48515 \ No newline at end of file diff --git a/db/schema_migrations/20241211134714 b/db/schema_migrations/20241211134714 new file mode 100644 index 0000000000000000000000000000000000000000..98a4d47780ab64cc1cbd13db7fd6d3b8e71e810e --- /dev/null +++ b/db/schema_migrations/20241211134714 @@ -0,0 +1 @@ +841b185889673d3293c949d91a53baf5501109846d0b2d625d50c2582fe610f2 \ No newline at end of file diff --git a/db/schema_migrations/20241211134715 b/db/schema_migrations/20241211134715 new file mode 100644 index 0000000000000000000000000000000000000000..e92a3224a667a5b7ee0665c9f13af6b2c204f076 --- /dev/null +++ b/db/schema_migrations/20241211134715 @@ -0,0 +1 @@ +4af7bbac18c391a5168bc68fbc53e046884f848c25d8c217aae397a37e52fe80 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 66c8184c240e033b4f15b7139555175b21d71f11..53517c277cc575522376413c1a2c0f9682984c3c 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1396,6 +1396,22 @@ RETURN NEW; END $$; +CREATE FUNCTION trigger_1f57c71a69fb() RETURNS trigger + LANGUAGE plpgsql + AS $$ +BEGIN +IF NEW."snippet_organization_id" IS NULL THEN + SELECT "organization_id" + INTO NEW."snippet_organization_id" + FROM "snippets" + WHERE "snippets"."id" = NEW."snippet_id"; +END IF; + +RETURN NEW; + +END +$$; + CREATE FUNCTION trigger_206cbe2dc1a2() RETURNS trigger LANGUAGE plpgsql AS $$ @@ -3539,6 +3555,22 @@ RETURN NEW; END $$; +CREATE FUNCTION trigger_d9468bfbb0b4() RETURNS trigger + LANGUAGE plpgsql + AS $$ +BEGIN +IF NEW."snippet_project_id" IS NULL THEN + SELECT "project_id" + INTO NEW."snippet_project_id" + FROM "snippets" + WHERE "snippets"."id" = NEW."snippet_id"; +END IF; + +RETURN NEW; + +END +$$; + CREATE FUNCTION trigger_da5fd3d6d75c() RETURNS trigger LANGUAGE plpgsql AS $$ @@ -21979,6 +22011,8 @@ CREATE TABLE snippet_repositories ( verification_failure text, verification_state smallint DEFAULT 0 NOT NULL, verification_started_at timestamp with time zone, + snippet_project_id bigint, + snippet_organization_id bigint, CONSTRAINT snippet_repositories_verification_failure_text_limit CHECK ((char_length(verification_failure) <= 255)) ); @@ -35618,6 +35652,10 @@ CREATE UNIQUE INDEX index_snippet_repositories_on_disk_path ON snippet_repositor CREATE INDEX index_snippet_repositories_on_shard_id ON snippet_repositories USING btree (shard_id); +CREATE INDEX index_snippet_repositories_on_snippet_organization_id ON snippet_repositories USING btree (snippet_organization_id); + +CREATE INDEX index_snippet_repositories_on_snippet_project_id ON snippet_repositories USING btree (snippet_project_id); + CREATE INDEX index_snippet_repositories_pending_verification ON snippet_repositories USING btree (verified_at NULLS FIRST) WHERE (verification_state = 0); CREATE INDEX index_snippet_repositories_verification_state ON snippet_repositories USING btree (verification_state); @@ -38914,6 +38952,8 @@ CREATE TRIGGER trigger_1ed40f4d5f4e BEFORE INSERT OR UPDATE ON packages_maven_me CREATE TRIGGER trigger_1eda1bc6ef53 BEFORE INSERT OR UPDATE ON merge_request_diff_details FOR EACH ROW EXECUTE FUNCTION trigger_1eda1bc6ef53(); +CREATE TRIGGER trigger_1f57c71a69fb BEFORE INSERT OR UPDATE ON snippet_repositories FOR EACH ROW EXECUTE FUNCTION trigger_1f57c71a69fb(); + CREATE TRIGGER trigger_206cbe2dc1a2 BEFORE INSERT OR UPDATE ON packages_package_files FOR EACH ROW EXECUTE FUNCTION trigger_206cbe2dc1a2(); CREATE TRIGGER trigger_207005e8e995 BEFORE INSERT OR UPDATE ON operations_strategies FOR EACH ROW EXECUTE FUNCTION trigger_207005e8e995(); @@ -39190,6 +39230,8 @@ CREATE TRIGGER trigger_d5c895007948 BEFORE INSERT OR UPDATE ON protected_environ CREATE TRIGGER trigger_d8c2de748d8c BEFORE INSERT OR UPDATE ON merge_request_predictions FOR EACH ROW EXECUTE FUNCTION trigger_d8c2de748d8c(); +CREATE TRIGGER trigger_d9468bfbb0b4 BEFORE INSERT OR UPDATE ON snippet_repositories FOR EACH ROW EXECUTE FUNCTION trigger_d9468bfbb0b4(); + CREATE TRIGGER trigger_da5fd3d6d75c BEFORE INSERT OR UPDATE ON packages_composer_metadata FOR EACH ROW EXECUTE FUNCTION trigger_da5fd3d6d75c(); CREATE TRIGGER trigger_dadd660afe2c BEFORE INSERT OR UPDATE ON packages_debian_group_distribution_keys FOR EACH ROW EXECUTE FUNCTION trigger_dadd660afe2c(); @@ -41230,6 +41272,9 @@ ALTER TABLE ONLY merge_request_context_commits ALTER TABLE ONLY approval_project_rules ADD CONSTRAINT fk_efa5a1e3fb FOREIGN KEY (security_orchestration_policy_configuration_id) REFERENCES security_orchestration_policy_configurations(id) ON DELETE CASCADE; +ALTER TABLE ONLY snippet_repositories + ADD CONSTRAINT fk_efaf4ac269 FOREIGN KEY (snippet_organization_id) REFERENCES organizations(id) ON DELETE CASCADE; + ALTER TABLE ONLY dora_daily_metrics ADD CONSTRAINT fk_efc32a39fa FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; @@ -41266,6 +41311,9 @@ ALTER TABLE ONLY abuse_reports ALTER TABLE ONLY timelogs ADD CONSTRAINT fk_f12ef8db70 FOREIGN KEY (timelog_category_id) REFERENCES timelog_categories(id) ON DELETE SET NULL; +ALTER TABLE ONLY snippet_repositories + ADD CONSTRAINT fk_f1319bee9d FOREIGN KEY (snippet_project_id) REFERENCES projects(id) ON DELETE CASCADE; + ALTER TABLE ONLY boards ADD CONSTRAINT fk_f15266b5f9 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE; diff --git a/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_organization_id.rb b/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_organization_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..a9fa1321d2325b781517b04de296e87d4d265409 --- /dev/null +++ b/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_organization_id.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Gitlab + module BackgroundMigration + class BackfillSnippetRepositoriesSnippetOrganizationId < BackfillDesiredShardingKeyJob + operation_name :backfill_snippet_repositories_snippet_organization_id + feature_category :source_code_management + end + end +end diff --git a/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_project_id.rb b/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_project_id.rb new file mode 100644 index 0000000000000000000000000000000000000000..fba695a972cbe1ba3b022ee8cde5a92f440da942 --- /dev/null +++ b/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_project_id.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Gitlab + module BackgroundMigration + class BackfillSnippetRepositoriesSnippetProjectId < BackfillDesiredShardingKeyJob + operation_name :backfill_snippet_repositories_snippet_project_id + feature_category :source_code_management + end + end +end diff --git a/spec/features/snippets/user_creates_snippet_spec.rb b/spec/features/snippets/user_creates_snippet_spec.rb index 23659beb8215abc54aa4dc1d41760c35a1f1daae..989072ba1fb52ba16a3d3ae92766ee5a00149f9a 100644 --- a/spec/features/snippets/user_creates_snippet_spec.rb +++ b/spec/features/snippets/user_creates_snippet_spec.rb @@ -16,6 +16,8 @@ let(:snippet_title_field) { 'snippet-title' } before do + create(:organization, :default) + sign_in(user) visit new_snippet_path diff --git a/spec/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_organization_id_spec.rb b/spec/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_organization_id_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..7c14ed1c0a9de280101590419bb512d27f1bebf2 --- /dev/null +++ b/spec/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_organization_id_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::BackfillSnippetRepositoriesSnippetOrganizationId, + feature_category: :source_code_management, + schema: 20241211134711 do + include_examples 'desired sharding key backfill job' do + let(:batch_table) { :snippet_repositories } + let(:backfill_column) { :snippet_organization_id } + let(:batch_column) { :snippet_id } + let(:backfill_via_table) { :snippets } + let(:backfill_via_column) { :organization_id } + let(:backfill_via_foreign_key) { :snippet_id } + end +end diff --git a/spec/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_project_id_spec.rb b/spec/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_project_id_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..f592656e03900e5d1c5e694dcc88d457c77eb968 --- /dev/null +++ b/spec/lib/gitlab/background_migration/backfill_snippet_repositories_snippet_project_id_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::BackgroundMigration::BackfillSnippetRepositoriesSnippetProjectId, + feature_category: :source_code_management, + schema: 20241211134706 do + include_examples 'desired sharding key backfill job' do + let(:batch_table) { :snippet_repositories } + let(:backfill_column) { :snippet_project_id } + let(:batch_column) { :snippet_id } + let(:backfill_via_table) { :snippets } + let(:backfill_via_column) { :project_id } + let(:backfill_via_foreign_key) { :snippet_id } + end +end diff --git a/spec/migrations/20241211134710_queue_backfill_snippet_repositories_snippet_project_id_spec.rb b/spec/migrations/20241211134710_queue_backfill_snippet_repositories_snippet_project_id_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..42dcb11edae1a1f4923490f0019c927446cfdd3f --- /dev/null +++ b/spec/migrations/20241211134710_queue_backfill_snippet_repositories_snippet_project_id_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe QueueBackfillSnippetRepositoriesSnippetProjectId, feature_category: :source_code_management do + let!(:batched_migration) { described_class::MIGRATION } + + it 'schedules a new batched migration' do + reversible_migration do |migration| + migration.before -> { + expect(batched_migration).not_to have_scheduled_batched_migration + } + + migration.after -> { + expect(batched_migration).to have_scheduled_batched_migration( + table_name: :snippet_repositories, + column_name: :snippet_id, + interval: described_class::DELAY_INTERVAL, + batch_size: described_class::BATCH_SIZE, + sub_batch_size: described_class::SUB_BATCH_SIZE, + gitlab_schema: :gitlab_main_cell, + job_arguments: [ + :snippet_project_id, + :snippets, + :project_id, + :snippet_id + ] + ) + } + end + end +end diff --git a/spec/migrations/20241211134715_queue_backfill_snippet_repositories_snippet_organization_id_spec.rb b/spec/migrations/20241211134715_queue_backfill_snippet_repositories_snippet_organization_id_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..0f49a34a42f4a80c911206192dc196719637b05f --- /dev/null +++ b/spec/migrations/20241211134715_queue_backfill_snippet_repositories_snippet_organization_id_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe QueueBackfillSnippetRepositoriesSnippetOrganizationId, feature_category: :source_code_management do + let!(:batched_migration) { described_class::MIGRATION } + + it 'schedules a new batched migration' do + reversible_migration do |migration| + migration.before -> { + expect(batched_migration).not_to have_scheduled_batched_migration + } + + migration.after -> { + expect(batched_migration).to have_scheduled_batched_migration( + table_name: :snippet_repositories, + column_name: :snippet_id, + interval: described_class::DELAY_INTERVAL, + batch_size: described_class::BATCH_SIZE, + sub_batch_size: described_class::SUB_BATCH_SIZE, + gitlab_schema: :gitlab_main_cell, + job_arguments: [ + :snippet_organization_id, + :snippets, + :organization_id, + :snippet_id + ] + ) + } + end + end +end diff --git a/spec/requests/api/graphql/mutations/snippets/create_spec.rb b/spec/requests/api/graphql/mutations/snippets/create_spec.rb index 9f9b312f12e3e3854a0edffc9f4f800bc3a9baed..a4b65e658a180edb2a44ec7e978c61da574bb143 100644 --- a/spec/requests/api/graphql/mutations/snippets/create_spec.rb +++ b/spec/requests/api/graphql/mutations/snippets/create_spec.rb @@ -37,6 +37,10 @@ def mutation_response graphql_mutation_response(:create_snippet) end + before do + create(:organization, :default) + end + subject { post_graphql_mutation(mutation, current_user: current_user) } context 'when the user does not have permission' do diff --git a/spec/services/snippets/create_service_spec.rb b/spec/services/snippets/create_service_spec.rb index d8fe2ac31c0a7febe29aa53133e9a863850fc87f..beb07e37946844210e67c23eb6455d5f2b154e03 100644 --- a/spec/services/snippets/create_service_spec.rb +++ b/spec/services/snippets/create_service_spec.rb @@ -25,6 +25,10 @@ let(:snippet) { subject.payload[:snippet] } + before do + create(:organization, :default) + end + shared_examples 'a service that creates a snippet' do it 'creates a snippet with the provided attributes' do expect(snippet.title).to eq(opts[:title])