From d30d70d32d624b567d771886c1baf70fb02b4ef1 Mon Sep 17 00:00:00 2001 From: Vasilii Iakliushin Date: Fri, 19 Jul 2024 11:44:37 +0200 Subject: [PATCH] Remove default value from `organization_id` field for snippets Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/460828 **Problem** After https://gitlab.com/gitlab-org/gitlab/-/merge_requests/157768/, `organization_id` should be set by the application. Default value in database is not necessary anymore. **Solution** Remove default value from `organization_id` Changelog: changed --- app/models/snippet.rb | 3 +++ db/fixtures/development/12_snippets.rb | 1 + .../20240719094108_change_snippets_default.rb | 11 +++++++++++ db/schema_migrations/20240719094108 | 1 + db/structure.sql | 2 +- spec/factories/snippets.rb | 1 + 6 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 db/post_migrate/20240719094108_change_snippets_default.rb create mode 100644 db/schema_migrations/20240719094108 diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 93acff57547c78..ba30cbc71dace7 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -20,6 +20,9 @@ class Snippet < ApplicationRecord include CreatedAtFilterable include EachBatch include Import::HasImportSource + include SafelyChangeColumnDefault + + columns_changing_default :organization_id MAX_FILE_COUNT = 10 diff --git a/db/fixtures/development/12_snippets.rb b/db/fixtures/development/12_snippets.rb index 4f885f99846e89..caa6887a581908 100644 --- a/db/fixtures/development/12_snippets.rb +++ b/db/fixtures/development/12_snippets.rb @@ -33,6 +33,7 @@ def self.cleanup title: FFaker::Lorem.sentence(3), file_name: 'file.rb', visibility_level: Gitlab::VisibilityLevel.values.sample, + organization: Organizations::Organization.default_organization, content: 'foo' }).tap do |snippet| snippet.repository.expire_exists_cache diff --git a/db/post_migrate/20240719094108_change_snippets_default.rb b/db/post_migrate/20240719094108_change_snippets_default.rb new file mode 100644 index 00000000000000..c21dce3c3bac57 --- /dev/null +++ b/db/post_migrate/20240719094108_change_snippets_default.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class ChangeSnippetsDefault < Gitlab::Database::Migration[2.2] + DEFAULT_ORGANIZATION_ID = 1 + + milestone '17.3' + + def change + change_column_default('snippets', 'organization_id', from: DEFAULT_ORGANIZATION_ID, to: nil) + end +end diff --git a/db/schema_migrations/20240719094108 b/db/schema_migrations/20240719094108 new file mode 100644 index 00000000000000..8cea5806381fba --- /dev/null +++ b/db/schema_migrations/20240719094108 @@ -0,0 +1 @@ +9d0b8d803cb0eebf25dbcba9b4013fd432fac732f91e9b2786b0ce23cf00436b \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index e69ef3f1bf00ae..4273b8f7804080 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -17748,7 +17748,7 @@ CREATE TABLE snippets ( secret boolean DEFAULT false NOT NULL, repository_read_only boolean DEFAULT false NOT NULL, imported_from smallint DEFAULT 0 NOT NULL, - organization_id bigint DEFAULT 1 + organization_id bigint ); CREATE SEQUENCE snippets_id_seq diff --git a/spec/factories/snippets.rb b/spec/factories/snippets.rb index e79387d685fa09..65168d040391c3 100644 --- a/spec/factories/snippets.rb +++ b/spec/factories/snippets.rb @@ -51,6 +51,7 @@ factory :personal_snippet, parent: :snippet, class: :PersonalSnippet do author { association(:author, :with_namespace) } + organization { association :organization, :default } project { nil } trait :secret do -- GitLab