Commit 055bf94b authored by yangxiaodong's avatar yangxiaodong

update batchsql scripts.

parent a3d26ab7
...@@ -4,52 +4,42 @@ using System.Threading; ...@@ -4,52 +4,42 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper; using Dapper;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace DotNetCore.CAP.EntityFrameworkCore namespace DotNetCore.CAP.EntityFrameworkCore
{ {
public class SqlServerStorage : IStorage public class SqlServerStorage : IStorage
{ {
private readonly IServiceProvider _provider; private readonly SqlServerOptions _options;
private readonly ILogger _logger; private readonly ILogger _logger;
public SqlServerStorage( public SqlServerStorage(ILogger<SqlServerStorage> logger, SqlServerOptions options)
IServiceProvider provider,
ILogger<SqlServerStorage> logger)
{ {
_provider = provider; _options = options;
_logger = logger; _logger = logger;
} }
public async Task InitializeAsync(CancellationToken cancellationToken) public async Task InitializeAsync(CancellationToken cancellationToken)
{ {
using (var scope = _provider.CreateScope()) if (cancellationToken.IsCancellationRequested) return;
{
if (cancellationToken.IsCancellationRequested) return;
var provider = scope.ServiceProvider;
var options = provider.GetRequiredService<SqlServerOptions>();
var sql = CreateDbTablesScript(options.Schema); var sql = CreateDbTablesScript(_options.Schema);
using (var connection = new SqlConnection(options.ConnectionString)) using (var connection = new SqlConnection(_options.ConnectionString))
{ {
await connection.ExecuteAsync(sql); await connection.ExecuteAsync(sql);
}
_logger.LogDebug("Ensuring all create database tables script are applied.");
} }
_logger.LogDebug("Ensuring all create database tables script are applied.");
} }
protected virtual string CreateDbTablesScript(string schema) protected virtual string CreateDbTablesScript(string schema)
{ {
var batchSql = var batchSql =
$@" $@"
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{schema}') IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{schema}')
BEGIN BEGIN
EXEC('CREATE SCHEMA {schema}') EXEC('CREATE SCHEMA {schema}')
END END;
GO
IF OBJECT_ID(N'[{schema}].[Queue]',N'U') IS NULL IF OBJECT_ID(N'[{schema}].[Queue]',N'U') IS NULL
BEGIN BEGIN
...@@ -57,8 +47,7 @@ BEGIN ...@@ -57,8 +47,7 @@ BEGIN
[MessageId] [int] NOT NULL, [MessageId] [int] NOT NULL,
[MessageType] [tinyint] NOT NULL [MessageType] [tinyint] NOT NULL
) ON [PRIMARY] ) ON [PRIMARY]
END END;
GO
IF OBJECT_ID(N'[{schema}].[Received]',N'U') IS NULL IF OBJECT_ID(N'[{schema}].[Received]',N'U') IS NULL
BEGIN BEGIN
...@@ -76,8 +65,7 @@ CREATE TABLE [{schema}].[Received]( ...@@ -76,8 +65,7 @@ CREATE TABLE [{schema}].[Received](
[Id] ASC [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END END;
GO
IF OBJECT_ID(N'[{schema}].[Published]',N'U') IS NULL IF OBJECT_ID(N'[{schema}].[Published]',N'U') IS NULL
BEGIN BEGIN
...@@ -94,8 +82,7 @@ CREATE TABLE [{schema}].[Published]( ...@@ -94,8 +82,7 @@ CREATE TABLE [{schema}].[Published](
[Id] ASC [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
END END;";
GO";
return batchSql; return batchSql;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment