Skip to content

Commit cbba84a

Browse files
committed
Bigint migration step3 for remaining tables
* step 3a and b for remaining tables: jobs, delayed_jobs, app_usage_events, service_usage_events * see also #4406
1 parent f24452b commit cbba84a

18 files changed

+274
-8
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3a_migration(self, :delayed_jobs)
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3b_migration(self, :delayed_jobs)
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3a_migration(self, :jobs)
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3b_migration(self, :jobs)
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3a_migration(self, :app_usage_events)
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3b_migration(self, :app_usage_events)
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3a_migration(self, :service_usage_events)
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3b_migration(self, :service_usage_events)
6+
end

lib/database/bigint_migration.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,78 @@ def backfill_id(db, table)
164164
end
165165
end
166166

167+
def step3a_migration(migration, table)
168+
migration.up do
169+
if database_type == :postgres &&
170+
!VCAP::BigintMigration.migration_completed?(self, table) &&
171+
!VCAP::BigintMigration.migration_skipped?(self, table)
172+
transaction { VCAP::BigintMigration.add_check_constraint(self, table) }
173+
begin
174+
VCAP::Migration.with_concurrent_timeout(self) do
175+
VCAP::BigintMigration.validate_check_constraint(self, table)
176+
end
177+
rescue Sequel::CheckConstraintViolation
178+
raise "Failed to add check constraint on '#{table}' table!\n" \
179+
"There are rows where 'id_bigint' does not match 'id', thus step 3 of the bigint migration cannot be executed.\n" \
180+
"Consider running rake task 'db:bigint_backfill[#{table}]'."
181+
end
182+
end
183+
end
184+
185+
migration.down do
186+
transaction { VCAP::BigintMigration.drop_check_constraint(self, table) if database_type == :postgres }
187+
end
188+
end
189+
190+
def step3b_migration(migration, table)
191+
migration.up do
192+
if database_type == :postgres && VCAP::BigintMigration.has_check_constraint?(self, table)
193+
transaction do
194+
# Drop check constraint and trigger function
195+
VCAP::BigintMigration.drop_check_constraint(self, table)
196+
VCAP::BigintMigration.drop_trigger_function(self, table)
197+
198+
# Drop old id column
199+
VCAP::BigintMigration.drop_pk_column(self, table)
200+
201+
# Switch id_bigint -> id
202+
VCAP::BigintMigration.rename_bigint_column(self, table)
203+
VCAP::BigintMigration.add_pk_constraint(self, table)
204+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, table)
205+
end
206+
end
207+
end
208+
209+
migration.down do
210+
if database_type == :postgres && VCAP::BigintMigration.migration_completed?(self, table)
211+
transaction do
212+
# Revert id -> id_bigint
213+
VCAP::BigintMigration.drop_identity(self, table)
214+
VCAP::BigintMigration.drop_timestamp_pk_index(self, table)
215+
VCAP::BigintMigration.drop_pk_constraint(self, table)
216+
VCAP::BigintMigration.revert_bigint_column_name(self, table)
217+
218+
# Restore old id column
219+
VCAP::BigintMigration.add_id_column(self, table)
220+
221+
# To restore the previous state it is necessary to backfill the id column. In case there is a lot of data in the
222+
# table this might be problematic, e.g. take a longer time.
223+
#
224+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
225+
# testing of the bigint migration steps.)
226+
VCAP::BigintMigration.backfill_id(self, table)
227+
228+
VCAP::BigintMigration.add_pk_constraint(self, table)
229+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, table)
230+
231+
# Recreate trigger function and check constraint
232+
VCAP::BigintMigration.create_trigger_function(self, table)
233+
VCAP::BigintMigration.add_check_constraint(self, table)
234+
end
235+
end
236+
end
237+
end
238+
167239
private
168240

169241
def column_attribute(db, table, column, attribute)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
require 'migrations/helpers/bigint_migration_step3_shared_context'
3+
4+
RSpec.describe 'bigint migration - service_usage_events table - step3a', isolation: :truncation, type: :migration do
5+
include_context 'bigint migration step3a' do
6+
let(:migration_filename_step1) { '20250729143100_bigint_migration_service_usage_events_step1.rb' }
7+
let(:migration_filename_step3a) { '20250930135612_bigint_migration_service_usage_events_step3a.rb' }
8+
let(:table) { :service_usage_events }
9+
let(:insert) do
10+
lambda do |db|
11+
db[:service_usage_events].insert(guid: SecureRandom.uuid, created_at: Time.now.utc,
12+
state: 'teststate', org_guid: SecureRandom.uuid,
13+
space_guid: SecureRandom.uuid, space_name: 'testspace',
14+
service_instance_guid: SecureRandom.uuid, service_instance_name: 'testinstance',
15+
service_instance_type: 'testtype')
16+
end
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)