Commit 7909b52f authored by Andreas Brandl's avatar Andreas Brandl

Merge branch 'backfill-releases-updated-at' into 'master'

Backfill Releases updated_at And Add Not Null Constraints

Closes #27638

See merge request gitlab-org/gitlab!17400
parents 40cfd00d 52cd60dc
---
title: Backfill releases table updated_at column and add not null constraints to created_at and updated_at
merge_request: 17400
author:
type: fixed
# frozen_string_literal: true
class BackfillReleasesTableUpdatedAtAndAddNotNullConstraintsToTimestamps < ActiveRecord::Migration[5.2]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
change_column_null(:releases, :created_at, false, Time.zone.now)
update_column_in_batches(:releases, :updated_at, Arel.sql('created_at')) do |table, query|
query.where(table[:updated_at].eq(nil))
end
change_column_null(:releases, :updated_at, false, Time.zone.now)
end
def down
change_column_null(:releases, :updated_at, true)
change_column_null(:releases, :created_at, true)
end
end
......@@ -3137,8 +3137,8 @@ ActiveRecord::Schema.define(version: 2019_09_27_074328) do
t.string "tag"
t.text "description"
t.integer "project_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "description_html"
t.integer "cached_markdown_version"
t.integer "author_id"
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20190920194925_backfill_releases_table_updated_at_and_add_not_null_constraints_to_timestamps.rb')
describe BackfillReleasesTableUpdatedAtAndAddNotNullConstraintsToTimestamps, :migration do
let(:releases) { table(:releases) }
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
subject(:migration) { described_class.new }
it 'fills null updated_at rows with the value of created_at' do
created_at_a = Time.zone.parse('2014-03-11T04:30:00Z')
created_at_b = Time.zone.parse('2019-09-10T12:00:00Z')
namespace = namespaces.create(name: 'foo', path: 'foo')
project = projects.create!(namespace_id: namespace.id)
release_a = releases.create!(project_id: project.id,
released_at: Time.zone.parse('2014-12-10T06:00:00Z'),
created_at: created_at_a)
release_b = releases.create!(project_id: project.id,
released_at: Time.zone.parse('2019-09-11T06:00:00Z'),
created_at: created_at_b)
release_a.update!(updated_at: nil)
release_b.update!(updated_at: nil)
disable_migrations_output { migrate! }
release_a.reload
release_b.reload
expect(release_a.updated_at).to eq(created_at_a)
expect(release_b.updated_at).to eq(created_at_b)
end
it 'does not change updated_at columns with a value' do
created_at_a = Time.zone.parse('2014-03-11T04:30:00Z')
updated_at_a = Time.zone.parse('2015-01-16T10:00:00Z')
created_at_b = Time.zone.parse('2019-09-10T12:00:00Z')
namespace = namespaces.create(name: 'foo', path: 'foo')
project = projects.create!(namespace_id: namespace.id)
release_a = releases.create!(project_id: project.id,
released_at: Time.zone.parse('2014-12-10T06:00:00Z'),
created_at: created_at_a,
updated_at: updated_at_a)
release_b = releases.create!(project_id: project.id,
released_at: Time.zone.parse('2019-09-11T06:00:00Z'),
created_at: created_at_b)
release_b.update!(updated_at: nil)
disable_migrations_output { migrate! }
release_a.reload
release_b.reload
expect(release_a.updated_at).to eq(updated_at_a)
expect(release_b.updated_at).to eq(created_at_b)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment