Browse Source

update batchsql scripts.

master
yangxiaodong 7 years ago
parent
commit
055bf94b19
1 changed files with 14 additions and 27 deletions
  1. +14
    -27
      src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorage.cs

+ 14
- 27
src/DotNetCore.CAP.EntityFrameworkCore/SqlServerStorage.cs View File

@@ -4,52 +4,42 @@ using System.Threading;
using System.Threading.Tasks;
using Dapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace DotNetCore.CAP.EntityFrameworkCore
{
public class SqlServerStorage : IStorage
{
private readonly IServiceProvider _provider;
private readonly SqlServerOptions _options;
private readonly ILogger _logger;

public SqlServerStorage(
IServiceProvider provider,
ILogger<SqlServerStorage> logger)
public SqlServerStorage(ILogger<SqlServerStorage> logger, SqlServerOptions options)
{
_provider = provider;
_options = options;
_logger = logger;
}

public async Task InitializeAsync(CancellationToken cancellationToken)
{
using (var scope = _provider.CreateScope())
{
if (cancellationToken.IsCancellationRequested) return;

var provider = scope.ServiceProvider;
var options = provider.GetRequiredService<SqlServerOptions>();
if (cancellationToken.IsCancellationRequested) return;

var sql = CreateDbTablesScript(options.Schema);
var sql = CreateDbTablesScript(_options.Schema);

using (var connection = new SqlConnection(options.ConnectionString))
{
await connection.ExecuteAsync(sql);
}
_logger.LogDebug("Ensuring all create database tables script are applied.");
using (var connection = new SqlConnection(_options.ConnectionString))
{
await connection.ExecuteAsync(sql);
}
_logger.LogDebug("Ensuring all create database tables script are applied.");
}

protected virtual string CreateDbTablesScript(string schema)
{
var batchSql =
$@"
$@"
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{schema}')
BEGIN
EXEC('CREATE SCHEMA {schema}')
END
GO
END;

IF OBJECT_ID(N'[{schema}].[Queue]',N'U') IS NULL
BEGIN
@@ -57,8 +47,7 @@ BEGIN
[MessageId] [int] NOT NULL,
[MessageType] [tinyint] NOT NULL
) ON [PRIMARY]
END
GO
END;

IF OBJECT_ID(N'[{schema}].[Received]',N'U') IS NULL
BEGIN
@@ -76,8 +65,7 @@ CREATE TABLE [{schema}].[Received](
[Id] ASC
)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]
END
GO
END;

IF OBJECT_ID(N'[{schema}].[Published]',N'U') IS NULL
BEGIN
@@ -94,8 +82,7 @@ CREATE TABLE [{schema}].[Published](
[Id] ASC
)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]
END
GO";
END;";
return batchSql;
}
}

Loading…
Cancel
Save