Skip to content

Commit

Permalink
Add Identity upgrade scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
CK159 committed Mar 11, 2019
1 parent b8b1e4d commit 7afd94d
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Db/Migrations/Identity2Core/01. GenerateEFCoreNewTables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Generate new tables AspNetRoleClaims,AspNetUserTokens
*/

GO
/****** Object: Table [dbo].[AspNetRoleClaims] Script Date: 17/5/2018 7:25:29 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AspNetRoleClaims](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ClaimType] [nvarchar](max) NULL,
[ClaimValue] [nvarchar](max) NULL,
[RoleId] [nvarchar](450) NOT NULL,
CONSTRAINT [PK_AspNetRoleClaims] PRIMARY KEY CLUSTERED
(
[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]
GO
/****** Object: Table [dbo].[AspNetUserTokens] Script Date: 17/5/2018 7:25:29 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AspNetUserTokens](
[UserId] [nvarchar](450) NOT NULL,
[LoginProvider] [nvarchar](450) NOT NULL,
[Name] [nvarchar](450) NOT NULL,
[Value] [nvarchar](max) NULL,
CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY CLUSTERED
(
[UserId] ASC,
[LoginProvider] ASC,
[Name] 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]
GO

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

GO
/****** Object: Table [dbo].[__EFMigrationsHistory] Script Date: 18/5/2018 1:55:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[__EFMigrationsHistory](
[MigrationId] [nvarchar](150) NOT NULL,
[ProductVersion] [nvarchar](32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY CLUSTERED
(
[MigrationId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[__EFMigrationsHistory] ([MigrationId], [ProductVersion]) VALUES (N'00000000000000_CreateIdentitySchema', N'2.0.2-rtm-10011')
GO
71 changes: 71 additions & 0 deletions Db/Migrations/Identity2Core/03. ExistingIdentityTableChanges.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@


/*
AspNetUsers table changes
*/
GO
DROP INDEX [UserNameIndex] ON [AspNetUsers];
GO
EXEC sp_rename N'AspNetUsers.LockoutEndDateUtc', N'LockoutEnd', N'COLUMN';
GO
ALTER TABLE [AspNetUsers] ADD [ConcurrencyStamp] nvarchar(max) NULL;
GO
ALTER TABLE [AspNetUsers] ADD [NormalizedEmail] nvarchar(256) NULL;
GO
ALTER TABLE [AspNetUsers] ADD [NormalizedUserName] nvarchar(256) NULL;
GO
CREATE INDEX [EmailIndex] ON [AspNetUsers] ([NormalizedEmail]);
GO
CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]) WHERE [NormalizedUserName] IS NOT NULL;
GO

/*
AspNetUserRolePermissions table changes
*/
GO
EXEC sp_rename N'AspNetUserRolePermissions.IX_RoleId', N'IX_AspNetUserRolePermissions_RoleId', N'INDEX';
GO

/*
AspNetRoles table changes
*/
GO
DROP INDEX [RoleNameIndex] ON [AspNetRoles];
GO
ALTER TABLE [AspNetRoles] ADD [ConcurrencyStamp] nvarchar(max) NULL;
GO
ALTER TABLE [AspNetRoles] ADD [NormalizedName] nvarchar(256) NULL;
GO
CREATE UNIQUE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]) WHERE [NormalizedName] IS NOT NULL;
GO

/*
AspNetUserClaims table changes
*/
GO
EXEC sp_rename N'AspNetUserClaims.IX_UserId', N'IX_AspNetUserClaims_UserId', N'INDEX';
GO

/*
AspNetUserLogins table changes
*/
GO
ALTER TABLE [AspNetUserLogins] DROP CONSTRAINT [PK_dbo.AspNetUserLogins];
GO
EXEC sp_rename N'AspNetUserLogins.IX_UserId', N'IX_AspNetUserLogins_UserId', N'INDEX';
GO
ALTER TABLE [AspNetUserLogins] ADD [ProviderDisplayName] nvarchar(max) NULL;
GO
ALTER TABLE [AspNetUserLogins] ADD CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]);
GO

/*
AspNetUserRoles table changes
*/
GO
DROP INDEX [IX_UserId] ON [AspNetUserRoles];
GO
EXEC sp_rename N'AspNetUserRoles.IX_RoleId', N'IX_AspNetUserRoles_RoleId', N'INDEX';
GO


102 changes: 102 additions & 0 deletions Db/Migrations/Identity2Core/04. ModifyIdentityColumns.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
This script will upgrade existing column sizes to new sizes (ASP.net Core Identity)
NOTE: Before run this query, please check foreign keys names are as same as in existing names.
*/
--Update Id column of AspNetUsers table and related foreign key columns.

