Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding prefix to table names #1557

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Hangfire.SqlServer/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
internal class Constants
{
public static readonly string DefaultSchema = "HangFire";
}
public static readonly string DefaultTablePrefix = string.Empty;
}
}
4 changes: 2 additions & 2 deletions src/Hangfire.SqlServer/CountersAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ BEGIN TRAN

DELETE TOP (@count) C
OUTPUT DELETED.[Key], DELETED.[Value], DELETED.[ExpireAt] INTO @RecordsToAggregate
FROM [{storage.SchemaName}].[Counter] C WITH (READPAST, XLOCK, INDEX(0))
FROM [{storage.SchemaName}].[{storage.TablePrefix}Counter] C WITH (READPAST, XLOCK, INDEX(0))

SET NOCOUNT ON

;MERGE [{storage.SchemaName}].[AggregatedCounter] WITH (FORCESEEK, HOLDLOCK) AS [Target]
;MERGE [{storage.SchemaName}].[{storage.TablePrefix}AggregatedCounter] WITH (FORCESEEK, HOLDLOCK) AS [Target]
USING (
SELECT [Key], SUM([Value]) as [Value], MAX([ExpireAt]) AS [ExpireAt] FROM @RecordsToAggregate
GROUP BY [Key]) AS [Source] ([Key], [Value], [ExpireAt])
Expand Down
98 changes: 94 additions & 4 deletions src/Hangfire.SqlServer/DefaultInstall.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,108 @@ DECLARE @SCHEMA_ID int;
SELECT @SCHEMA_ID = [schema_id] FROM [sys].[schemas] WHERE [name] = 'HangFire';

-- Create the [HangFire].Schema table if not exists
IF NOT EXISTS(SELECT [object_id] FROM [sys].[tables]
WHERE [name] = 'Schema' AND [schema_id] = @SCHEMA_ID)

SELECT @SCHEMAColumnName = [t].[name] FROM [sys].[columns] c
INNER JOIN [sys].[tables] t ON [c].[object_id] = [t.object_id]
WHERE [c].[name] = 'Version' and [t].[name] like '%Schema' and [t].schema_id = @SCHEMA_ID

IF NOT EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @SCHEMAColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
CREATE TABLE [HangFire].[Schema](
[Version] [int] NOT NULL,
[TablePrefix] varchar(20) NOT NULL DEFAULT(''),
[TablePrefixOld] varchar(20) NOT NULL DEFAULT(''),
CONSTRAINT [PK_HangFire_Schema] PRIMARY KEY CLUSTERED ([Version] ASC)
);
SET @SCHEMAColumnName = '[Schema]';
PRINT 'Created table [HangFire].[Schema]';
END
ELSE
PRINT 'Table [HangFire].[Schema] already exists';


-- Update table names if prefix has changed
DECLARE @OLDTABLEPREFIX varchar(20) = SUBSTRING(@SCHEMAColumnName, CHARINDEX('Schema', @SCHEMAColumnName), 7);

IF '' <> @OLDTABLEPREFIX
BEGIN
DECLARE @OLDAggregatedCounterOldColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'AggregatedCounter');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDAggregatedCounterOldColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDAggregatedCounter varchar(37) = CONCAT('[HangFire].[', @OLDAggregatedCounterOldColumnName, ']');
EXEC sp_rename @OLDAggregatedCounter, '[HangFire].[AggregatedCounter]'
END

DECLARE @OLDCounterColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'Counter');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDCounterColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDCounter varchar(27) = CONCAT('[HangFire].[', @OLDCounterColumnName, ']');
EXEC sp_rename @OLDCounter, '[HangFire].[Counter]'
END

DECLARE @OLDHashColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'Hash');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDCounterColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDHash varchar(24) = CONCAT('[HangFire].[', @OLDHashColumnName,']');
EXEC sp_rename @OLDHash, '[HangFire].[Hash]'
END

