Skip to content

Commit d1ec6d9

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 d1ec6d9

18 files changed

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