Skip to content

Commit ccfba1e

Browse files
authored
Bigint migration step3 for remaining tables (#4591)
* step 3a and b for remaining tables: jobs, delayed_jobs, app_usage_events, service_usage_events * see also #4406
1 parent 2b5ac3a commit ccfba1e

18 files changed

+280
-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_step3ab'
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_step3ab'
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_step3ab'
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_step3ab'
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_step3ab'
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_step3ab'
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_step3ab'
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_step3ab'
2+
3+
Sequel.migration do
4+
no_transaction
5+
VCAP::BigintMigration.step3b_migration(self, :service_usage_events)
6+
end
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require 'database/bigint_migration'
2+
3+
module VCAP::BigintMigration
4+
class << self
5+
def step3a_migration(migration, table)
6+
migration.up do
7+
if database_type == :postgres &&
8+
!VCAP::BigintMigration.migration_completed?(self, table) &&
9+
!VCAP::BigintMigration.migration_skipped?(self, table)
10+
transaction { VCAP::BigintMigration.add_check_constraint(self, table) }
11+
begin
12+
VCAP::Migration.with_concurrent_timeout(self) do
13+
VCAP::BigintMigration.validate_check_constraint(self, table)
14+
end
15+
rescue Sequel::CheckConstraintViolation
16+
raise "Failed to add check constraint on '#{table}' table!\n" \
17+
"There are rows where 'id_bigint' does not match 'id', " \
18+
"thus step 3 of the bigint migration cannot be executed.\n" \
19+
"Consider running rake task 'db:bigint_backfill[#{table}]'."
20+
end
21+
end
22+
end
23+
24+
migration.down do
25+
transaction { VCAP::BigintMigration.drop_check_constraint(self, table) if database_type == :postgres }
26+
end
27+
end
28+
29+
def step3b_migration(migration, table)
30+
migration.up do
31+
if database_type == :postgres && VCAP::BigintMigration.has_check_constraint?(self, table)
32+
transaction do
33+
# Drop check constraint and trigger function
34+
VCAP::BigintMigration.drop_check_constraint(self, table)
35+
VCAP::BigintMigration.drop_trigger_function(self, table)
36+
37+
# Drop old id column
38+
VCAP::BigintMigration.drop_pk_column(self, table)
39+
40+
# Switch id_bigint -> id
41+
VCAP::BigintMigration.rename_bigint_column(self, table)
42+
VCAP::BigintMigration.add_pk_constraint(self, table)
43+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, table)
44+
end
45+
end
46+
end
47+
48+
migration.down do
49+
if database_type == :postgres && VCAP::BigintMigration.migration_completed?(self, table)
50+
transaction do
51+
# Revert id -> id_bigint
52+
VCAP::BigintMigration.drop_identity(self, table)
53+
VCAP::BigintMigration.drop_timestamp_pk_index(self, table)
54+
VCAP::BigintMigration.drop_pk_constraint(self, table)
55+
VCAP::BigintMigration.revert_bigint_column_name(self, table)
56+
57+
# Restore old id column
58+
VCAP::BigintMigration.add_id_column(self, table)
59+
60+
# To restore the previous state it is necessary to backfill the id column. In case there is a lot of data in the
61+
# table this might be problematic, e.g. take a longer time.
62+
#
63+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
64+
# testing of the bigint migration steps.)
65+
VCAP::BigintMigration.backfill_id(self, table)
66+
67+
VCAP::BigintMigration.add_pk_constraint(self, table)
68+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, table)
69+
70+
# Recreate trigger function and check constraint
71+
VCAP::BigintMigration.create_trigger_function(self, table)
72+
VCAP::BigintMigration.add_check_constraint(self, table)
73+
end
74+
end
75+
end
76+
end
77+
end
78+
end
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)