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

Migrations added #24

Open
wants to merge 1 commit into
base: master
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
42 changes: 42 additions & 0 deletions Configuration/DBInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using DSM.UI.Api.Helpers;
using Microsoft.EntityFrameworkCore;

namespace DSM.UI.Api.Configuration
{
public class DBInitializer
{
public enum DBContextType
{
DSMStorageServer,
DSMAuthServer,
DSMVCenterServer
}

public static void Initialize(string connectionString, DBContextType type)
{
switch (type)
{
case DBContextType.DSMStorageServer:
using (DSMStorageDataContext context = new DSMStorageDataContext(connectionString))
{
context.Database.Migrate();
}
break;
case DBContextType.DSMAuthServer:
using (DSMAuthDbContext context = new DSMAuthDbContext(connectionString))
{
context.Database.Migrate();
}
break;
case DBContextType.DSMVCenterServer:
using (DSMVCenterDbContext context = new DSMVCenterDbContext(connectionString))
{
context.Database.Migrate();
}
break;
default:
break;
}
}
}
}
9 changes: 9 additions & 0 deletions Enums/RowState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DSM.UI.Api.Enums
{
public enum RowState
{
Deleted = -1,
Disabled = 0,
Enabled = 1
}
}
8 changes: 8 additions & 0 deletions Helpers/DSMAuthDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
using DSM.UI.Api.Models.User;
using DSM.UI.Api.Models.WebAccessLogs;
using Microsoft.EntityFrameworkCore;
using System;

