From fe94eb192dc65b6c628a67d7e14655fbd92a67bd Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 1 Nov 2023 09:31:20 -0700 Subject: [PATCH] feat: #13 allow schema install to be disabled --- demo/DemoServer/Startup.cs | 9 +++++++ demo/DemoServer/appsettings.json | 2 +- .../Internal/SqlServer/SqlInstaller.cs | 25 +++++++++++++------ .../Internal/SqlServer/install.sql | 13 ++++++++-- .../SqlServerOptions.cs | 11 ++++++++ 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/demo/DemoServer/Startup.cs b/demo/DemoServer/Startup.cs index 118c263..359d034 100644 --- a/demo/DemoServer/Startup.cs +++ b/demo/DemoServer/Startup.cs @@ -37,6 +37,15 @@ public void ConfigureServices(IServiceCollection services) o.TableCount = 1; o.SchemaName = "SignalRCore"; }); + + // Example of using DI to configure options: + services.AddOptions().Configure((o, config) => + { + o.ConnectionString = config.GetConnectionString("Default"); + }); + + // Register specific hubs with specific backplanes: + //services.AddSingleton, DefaultHubLifetimeManager>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/demo/DemoServer/appsettings.json b/demo/DemoServer/appsettings.json index 12a78cd..97ae8fd 100644 --- a/demo/DemoServer/appsettings.json +++ b/demo/DemoServer/appsettings.json @@ -3,7 +3,7 @@ "LogLevel": { "Default": "Information", "Microsoft": "Warning", - "IntelliTect.AspNetCore.SignalR": "Debug", + "IntelliTect.AspNetCore.SignalR": "Trace", "Microsoft.Hosting.Lifetime": "Information" } }, diff --git a/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/SqlInstaller.cs b/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/SqlInstaller.cs index 10770a6..dc7b242 100644 --- a/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/SqlInstaller.cs +++ b/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/SqlInstaller.cs @@ -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 { @@ -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) { diff --git a/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/install.sql b/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/install.sql index 572c3da..c192934 100644 --- a/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/install.sql +++ b/src/IntelliTect.AspNetCore.SignalR.SqlServer/Internal/SqlServer/install.sql @@ -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, diff --git a/src/IntelliTect.AspNetCore.SignalR.SqlServer/SqlServerOptions.cs b/src/IntelliTect.AspNetCore.SignalR.SqlServer/SqlServerOptions.cs index 4990714..893c04d 100644 --- a/src/IntelliTect.AspNetCore.SignalR.SqlServer/SqlServerOptions.cs +++ b/src/IntelliTect.AspNetCore.SignalR.SqlServer/SqlServerOptions.cs @@ -46,6 +46,17 @@ public class SqlServerOptions /// public bool AutoEnableServiceBroker { get; set; } = false; + /// + /// + /// 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 install.sql + /// script in this project's repository, changing the @SCHEMA_NAME, @MESSAGE_TABLE_COUNT, + /// and @MESSAGE_TABLE_NAME variables to match your configuration. + /// + /// + public bool AutoInstallSchema { get; set; } = true; + /// /// Flag enum that specifies the allowed modes for retrieving messages from SQL Server. Default Auto. ///