1
0
mirror of https://github.com/bitwarden/server synced 2025-12-31 07:33:43 +00:00
Files
server/util/Migrator/DbScripts/2025-12-12_00_PopulateMaxStorageGbIncreased.sql
cyprain-okeke 2b742b0343 [PM-27605] Populate MaxStorageGbIncreased for storage increase from 1GB to 5GB. (#6579)
* Add transition migration to populate MaxStorageGbIncreased

This migration populates the MaxStorageGbIncreased column for Users and
Organizations by setting it to MaxStorageGb + 4, representing the storage
increase from 1GB to 5GB.

This migration depends on PM-27603 being deployed first to create the
MaxStorageGbIncreased column.

Target release: January 6th, 2026

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Using batched updates to reduce lock

* Add changes base on ticket adjustment

* Added the dependency check

* Add temporary index for performance

* Resolved the conflict

* resolve the syntax error

* Update the migration script

* Rename the file to updated date

* Revert the existing merge file change

* revert the change

* revert renaming

* rename file to updated date

* Add the column after renaming

* Rename other migration file to set current date

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Alex Morask <amorask@bitwarden.com>
Co-authored-by: Conner Turnbull <133619638+cturnbull-bitwarden@users.noreply.github.com>
2025-12-18 13:32:03 -05:00

121 lines
3.8 KiB
Transact-SQL

-- ========================================
-- Dependency Validation
-- ========================================
IF NOT EXISTS (
SELECT 1 FROM sys.columns
WHERE object_id = OBJECT_ID('[dbo].[User]')
AND name = 'MaxStorageGbIncreased'
)
BEGIN
RAISERROR('MaxStorageGbIncreased column does not exist in User table. PM-27603 must be deployed first.', 16, 1);
RETURN;
END;
IF NOT EXISTS (
SELECT 1 FROM sys.columns
WHERE object_id = OBJECT_ID('[dbo].[Organization]')
AND name = 'MaxStorageGbIncreased'
)
BEGIN
RAISERROR('MaxStorageGbIncreased column does not exist in Organization table. PM-27603 must be deployed first.', 16, 1);
RETURN;
END;
GO
-- ========================================
-- User Table Migration
-- ========================================
-- Create temporary index for performance
IF NOT EXISTS (
SELECT 1
FROM sys.indexes
WHERE object_id = OBJECT_ID('dbo.User')
AND name = 'IX_TEMP_User_MaxStorageGb_MaxStorageGbIncreased'
)
BEGIN
PRINT 'Creating temporary index on User table...';
CREATE INDEX IX_TEMP_User_MaxStorageGb_MaxStorageGbIncreased
ON [dbo].[User]([MaxStorageGb], [MaxStorageGbIncreased]);
PRINT 'Temporary index created.';
END
GO
-- Populate MaxStorageGbIncreased for Users in batches
DECLARE @BatchSize INT = 5000;
DECLARE @RowsAffected INT = 1;
DECLARE @TotalUpdated INT = 0;
PRINT 'Starting User table update...';
WHILE @RowsAffected > 0
BEGIN
UPDATE TOP (@BatchSize) [dbo].[User]
SET [MaxStorageGbIncreased] = [MaxStorageGb] + 4
WHERE [MaxStorageGb] IS NOT NULL
AND [MaxStorageGbIncreased] IS NULL;
SET @RowsAffected = @@ROWCOUNT;
SET @TotalUpdated = @TotalUpdated + @RowsAffected;
PRINT 'Users updated: ' + CAST(@TotalUpdated AS VARCHAR(10));
WAITFOR DELAY '00:00:00.100'; -- 100ms delay to reduce contention
END
PRINT 'User table update complete. Total rows updated: ' + CAST(@TotalUpdated AS VARCHAR(10));
GO
-- Drop temporary index
DROP INDEX IF EXISTS [dbo].[User].[IX_TEMP_User_MaxStorageGb_MaxStorageGbIncreased];
PRINT 'Temporary index on User table dropped.';
GO
-- ========================================
-- Organization Table Migration
-- ========================================
-- Create temporary index for performance
IF NOT EXISTS (
SELECT 1
FROM sys.indexes
WHERE object_id = OBJECT_ID('dbo.Organization')
AND name = 'IX_TEMP_Organization_MaxStorageGb_MaxStorageGbIncreased'
)
BEGIN
PRINT 'Creating temporary index on Organization table...';
CREATE INDEX IX_TEMP_Organization_MaxStorageGb_MaxStorageGbIncreased
ON [dbo].[Organization]([MaxStorageGb], [MaxStorageGbIncreased]);
PRINT 'Temporary index created.';
END
GO
-- Populate MaxStorageGbIncreased for Organizations in batches
DECLARE @BatchSize INT = 5000;
DECLARE @RowsAffected INT = 1;
DECLARE @TotalUpdated INT = 0;
PRINT 'Starting Organization table update...';
WHILE @RowsAffected > 0
BEGIN
UPDATE TOP (@BatchSize) [dbo].[Organization]
SET [MaxStorageGbIncreased] = [MaxStorageGb] + 4
WHERE [MaxStorageGb] IS NOT NULL
AND [MaxStorageGbIncreased] IS NULL;
SET @RowsAffected = @@ROWCOUNT;
SET @TotalUpdated = @TotalUpdated + @RowsAffected;
PRINT 'Organizations updated: ' + CAST(@TotalUpdated AS VARCHAR(10));
WAITFOR DELAY '00:00:00.100'; -- 100ms delay to reduce contention
END
PRINT 'Organization table update complete. Total rows updated: ' + CAST(@TotalUpdated AS VARCHAR(10));
GO
-- Drop temporary index
DROP INDEX IF EXISTS [dbo].[Organization].[IX_TEMP_Organization_MaxStorageGb_MaxStorageGbIncreased];
PRINT 'Temporary index on Organization table dropped.';
GO