namespace DSM.UI.Api.Helpers
{
public class DSMAuthDbContext : DbContext
{
private string _connectionString;
public DSMAuthDbContext(DbContextOptions<DSMAuthDbContext> options) : base(options) { }

public DSMAuthDbContext(string connectionString)
{
this._connectionString = connectionString;
this.Database.SetCommandTimeout(TimeSpan.FromMinutes(1));
}

public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Domain> Domains { get; set; }
Expand Down
8 changes: 8 additions & 0 deletions Helpers/DSMStorageDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
using DSM.UI.Api.Models.Reports;
using DSM.UI.Api.Models.Server;
using Microsoft.EntityFrameworkCore;
using System;

namespace DSM.UI.Api
{
public class DSMStorageDataContext : DbContext
{
private static DSMStorageDataContext _instance;
private string _connectionString;
public static DSMStorageDataContext GetInstance()
{
if (_instance == null)
Expand All @@ -28,6 +30,12 @@ public DSMStorageDataContext(DbContextOptions options) : base(options)
{
}

public DSMStorageDataContext(string connectionString)
{
this._connectionString = connectionString;
this.Database.SetCommandTimeout(TimeSpan.FromMinutes(1));
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DbInventory>(entity =>
Expand Down
20 changes: 20 additions & 0 deletions Helpers/DSMVCenterDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Diagnostics.CodeAnalysis;

namespace DSM.UI.Api.Helpers
{
public class DSMVCenterDbContext : DbContext
{
private string _connectionString;

public DSMVCenterDbContext([NotNull] DbContextOptions options) : base(options) { }

public DSMVCenterDbContext(string connectionString)
{
this._connectionString = connectionString;
this.Database.SetCommandTimeout(TimeSpan.FromMinutes(1));
}

}
}
12 changes: 12 additions & 0 deletions Helpers/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace DSM.UI.Api.Helpers
{
public static class DateTimeExtensions
{
public static DateTime DefaultSqlDateTime(this DateTime dateTime)
{
return new DateTime(1900, 01, 01);
}
}
}
42 changes: 42 additions & 0 deletions Migrations/Functions/StorageServer/0001CreateSQLToGenericType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using System.Diagnostics.CodeAnalysis;

namespace DSM.UI.Api.Migrations.Functions.StorageServer
{
[DbContext(typeof(DSMStorageDataContext))]
[Migration("ZX0001-CreateSQLToGenericType_Function")]
public class CreateSQLToGenericType : Migration
{
protected override void Up([NotNull] MigrationBuilder migrationBuilder)
{
string functionCreateQuery = @"
CREATE FUNCTION SQLToGenericType (@TypeName VARCHAR(20))
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @RetVal VARCHAR(20)
SELECT @RetVal =
CASE LOWER(@TypeName)
WHEN 'bigint' THEN 'int64'
WHEN 'nvarchar' THEN 'string'
WHEN 'varchar' THEN 'string'
WHEN 'bit' THEN 'boolean'
WHEN 'datetime2' THEN 'datetime'
WHEN 'datetime' THEN 'datetime'
WHEN 'int' THEN 'int32'
WHEN 'smallint' THEN 'int16'
WHEN 'char' THEN 'string'
WHEN 'money' THEN 'decimal'
WHEN 'real' THEN 'decimal'
WHEN 'float' THEN 'float'
WHEN 'date' THEN 'datetime'
ELSE @TypeName
END
RETURN (@RetVal)
END
";
migrationBuilder.Sql(sql: functionCreateQuery);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using System.Diagnostics.CodeAnalysis;

namespace DSM.UI.Api.Migrations.StoredProcedures.StorageServer
{
[DbContext(typeof(DSMStorageDataContext))]
[Migration("ZY0001-CreateSpSiteDeleteStatus_StoredProcedure")]
public class CreateSpSiteDeleteStatus : Migration
{
protected override void Up([NotNull] MigrationBuilder migrationBuilder)
{
string spCreateQuery = @"
CREATE PROCEDURE SP_SITE_DELETE_STATUS
AS
BEGIN
UPDATE
dbo.IISSite
SET
DateDeleted = Sites.LastUpdated
FROM
dbo.Servers AS Srv WITH (NOLOCK)
INNER JOIN dbo.IISSite AS Sites
ON Sites.MachineName = SUBSTRING(Srv.HostName,0,CHARINDEX('.', Srv.HostName))
INNER JOIN
(SELECT
ServerName, Max(LastUpdated) AS LastUpdate
FROM
dbo.Servers AS Srv WITH (NOLOCK)
INNER JOIN dbo.IISSite AS Sites
ON Sites.MachineName = SUBSTRING(Srv.HostName,0,CHARINDEX('.', Srv.HostName))
GROUP BY
ServerName) AS TServer
WHERE
DATEDIFF(DAY, Sites.LastUpdated, TServer.LastUpdate) > 1
OR Srv.PowerState = 0

UPDATE
dbo.IISSite
SET
DateDeleted = ST.LastUpdated
FROM
dbo.IISSite AS ST WITH (NOLOCK)
LEFT OUTER JOIN dbo.Servers AS SRV WITH (NOLOCK)
ON ST.MachineName = SUBSTRING( SRV.HostName, 0, CHARINDEX( '.', SRV.HostName))
WHERE
SRV.ServerName IS NULL
END
";
migrationBuilder.Sql(sql: spCreateQuery);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using System.Diagnostics.CodeAnalysis;

namespace DSM.UI.Api.Migrations.StoredProcedures.StorageServer
{
[DbContext(typeof(DSMStorageDataContext))]
[Migration("ZY0002-CreateGetProcedureParameters_StoredProcedure")]
public class CreateGetProcedureParameters : Migration
{
protected override void Up([NotNull] MigrationBuilder migrationBuilder)
{
string spCreateQuery = @"
CREATE PROCEDURE GetProcedureParameters
(
@ProcedureName VARCHAR(120)
)
AS
BEGIN
SELECT
SUBSTRING(Parameters.name,2,LEN(Parameters.name)-1) AS ParamName,
dbo.SQLToGenericType(Types.Name) AS TypeName,
Parameters.max_length AS [MaxLength],
0 AS Nullable
FROM
sys.parameters AS Parameters
INNER JOIN sys.types AS Types
ON Parameters.user_type_id = Types.user_type_id
WHERE
Parameters.object_id = OBJECT_ID(@ProcedureName)
END
";
migrationBuilder.Sql(sql: spCreateQuery);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;

namespace DSM.UI.Api.Migrations.StoredProcedures.StorageServer
{
[DbContext(typeof(DSMStorageDataContext))]
[Migration("ZY0003-CreateSpDTIISMCoreAddNewBinding_StoredProcedure")]
public class CreateSpDTIISMCoreAddNewBinding : Migration
{
protected override void Up([NotNull] MigrationBuilder migrationBuilder)
{
string spCreateQuery = @"
-- =============================================
-- Author: Onur Akkaya
-- Create date: 19.03.2019
-- Description: Adds new binding to table
-- =============================================
CREATE PROCEDURE SP_DTIISMCore_AddNewBinding
@BindingInformation NVARCHAR(200),
@Host NVARCHAR(150),
@SiteId INT,
@IsSSLBound BIT,
@IpAddress NVARCHAR(64),
@IpAddressFamily NVARCHAR(50),
@Port INT,
@Protocol NVARCHAR(40)
AS
BEGIN
DECLARE @CNT INT
SELECT
@CNT = COUNT(*)
FROM
dbo.IISSiteBinding
WHERE
Host= @Host
AND SiteId = @SiteId
AND Port = @Port

IF @CNT > 0
UPDATE
dbo.IISSiteBinding
SET
IsSSLBound = @IsSSLBound, IpAddress = @IpAddress, IpAdressFamily = @IpAddressFamily, Port = @Port, Host = @Host, Protocol = @Protocol,
SiteId= @SiteId
OUTPUT
INSERTED.[Id]
WHERE
Host= @Host
AND SiteId = @SiteId
AND Port =@Port
ELSE
INSERT INTO dbo.IISSiteBinding
( IsSSLBound, IpAddress, IpAdressFamily, Port, Host, Protocol,
SiteId)
OUTPUT
INSERTED.[Id]
VALUES( @IsSSLBound, @IpAddress, @IpAddressFamily, @Port, @Host, @Protocol,
@SiteId)
END
";
migrationBuilder.Sql(sql: spCreateQuery);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using System.Diagnostics.CodeAnalysis;

namespace DSM.UI.Api.Migrations.StoredProcedures.StorageServer
{
[DbContext(typeof(DSMStorageDataContext))]
[Migration("ZY0004-CreateSpDTIISMCoreAddNewBindingMultiple_StoredProcedure")]
public class CreateSpDTIISMCoreAddNewBindingMultiple : Migration
{
protected override void Up([NotNull] MigrationBuilder migrationBuilder)
{
string spCreateQuery = @"
-- =============================================
-- Author: Onur Akkaya
-- Create date: 19.03.2019
-- Description: Add new site to table
-- =============================================
CREATE PROCEDURE SP_DTIISMCore_AddNewBindingMultiple
@Table SiteBindingObj READONLY
AS
BEGIN
MERGE dbo.IISSiteBinding AS SourceBinding USING @Table AS DestBinding
ON (SourceBinding.Host = DestBinding.Host AND SourceBinding.Port = DestBinding.Port AND SourceBinding.SiteId = DestBinding.SiteId)
WHEN MATCHED THEN UPDATE
SET IsSSLBound = DestBinding.IsSSLBound, IpAddress = DestBinding.IpAddress,
IpAdressFamily = DestBinding.IpAddressFamily, Port = DestBinding.Port, Host= DestBinding.Host, Protocol = DestBinding.Protocol,
SiteId = DestBinding.SiteId
WHEN NOT MATCHED BY TARGET THEN INSERT
(IsSSLBound, IpAddress, IpAdressFamily, Port, Host, Protocol,
SiteId)
VALUES
(DestBinding.IsSSLBound, DestBinding.IpAddress, DestBinding.IpAddressFamily, DestBinding.Port, DestBinding.Host, DestBinding.Protocol,
DestBinding.SiteId);

SELECT
Id, IsSSLBound, IpAddress, IpAdressFamily, Port,
Host, Protocol, SiteId
FROM
dbo.IISSiteBinding
WHERE
Host IN (SELECT Host FROM @Table)
AND Port IN (SELECT Port FROM @Table)
AND SiteId IN (SELECT SiteId FROM @Table)
RETURN
END
";
migrationBuilder.Sql(sql: spCreateQuery);
}
}
}
Loading