Skip to content

Commit 747efd5

Browse files
authored
Bigint Migration for Multiple Tables (Step 1) (#4480)
- dedicated migration file for delayed_jobs, jobs, app_usage_events and service_usage_events - migration spec per table
1 parent a1ba4ec commit 747efd5

9 files changed

+191
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'database/bigint_migration'
2+
3+
table = :delayed_jobs
4+
5+
Sequel.migration do
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.opt_out?
8+
if VCAP::BigintMigration.table_empty?(self, table)
9+
VCAP::BigintMigration.change_pk_to_bigint(self, table)
10+
else
11+
VCAP::BigintMigration.add_bigint_column(self, table)
12+
VCAP::BigintMigration.create_trigger_function(self, table)
13+
end
14+
end
15+
end
16+
17+
down do
18+
if database_type == :postgres
19+
# There is no guarantee that the table is still empty - which was the condition for simply switching the id
20+
# column's type to bigint. We nevertheless want to revert the type to integer as this is the opposite procedure of
21+
# the up migration. In case there is a lot of data in the table at this moment in time, this change might be
22+
# problematic, e.g. take a longer time.
23+
#
24+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
25+
# testing of the bigint migration steps.)
26+
VCAP::BigintMigration.revert_pk_to_integer(self, table)
27+
28+
VCAP::BigintMigration.drop_trigger_function(self, table)
29+
VCAP::BigintMigration.drop_bigint_column(self, table)
30+
end
31+
end
32+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'database/bigint_migration'
2+
3+
table = :jobs
4+
5+
Sequel.migration do
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.opt_out?
8+
if VCAP::BigintMigration.table_empty?(self, table)
9+
VCAP::BigintMigration.change_pk_to_bigint(self, table)
10+
else
11+
VCAP::BigintMigration.add_bigint_column(self, table)
12+
VCAP::BigintMigration.create_trigger_function(self, table)
13+
end
14+
end
15+
end
16+
17+
down do
18+
if database_type == :postgres
19+
# There is no guarantee that the table is still empty - which was the condition for simply switching the id
20+
# column's type to bigint. We nevertheless want to revert the type to integer as this is the opposite procedure of
21+
# the up migration. In case there is a lot of data in the table at this moment in time, this change might be
22+
# problematic, e.g. take a longer time.
23+
#
24+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
25+
# testing of the bigint migration steps.)
26+
VCAP::BigintMigration.revert_pk_to_integer(self, table)
27+
28+
VCAP::BigintMigration.drop_trigger_function(self, table)
29+
VCAP::BigintMigration.drop_bigint_column(self, table)
30+
end
31+
end
32+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'database/bigint_migration'
2+
3+
table = :app_usage_events
4+
5+
Sequel.migration do
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.opt_out?
8+
if VCAP::BigintMigration.table_empty?(self, table)
9+
VCAP::BigintMigration.change_pk_to_bigint(self, table)
10+
else
11+
VCAP::BigintMigration.add_bigint_column(self, table)
12+
VCAP::BigintMigration.create_trigger_function(self, table)
13+
end
14+
end
15+
end
16+
17+
down do
18+
if database_type == :postgres
19+
# There is no guarantee that the table is still empty - which was the condition for simply switching the id
20+
# column's type to bigint. We nevertheless want to revert the type to integer as this is the opposite procedure of
21+
# the up migration. In case there is a lot of data in the table at this moment in time, this change might be
22+
# problematic, e.g. take a longer time.
23+
#
24+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
25+
# testing of the bigint migration steps.)
26+
VCAP::BigintMigration.revert_pk_to_integer(self, table)
27+
28+
VCAP::BigintMigration.drop_trigger_function(self, table)
29+
VCAP::BigintMigration.drop_bigint_column(self, table)
30+
end
31+
end
32+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'database/bigint_migration'
2+
3+
table = :service_usage_events
4+
5+
Sequel.migration do
6+
up do
7+
if database_type == :postgres && !VCAP::BigintMigration.opt_out?
8+
if VCAP::BigintMigration.table_empty?(self, table)
9+
VCAP::BigintMigration.change_pk_to_bigint(self, table)
10+
else
11+
VCAP::BigintMigration.add_bigint_column(self, table)
12+
VCAP::BigintMigration.create_trigger_function(self, table)
13+
end
14+
end
15+
end
16+
17+
down do
18+
if database_type == :postgres
19+
# There is no guarantee that the table is still empty - which was the condition for simply switching the id
20+
# column's type to bigint. We nevertheless want to revert the type to integer as this is the opposite procedure of
21+
# the up migration. In case there is a lot of data in the table at this moment in time, this change might be
22+
# problematic, e.g. take a longer time.
23+
#
24+
# Ideally this down migration SHOULD NEVER BE EXECUTED IN A PRODUCTION SYSTEM! (It's there for proper integration
25+
# testing of the bigint migration steps.)
26+
VCAP::BigintMigration.revert_pk_to_integer(self, table)
27+
28+
VCAP::BigintMigration.drop_trigger_function(self, table)
29+
VCAP::BigintMigration.drop_bigint_column(self, table)
30+
end
31+
end
32+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
require 'migrations/helpers/bigint_migration_step1_shared_context'
3+
4+
RSpec.describe 'bigint migration - delayed_jobs table - step1', isolation: :truncation, type: :migration do
5+
include_context 'bigint migration step1' do
6+
let(:migration_filename) { '20250729142800_bigint_migration_delayed_jobs_step1.rb' }
7+
let(:table) { :delayed_jobs }
8+
let(:insert) do
9+
lambda do |db|
10+
db[:delayed_jobs].insert(guid: SecureRandom.uuid)
11+
end
12+
end
13+
end
14+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'spec_helper'
2+
require 'migrations/helpers/bigint_migration_step1_shared_context'
3+
4+
RSpec.describe 'bigint migration - jobs table - step1', isolation: :truncation, type: :migration do
5+
include_context 'bigint migration step1' do
6+
let(:migration_filename) { '20250729142900_bigint_migration_jobs_step1.rb' }
7+
let(:table) { :jobs }
8+
let(:insert) do
9+
lambda do |db|
10+
db[:jobs].insert(guid: SecureRandom.uuid)
11+
end
12+
end
13+
end
14+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
require 'migrations/helpers/bigint_migration_step1_shared_context'
3+
4+
RSpec.describe 'bigint migration - app_usage_events table - step1', isolation: :truncation, type: :migration do
5+
include_context 'bigint migration step1' do
6+
let(:migration_filename) { '20250729143000_bigint_migration_app_usage_events_step1.rb' }
7+
let(:table) { :app_usage_events }
8+
let(:insert) do
9+
lambda do |db|
10+
db[:app_usage_events].insert(guid: SecureRandom.uuid, created_at: Time.now.utc, instance_count: 1,
11+
memory_in_mb_per_instance: 2, state: 'state', app_guid: 'app_guid',
12+
app_name: 'app_name', space_guid: 'space_guid', space_name: 'space_name',
13+
org_guid: 'org_guid')
14+
end
15+
end
16+
end
17+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
require 'migrations/helpers/bigint_migration_step1_shared_context'
3+
4+
RSpec.describe 'bigint migration - service_usage_events table - step1', isolation: :truncation, type: :migration do
5+
include_context 'bigint migration step1' do
6+
let(:migration_filename) { '20250729143100_bigint_migration_service_usage_events_step1.rb' }
7+
let(:table) { :service_usage_events }
8+
let(:insert) do
9+
lambda do |db|
10+
db[:service_usage_events].insert(guid: SecureRandom.uuid, created_at: Time.now.utc, state: 'state',
11+
org_guid: 'org_guid', space_guid: 'space_guid', space_name: 'space_name',
12+
service_instance_guid: 'si_guid', service_instance_name: 'si_name',
13+
service_instance_type: 'si_type')
14+
end
15+
end
16+
end
17+
end

spec/migrations/helpers/bigint_migration_step1_shared_context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
it 'fails with a proper error message' do
5454
expect do
5555
VCAP::BigintMigration.backfill(logger, db, table)
56-
end.to raise_error(RuntimeError, /table 'events' does not contain column 'id_bigint'/)
56+
end.to raise_error(RuntimeError, /table '#{table}' does not contain column 'id_bigint'/)
5757
end
5858
end
5959
end

0 commit comments

Comments
 (0)