Skip to content

Commit

Permalink
feat: #13 allow schema install to be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Nov 1, 2023
1 parent be051b3 commit fe94eb1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
9 changes: 9 additions & 0 deletions demo/DemoServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public void ConfigureServices(IServiceCollection services)
o.TableCount = 1;
o.SchemaName = "SignalRCore";
});

// Example of using DI to configure options:
services.AddOptions<SqlServerOptions>().Configure<IConfiguration>((o, config) =>
{
o.ConnectionString = config.GetConnectionString("Default");
});

// Register specific hubs with specific backplanes:
//services.AddSingleton<HubLifetimeManager<ChatHubB>, DefaultHubLifetimeManager<ChatHubB>>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion demo/DemoServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"IntelliTect.AspNetCore.SignalR": "Debug",
"IntelliTect.AspNetCore.SignalR": "Trace",
"Microsoft.Hosting.Lifetime": "Information"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public SqlInstaller(SqlServerOptions options, ILogger logger, string messagesTab

public async Task Install()
{
if (!_options.AutoEnableServiceBroker && !_options.AutoInstallSchema)
{
_logger.LogInformation("Skipping install of SignalR SQL objects");
return;
}

_logger.LogInformation("Start installing SignalR SQL objects");
try
{
Expand All @@ -47,17 +53,20 @@ public async Task Install()
}
}

var script = GetType().Assembly.StringResource("install.sql");
if (_options.AutoInstallSchema)
{
var script = GetType().Assembly.StringResource("install.sql");

script = script.Replace("SET @SCHEMA_NAME = 'SignalR';", "SET @SCHEMA_NAME = '" + _options.SchemaName + "';");
script = script.Replace("SET @TARGET_SCHEMA_VERSION = 1;", "SET @TARGET_SCHEMA_VERSION = " + SchemaVersion + ";");
script = script.Replace("SET @MESSAGE_TABLE_COUNT = 1;", "SET @MESSAGE_TABLE_COUNT = " + _options.TableCount + ";");
script = script.Replace("SET @MESSAGE_TABLE_NAME = 'Messages';", "SET @MESSAGE_TABLE_NAME = '" + _messagesTableNamePrefix + "';");
script = script.Replace("SET @SCHEMA_NAME = 'SignalR';", "SET @SCHEMA_NAME = '" + _options.SchemaName + "';");
script = script.Replace("SET @TARGET_SCHEMA_VERSION = 1;", "SET @TARGET_SCHEMA_VERSION = " + SchemaVersion + ";");
script = script.Replace("SET @MESSAGE_TABLE_COUNT = 1;", "SET @MESSAGE_TABLE_COUNT = " + _options.TableCount + ";");
script = script.Replace("SET @MESSAGE_TABLE_NAME = 'Messages_YourHubName';", "SET @MESSAGE_TABLE_NAME = '" + _messagesTableNamePrefix + "';");

command.CommandText = script;
await command.ExecuteNonQueryAsync();
command.CommandText = script;
await command.ExecuteNonQueryAsync();

_logger.LogInformation("SignalR SQL objects installed");
_logger.LogInformation("SignalR SQL objects installed");
}
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ DECLARE @SCHEMA_NAME nvarchar(32),
@CREATE_MESSAGE_TABLE_DDL nvarchar(1000),
@CREATE_MESSAGE_ID_TABLE_DDL nvarchar(1000);


-- IF MANUALLY INSTALLING, ADJUST THESE VARIABLES TO MATCH YOUR CONFIG:

SET @SCHEMA_NAME = 'SignalR';
SET @MESSAGE_TABLE_COUNT = 1;
-- Replace 'YourHubName' with your hub's unqualified class name,
-- or according to your custom TableSlugGenerator setting if used.
SET @MESSAGE_TABLE_NAME = 'Messages_YourHubName';

-- END CUSTOMIZABLE VARIABLES


SET @SCHEMA_TABLE_NAME = 'Schema';
SET @TARGET_SCHEMA_VERSION = 1;
SET @MESSAGE_TABLE_COUNT = 1;
SET @MESSAGE_TABLE_NAME = 'Messages';
SET @CREATE_MESSAGE_TABLE_DDL =
N'CREATE TABLE [' + @SCHEMA_NAME + N'].[@TableName](
[PayloadId] [bigint] NOT NULL,
Expand Down
11 changes: 11 additions & 0 deletions src/IntelliTect.AspNetCore.SignalR.SqlServer/SqlServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public class SqlServerOptions
/// </summary>
public bool AutoEnableServiceBroker { get; set; } = false;

/// <summary>
/// <para>
/// If true (the default), on startup the application will attempt to automatically install its
/// required tables into the target database. If disabled, you are required to install the tables yourself
/// using the <see href="https://github.com/IntelliTect/IntelliTect.AspNetCore.SignalR.SqlServer/blob/master/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/install.sql">install.sql</see>
/// script in this project's repository, changing the @SCHEMA_NAME, @MESSAGE_TABLE_COUNT,
/// and @MESSAGE_TABLE_NAME variables to match your configuration.
/// </para>
/// </summary>
public bool AutoInstallSchema { get; set; } = true;

/// <summary>
/// Flag enum that specifies the allowed modes for retrieving messages from SQL Server. Default Auto.
/// </summary>
Expand Down

0 comments on commit fe94eb1

Please sign in to comment.