Remove nils from project_statistics.packages_size

Now it defaults to 0
This commit is contained in:
Alessio Caiazza 2019-05-31 09:59:36 +00:00 committed by Andreas Brandl
parent bc32249b30
commit 724b5afbd9
5 changed files with 66 additions and 2 deletions

View file

@ -48,7 +48,7 @@ class ProjectStatistics < ApplicationRecord
# older migrations fail due to non-existent attribute without this
def packages_size
has_attribute?(:packages_size) ? super.to_i : 0
has_attribute?(:packages_size) ? super : 0
end
def update_storage_size

View file

@ -0,0 +1,5 @@
---
title: Forbid NULL in project_statistics.packages_size
merge_request: 28400
author:
type: other

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
class ChangePackagesSizeDefaultsInProjectStatistics < ActiveRecord::Migration[5.1]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
change_column_default :project_statistics, :packages_size, 0
update_column_in_batches(:project_statistics, :packages_size, 0) do |table, query|
query.where(table[:packages_size].eq(nil))
end
change_column_null :project_statistics, :packages_size, false
end
def down
change_column_null :project_statistics, :packages_size, true
change_column_default :project_statistics, :packages_size, nil
end
end

View file

@ -1744,7 +1744,7 @@ ActiveRecord::Schema.define(version: 20190527194900) do
t.bigint "repository_size", default: 0, null: false
t.bigint "lfs_objects_size", default: 0, null: false
t.bigint "build_artifacts_size", default: 0, null: false
t.bigint "packages_size"
t.bigint "packages_size", default: 0, null: false
t.bigint "wiki_size"
t.index ["namespace_id"], name: "index_project_statistics_on_namespace_id", using: :btree
t.index ["project_id"], name: "index_project_statistics_on_project_id", unique: true, using: :btree

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20190516155724_change_packages_size_defaults_in_project_statistics.rb')
describe ChangePackagesSizeDefaultsInProjectStatistics, :migration do
let(:project_statistics) { table(:project_statistics) }
let(:projects) { table(:projects) }
it 'removes null packages_size' do
stats_to_migrate = 10
stats_to_migrate.times do |i|
p = projects.create!(name: "project #{i}", namespace_id: 1)
project_statistics.create!(project_id: p.id, namespace_id: p.namespace_id)
end
expect { migrate! }
.to change { ProjectStatistics.where(packages_size: nil).count }
.from(stats_to_migrate)
.to(0)
end
it 'defaults packages_size to 0' do
project = projects.create!(name: 'a new project', namespace_id: 2)
stat = project_statistics.create!(project_id: project.id, namespace_id: project.namespace_id)
expect(stat.packages_size).to be_nil
migrate!
stat.reload
expect(stat.packages_size).to eq(0)
end
end