-- Drop foreign key constraints first.
ALTER TABLE AspNetUserLogins DROP CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId]
GO
ALTER TABLE AspNetUserClaims DROP CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId]
GO
ALTER TABLE AspNetUserRoles DROP CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId]
GO

--Alter column size
ALTER TABLE AspNetUsers ALTER COLUMN Id nvarchar(450) NOT NULL;
GO

--Alter column size
ALTER TABLE AspNetUserLogins ALTER COLUMN UserId nvarchar(450) NOT NULL;
GO

--Alter column size
ALTER TABLE AspNetUserClaims ALTER COLUMN UserId nvarchar(450) NOT NULL;
GO

--Alter column size
ALTER TABLE AspNetUserRoles ALTER COLUMN UserId nvarchar(450) NOT NULL;
GO

--Create foreign key constraints again.
ALTER TABLE AspNetUserLogins
ADD CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId] FOREIGN KEY (UserId)
REFERENCES AspNetUsers (Id)
ON DELETE CASCADE
ON UPDATE CASCADE
GO

ALTER TABLE AspNetUserClaims
ADD CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId] FOREIGN KEY (UserId)
REFERENCES AspNetUsers (Id)
ON DELETE CASCADE
ON UPDATE CASCADE
GO

ALTER TABLE AspNetUserRoles
ADD CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId] FOREIGN KEY (UserId)
REFERENCES AspNetUsers (Id)
ON DELETE CASCADE
ON UPDATE CASCADE
GO

-------------------------------------------------------------------------------------------

--Update Id column of AspNetRoles table and related foreign key columns.

-- Drop foreign key constraints first.
ALTER TABLE AspNetUserRoles DROP CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId]
GO

-- Drop foreign key constraints first.
ALTER TABLE AspNetUserRolePermissions DROP CONSTRAINT [FK_dbo.AspNetUserRolePermissions_dbo.AspNetRoles_RoleId]
GO

--Alter column size
ALTER TABLE AspNetRoles ALTER COLUMN Id nvarchar(450) NOT NULL;
GO

--Alter column size
ALTER TABLE AspNetUserRoles ALTER COLUMN RoleId nvarchar(450) NOT NULL;
GO

DROP INDEX [IX_AspNetUserRolePermissions_RoleId] ON [dbo].[AspNetUserRolePermissions]
GO

--Alter column size
ALTER TABLE AspNetUserRolePermissions ALTER COLUMN RoleId nvarchar(450) NOT NULL;
GO

CREATE NONCLUSTERED INDEX [IX_AspNetUserRolePermissions_RoleId] ON [dbo].[AspNetUserRolePermissions]
(
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

--Create foreign key constraints again.
ALTER TABLE AspNetUserRoles
ADD CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId] FOREIGN KEY (RoleId)
REFERENCES AspNetRoles (Id)
ON DELETE CASCADE
ON UPDATE CASCADE
GO

--Create foreign key constraints again.
ALTER TABLE AspNetUserRolePermissions
ADD CONSTRAINT [FK_dbo.AspNetUserRolePermissions_dbo.AspNetRoles_RoleId] FOREIGN KEY (RoleId)
REFERENCES AspNetRoles (Id)
ON DELETE CASCADE
ON UPDATE CASCADE
GO
14 changes: 14 additions & 0 deletions Db/Migrations/Identity2Core/05. AddForeignKeysToNewTables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--Add foreign keys to new tables

ALTER TABLE [dbo].[AspNetRoleClaims] WITH CHECK ADD CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY([RoleId])
REFERENCES [dbo].[AspNetRoles] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[AspNetRoleClaims] CHECK CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId]
GO
ALTER TABLE [dbo].[AspNetUserTokens] WITH CHECK ADD CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[AspNetUsers] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[AspNetUserTokens] CHECK CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId]
GO
30 changes: 30 additions & 0 deletions Db/Migrations/Identity2Core/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Cft.Fbo.AuthService.Models;
using Microsoft.AspNetCore.Identity;

namespace Cft.Fbo.AuthService.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
//builder.Entity<UserRolePermission>().ToTable("AspNetUserRolePermissions");
builder.Entity<IdentityRole>().ToTable("AspNetRoles");
builder.Entity<ApplicationRole>().ToTable("AspNetRoles");
}
}
}
Loading

0 comments on commit 7afd94d

Please sign in to comment.