DECLARE @OLDJobColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'Job');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDJobColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDJob varchar(24) = CONCAT('[HangFire].[', @OLDJobColumnName, ']');
EXEC sp_rename @OLDJob, '[HangFire].[Job]'
END

DECLARE @OLDJobParameterColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'JobParameter');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDJobParameterColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDJobParameter varchar(24) = CONCAT('[HangFire].[', @OLDJobParameterColumnName, ']');
EXEC sp_rename @OLDJobParameter, '[HangFire].[JobParameter]'
END

DECLARE @OLDJobQueueColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'JobQueue');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDJobQueueColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDJobQueue varchar(24) = CONCAT('[HangFire].[', @OLDJobQueueColumnName, ']');
EXEC sp_rename @OLDJobQueue, '[HangFire].[JobQueue]'
END

DECLARE @OLDListColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'List');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDListColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDList varchar(24) = CONCAT('[HangFire].[', @OLDListColumnName, ']');
EXEC sp_rename @OLDList, '[HangFire].[List]'
END

DECLARE @OLDSchemaColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'Schema');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDSchemaColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDSchema varchar(24) = CONCAT('[HangFire].[', @OLDSchemaColumnName, ']');
EXEC sp_rename @OLDSchema, '[HangFire].[Schema]'
END

DECLARE @OLDServerColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'Server');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDServerColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDServer varchar(24) = CONCAT('[HangFire].[', @OLDServerColumnName, ']');
EXEC sp_rename @OLDServer, '[HangFire].[Server]'
END

DECLARE @OLDSetColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'Set');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDSetColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDSet varchar(24) = CONCAT('[HangFire].[', @OLDSetColumnName, ']');
EXEC sp_rename @OLDSet, '[HangFire].[Set]'
END
DECLARE @OLDStateColumnName varchar(37) = CONCAT(@OLDTABLEPREFIX, 'State');
IF EXISTS(SELECT [object_id] FROM [sys].[tables] WHERE [name] = @OLDSetColumnName AND [schema_id] = @SCHEMA_ID)
BEGIN
DECLARE @OLDState varchar(24) = CONCAT('[HangFire].[', @OLDState, ']');
EXEC sp_rename @OLDState, '[HangFire].[State]'
END
END


DECLARE @CURRENT_SCHEMA_VERSION int;
SELECT @CURRENT_SCHEMA_VERSION = [Version] FROM [HangFire].[Schema];

Expand Down Expand Up @@ -443,7 +533,7 @@ BEGIN
-- non-null state name. This will decrease the number of operations, when creating a background job.
-- It will be recreated later in the migration.

DROP INDEX [IX_HangFire_Job_StateName] ON [HangFire].Job;
DROP INDEX [IX_HangFire_Job_StateName] ON [HangFire].[Job];
PRINT 'Dropped index [IX_HangFire_Job_StateName]';

-- Dropping foreign key constraints based on the JobId column, because we need to modify the underlying
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.SqlServer/DefaultInstall.tt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"Hangfire.SqlServer.Install.sql");

script = script.Replace("$(HangFireSchema)", "HangFire");

script = script.Replace("$(HangFireTabelPrefix)", "");
Write(script);
#>

Expand Down
7 changes: 4 additions & 3 deletions src/Hangfire.SqlServer/ExpirationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class ExpirationManager : IServerComponent
private static readonly string[] ProcessedTables =
{
"AggregatedCounter",
"Job",
"Job",
"List",
"Set",
"Hash",
Expand All @@ -61,9 +61,10 @@ public ExpirationManager(SqlServerStorage storage, TimeSpan checkInterval)

public void Execute(CancellationToken cancellationToken)
{
foreach (var table in ProcessedTables)
foreach (var tableNameWithOutPrefix in ProcessedTables)
{
_logger.Debug($"Removing outdated records from the '{table}' table...");
var table = _storage.TablePrefix + tableNameWithOutPrefix;
_logger.Debug($"Removing outdated records from the '{table}' table...");

UseConnectionDistributedLock(_storage, connection =>
{
Expand Down
Loading