diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-10-26 16:40:56 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-11-01 14:09:19 +0100 |
commit | 6d7bf439d6bad4fff6bba0fbdb91edf50974ad4b (patch) | |
tree | 3f5021fba0b21edc60d13820a5ac2057049dbfcd /db/migrate | |
parent | 8e674510c582c75cab55d80a6253a6c9fb6addc2 (diff) | |
download | gitlab-ce-6d7bf439d6bad4fff6bba0fbdb91edf50974ad4b.tar.gz |
Enforce a default snippet access level in the DB
This adds a database migration that ensures that
project_features.snippets_access_level defaults to a value of 20 (=
ProjectFeature::ENABLED), instead of NULL. This allows us to simplify
some of the queries used for obtaining snippets, as we no longer need to
handle cases where this column is NULL.
Diffstat (limited to 'db/migrate')
-rw-r--r-- | db/migrate/20181026143227_migrate_snippets_access_level_default_value.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/db/migrate/20181026143227_migrate_snippets_access_level_default_value.rb b/db/migrate/20181026143227_migrate_snippets_access_level_default_value.rb new file mode 100644 index 00000000000..ede0ee27b8a --- /dev/null +++ b/db/migrate/20181026143227_migrate_snippets_access_level_default_value.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class MigrateSnippetsAccessLevelDefaultValue < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + ENABLED = 20 + + disable_ddl_transaction! + + class ProjectFeature < ActiveRecord::Base + include EachBatch + + self.table_name = 'project_features' + end + + def up + change_column_default :project_features, :snippets_access_level, ENABLED + + # On GitLab.com this will update about 28 000 rows. Since our updates are + # very small and this column is not indexed, these updates should be very + # lightweight. + ProjectFeature.where(snippets_access_level: nil).each_batch do |batch| + batch.update_all(snippets_access_level: ENABLED) + end + + # We do not need to perform this in a post-deployment migration as the + # ProjectFeature model already enforces a default value for all new rows. + change_column_null :project_features, :snippets_access_level, false + end + + def down + change_column_null :project_features, :snippets_access_level, true + change_column_default :project_features, :snippets_access_level, nil + + # We can't migrate from 20 -> NULL, as some projects may have explicitly set + # the access level to 20. + end +end |