Skip to content

Commit d86d14c

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 d86d14c

17 files changed

+478
-8
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.migration_completed?(self, :delayed_jobs) && !VCAP::BigintMigration.migration_skipped?(self, :delayed_jobs)
8+
transaction do
9+
VCAP::BigintMigration.add_check_constraint(self, :delayed_jobs)
10+
end
11+
12+
begin
13+
VCAP::Migration.with_concurrent_timeout(self) do
14+
VCAP::BigintMigration.validate_check_constraint(self, :delayed_jobs)
15+
end
16+
rescue Sequel::CheckConstraintViolation
17+
raise "Failed to add check constraint on 'delayed_jobs' table!\n" \
18+
"There are rows where 'id_bigint' does not match 'id', thus step 3 of the bigint migration cannot be executed.\n" \
19+
"Consider running rake task 'db:bigint_backfill[delayed_jobs]'."
20+
end
21+
end
22+
end
23+
24+
down do
25+
transaction do
26+
VCAP::BigintMigration.drop_check_constraint(self, :delayed_jobs) if database_type == :postgres
27+
end
28+
end
29+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && VCAP::BigintMigration.has_check_constraint?(self, :delayed_jobs)
8+
transaction do
9+
# Drop check constraint and trigger function
10+
VCAP::BigintMigration.drop_check_constraint(self, :delayed_jobs)
11+
VCAP::BigintMigration.drop_trigger_function(self, :delayed_jobs)
12+
13+
# Drop old id column
14+
VCAP::BigintMigration.drop_pk_column(self, :delayed_jobs)
15+
16+
# Switch id_bigint -> id
17+
VCAP::BigintMigration.rename_bigint_column(self, :delayed_jobs)
18+
VCAP::BigintMigration.add_pk_constraint(self, :delayed_jobs)
19+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :delayed_jobs)
20+
end
21+
end
22+
end
23+
24+
down do
25+
if database_type == :postgres && VCAP::BigintMigration.migration_completed?(self, :delayed_jobs)
26+
transaction do
27+
# Revert id -> id_bigint
28+
VCAP::BigintMigration.drop_identity(self, :delayed_jobs)
29+
VCAP::BigintMigration.drop_timestamp_pk_index(self, :delayed_jobs)
30+
VCAP::BigintMigration.drop_pk_constraint(self, :delayed_jobs)
31+
VCAP::BigintMigration.revert_bigint_column_name(self, :delayed_jobs)
32+
33+
# Restore old id column
34+
VCAP::BigintMigration.add_id_column(self, :delayed_jobs)
35+
36+
# To restore the previous state it is necessary to backfill the id column. In case there is a lot of data in the
37+
# table this might be problematic, e.g. take a longer time.
38+
#
39+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
40+
# testing of the bigint migration steps.)
41+
VCAP::BigintMigration.backfill_id(self, :delayed_jobs)
42+
43+
VCAP::BigintMigration.add_pk_constraint(self, :delayed_jobs)
44+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :delayed_jobs)
45+
46+
# Recreate trigger function and check constraint
47+
VCAP::BigintMigration.create_trigger_function(self, :delayed_jobs)
48+
VCAP::BigintMigration.add_check_constraint(self, :delayed_jobs)
49+
end
50+
end
51+
end
52+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.migration_completed?(self, :jobs) && !VCAP::BigintMigration.migration_skipped?(self, :jobs)
8+
transaction do
9+
VCAP::BigintMigration.add_check_constraint(self, :jobs)
10+
end
11+
12+
begin
13+
VCAP::Migration.with_concurrent_timeout(self) do
14+
VCAP::BigintMigration.validate_check_constraint(self, :jobs)
15+
end
16+
rescue Sequel::CheckConstraintViolation
17+
raise "Failed to add check constraint on 'jobs' table!\n" \
18+
"There are rows where 'id_bigint' does not match 'id', thus step 3 of the bigint migration cannot be executed.\n" \
19+
"Consider running rake task 'db:bigint_backfill[jobs]'."
20+
end
21+
end
22+
end
23+
24+
down do
25+
transaction do
26+
VCAP::BigintMigration.drop_check_constraint(self, :jobs) if database_type == :postgres
27+
end
28+
end
29+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && VCAP::BigintMigration.has_check_constraint?(self, :jobs)
8+
transaction do
9+
# Drop check constraint and trigger function
10+
VCAP::BigintMigration.drop_check_constraint(self, :jobs)
11+
VCAP::BigintMigration.drop_trigger_function(self, :jobs)
12+
13+
# Drop old id column
14+
VCAP::BigintMigration.drop_pk_column(self, :jobs)
15+
16+
# Switch id_bigint -> id
17+
VCAP::BigintMigration.rename_bigint_column(self, :jobs)
18+
VCAP::BigintMigration.add_pk_constraint(self, :jobs)
19+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :jobs)
20+
end
21+
end
22+
end
23+
24+
down do
25+
if database_type == :postgres && VCAP::BigintMigration.migration_completed?(self, :jobs)
26+
transaction do
27+
# Revert id -> id_bigint
28+
VCAP::BigintMigration.drop_identity(self, :jobs)
29+
VCAP::BigintMigration.drop_timestamp_pk_index(self, :jobs)
30+
VCAP::BigintMigration.drop_pk_constraint(self, :jobs)
31+
VCAP::BigintMigration.revert_bigint_column_name(self, :jobs)
32+
33+
# Restore old id column
34+
VCAP::BigintMigration.add_id_column(self, :jobs)
35+
36+
# To restore the previous state it is necessary to backfill the id column. In case there is a lot of data in the
37+
# table this might be problematic, e.g. take a longer time.
38+
#
39+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
40+
# testing of the bigint migration steps.)
41+
VCAP::BigintMigration.backfill_id(self, :jobs)
42+
43+
VCAP::BigintMigration.add_pk_constraint(self, :jobs)
44+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :jobs)
45+
46+
# Recreate trigger function and check constraint
47+
VCAP::BigintMigration.create_trigger_function(self, :jobs)
48+
VCAP::BigintMigration.add_check_constraint(self, :jobs)
49+
end
50+
end
51+
end
52+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.migration_completed?(self, :app_usage_events) && !VCAP::BigintMigration.migration_skipped?(self, :app_usage_events)
8+
transaction do
9+
VCAP::BigintMigration.add_check_constraint(self, :app_usage_events)
10+
end
11+
12+
begin
13+
VCAP::Migration.with_concurrent_timeout(self) do
14+
VCAP::BigintMigration.validate_check_constraint(self, :app_usage_events)
15+
end
16+
rescue Sequel::CheckConstraintViolation
17+
raise "Failed to add check constraint on 'app_usage_events' table!\n" \
18+
"There are rows where 'id_bigint' does not match 'id', thus step 3 of the bigint migration cannot be executed.\n" \
19+
"Consider running rake task 'db:bigint_backfill[app_usage_events]'."
20+
end
21+
end
22+
end
23+
24+
down do
25+
transaction do
26+
VCAP::BigintMigration.drop_check_constraint(self, :app_usage_events) if database_type == :postgres
27+
end
28+
end
29+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && VCAP::BigintMigration.has_check_constraint?(self, :app_usage_events)
8+
transaction do
9+
# Drop check constraint and trigger function
10+
VCAP::BigintMigration.drop_check_constraint(self, :app_usage_events)
11+
VCAP::BigintMigration.drop_trigger_function(self, :app_usage_events)
12+
13+
# Drop old id column
14+
VCAP::BigintMigration.drop_pk_column(self, :app_usage_events)
15+
16+
# Switch id_bigint -> id
17+
VCAP::BigintMigration.rename_bigint_column(self, :app_usage_events)
18+
VCAP::BigintMigration.add_pk_constraint(self, :app_usage_events)
19+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :app_usage_events)
20+
end
21+
end
22+
end
23+
24+
down do
25+
if database_type == :postgres && VCAP::BigintMigration.migration_completed?(self, :app_usage_events)
26+
transaction do
27+
# Revert id -> id_bigint
28+
VCAP::BigintMigration.drop_identity(self, :app_usage_events)
29+
VCAP::BigintMigration.drop_timestamp_pk_index(self, :app_usage_events)
30+
VCAP::BigintMigration.drop_pk_constraint(self, :app_usage_events)
31+
VCAP::BigintMigration.revert_bigint_column_name(self, :app_usage_events)
32+
33+
# Restore old id column
34+
VCAP::BigintMigration.add_id_column(self, :app_usage_events)
35+
36+
# To restore the previous state it is necessary to backfill the id column. In case there is a lot of data in the
37+
# table this might be problematic, e.g. take a longer time.
38+
#
39+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
40+
# testing of the bigint migration steps.)
41+
VCAP::BigintMigration.backfill_id(self, :app_usage_events)
42+
43+
VCAP::BigintMigration.add_pk_constraint(self, :app_usage_events)
44+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :app_usage_events)
45+
46+
# Recreate trigger function and check constraint
47+
VCAP::BigintMigration.create_trigger_function(self, :app_usage_events)
48+
VCAP::BigintMigration.add_check_constraint(self, :app_usage_events)
49+
end
50+
end
51+
end
52+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.migration_completed?(self, :service_usage_events) && !VCAP::BigintMigration.migration_skipped?(self, :service_usage_events)
8+
transaction do
9+
VCAP::BigintMigration.add_check_constraint(self, :service_usage_events)
10+
end
11+
12+
begin
13+
VCAP::Migration.with_concurrent_timeout(self) do
14+
VCAP::BigintMigration.validate_check_constraint(self, :service_usage_events)
15+
end
16+
rescue Sequel::CheckConstraintViolation
17+
raise "Failed to add check constraint on 'service_usage_events' table!\n" \
18+
"There are rows where 'id_bigint' does not match 'id', thus step 3 of the bigint migration cannot be executed.\n" \
19+
"Consider running rake task 'db:bigint_backfill[service_usage_events]'."
20+
end
21+
end
22+
end
23+
24+
down do
25+
transaction do
26+
VCAP::BigintMigration.drop_check_constraint(self, :service_usage_events) if database_type == :postgres
27+
end
28+
end
29+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'database/bigint_migration'
2+
3+
Sequel.migration do
4+
no_transaction
5+
6+
up do
7+
if database_type == :postgres && VCAP::BigintMigration.has_check_constraint?(self, :service_usage_events)
8+
transaction do
9+
# Drop check constraint and trigger function
10+
VCAP::BigintMigration.drop_check_constraint(self, :service_usage_events)
11+
VCAP::BigintMigration.drop_trigger_function(self, :service_usage_events)
12+
13+
# Drop old id column
14+
VCAP::BigintMigration.drop_pk_column(self, :service_usage_events)
15+
16+
# Switch id_bigint -> id
17+
VCAP::BigintMigration.rename_bigint_column(self, :service_usage_events)
18+
VCAP::BigintMigration.add_pk_constraint(self, :service_usage_events)
19+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :service_usage_events)
20+
end
21+
end
22+
end
23+
24+
down do
25+
if database_type == :postgres && VCAP::BigintMigration.migration_completed?(self, :service_usage_events)
26+
transaction do
27+
# Revert id -> id_bigint
28+
VCAP::BigintMigration.drop_identity(self, :service_usage_events)
29+
VCAP::BigintMigration.drop_timestamp_pk_index(self, :service_usage_events)
30+
VCAP::BigintMigration.drop_pk_constraint(self, :service_usage_events)
31+
VCAP::BigintMigration.revert_bigint_column_name(self, :service_usage_events)
32+
33+
# Restore old id column
34+
VCAP::BigintMigration.add_id_column(self, :service_usage_events)
35+
36+
# To restore the previous state it is necessary to backfill the id column. In case there is a lot of data in the
37+
# table this might be problematic, e.g. take a longer time.
38+
#
39+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
40+
# testing of the bigint migration steps.)
41+
VCAP::BigintMigration.backfill_id(self, :service_usage_events)
42+
43+
VCAP::BigintMigration.add_pk_constraint(self, :service_usage_events)
44+
VCAP::BigintMigration.set_pk_as_identity_with_correct_start_value(self, :service_usage_events)
45+
46+
# Recreate trigger function and check constraint
47+
VCAP::BigintMigration.create_trigger_function(self, :service_usage_events)
48+
VCAP::BigintMigration.add_check_constraint(self, :service_usage_events)
49+
end
50+
end
51+
end
52+
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
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'spec_helper'
2+
require 'migrations/helpers/bigint_migration_step3_shared_context'
3+
4+
RSpec.describe 'bigint migration - delayed jobs table - step3a', isolation: :truncation, type: :migration do
5+
include_context 'bigint migration step3a' do
6+
let(:migration_filename_step1) { '20250729142800_bigint_migration_delayed_jobs_step1.rb' }
7+
let(:migration_filename_step3a) { '20250930135612_bigint_migration_service_usage_events_step3a.rb' }
8+
let(:table) { :delayed_jobs }
9+
let(:insert) do
10+
lambda do |db|
11+
db[:delayed_jobs].insert(guid: SecureRandom.uuid, created_at: Time.now.utc, updated_at: Time.now.utc)
12+
end
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)