From 521350f97192061b0dd0f2c1b11e3864d1c2e615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentina=20Cupa=C4=87?= Date: Sat, 18 Jun 2022 19:40:21 +0200 Subject: [PATCH] #25 Infrastructure - BankAccountRepository - EF (Added migrations) --- README.md | 10 +++- ...0220618173926_InitialMigration.Designer.cs | 60 +++++++++++++++++++ .../20220618173926_InitialMigration.cs | 36 +++++++++++ .../DatabaseContextModelSnapshot.cs | 58 ++++++++++++++++++ ...Optivem.Kata.Banking.Infrastructure.csproj | 7 ++- .../Persistence/DatabaseContextFactory.cs | 22 +++++++ .../Optivem.Kata.Banking.Web.csproj | 4 -- .../System/BankAccountControllerSystemTest.cs | 2 +- 8 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.Designer.cs create mode 100644 src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.cs create mode 100644 src/Optivem.Kata.Banking.Infrastructure/Migrations/DatabaseContextModelSnapshot.cs create mode 100644 src/Optivem.Kata.Banking.Infrastructure/Persistence/DatabaseContextFactory.cs diff --git a/README.md b/README.md index 73fba6b..accb69c 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,14 @@ dotnet test Optivem.Kata.Banking.sln ## Migrations +To add a migration: + +``` +dotnet ef migrations add NameOfSomeMigration --project .\src\Optivem.Kata.Banking.Infrastructure ``` -dotnet ef migrations add InitialMigration --project .\src\Optivem.Kata.Banking.Web + +To remove a migration: + ``` +dotnet ef migrations remove --project .\src\Optivem.Kata.Banking.Infrastructure +``` \ No newline at end of file diff --git a/src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.Designer.cs b/src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.Designer.cs new file mode 100644 index 0000000..9507700 --- /dev/null +++ b/src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.Designer.cs @@ -0,0 +1,60 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Optivem.Kata.Banking.Infrastructure.Persistence; + +#nullable disable + +namespace Optivem.Kata.Banking.Infrastructure.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20220618173926_InitialMigration")] + partial class InitialMigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("Optivem.Kata.Banking.Infrastructure.Persistence.BankAccountRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("AccountNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Balance") + .HasColumnType("int"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("BankAccounts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.cs b/src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.cs new file mode 100644 index 0000000..348e5ed --- /dev/null +++ b/src/Optivem.Kata.Banking.Infrastructure/Migrations/20220618173926_InitialMigration.cs @@ -0,0 +1,36 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Optivem.Kata.Banking.Infrastructure.Migrations +{ + public partial class InitialMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "BankAccounts", + columns: table => new + { + Id = table.Column(type: "bigint", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + AccountNumber = table.Column(type: "nvarchar(max)", nullable: false), + FirstName = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + Balance = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BankAccounts", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BankAccounts"); + } + } +} diff --git a/src/Optivem.Kata.Banking.Infrastructure/Migrations/DatabaseContextModelSnapshot.cs b/src/Optivem.Kata.Banking.Infrastructure/Migrations/DatabaseContextModelSnapshot.cs new file mode 100644 index 0000000..d60251c --- /dev/null +++ b/src/Optivem.Kata.Banking.Infrastructure/Migrations/DatabaseContextModelSnapshot.cs @@ -0,0 +1,58 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Optivem.Kata.Banking.Infrastructure.Persistence; + +#nullable disable + +namespace Optivem.Kata.Banking.Infrastructure.Migrations +{ + [DbContext(typeof(DatabaseContext))] + partial class DatabaseContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("Optivem.Kata.Banking.Infrastructure.Persistence.BankAccountRecord", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("AccountNumber") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Balance") + .HasColumnType("int"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("BankAccounts"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Optivem.Kata.Banking.Infrastructure/Optivem.Kata.Banking.Infrastructure.csproj b/src/Optivem.Kata.Banking.Infrastructure/Optivem.Kata.Banking.Infrastructure.csproj index 4174dca..6c96ca3 100644 --- a/src/Optivem.Kata.Banking.Infrastructure/Optivem.Kata.Banking.Infrastructure.csproj +++ b/src/Optivem.Kata.Banking.Infrastructure/Optivem.Kata.Banking.Infrastructure.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -8,6 +8,11 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/src/Optivem.Kata.Banking.Infrastructure/Persistence/DatabaseContextFactory.cs b/src/Optivem.Kata.Banking.Infrastructure/Persistence/DatabaseContextFactory.cs new file mode 100644 index 0000000..d20cdad --- /dev/null +++ b/src/Optivem.Kata.Banking.Infrastructure/Persistence/DatabaseContextFactory.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Optivem.Kata.Banking.Infrastructure.Persistence +{ + public class DatabaseContextFactory : IDesignTimeDbContextFactory + { + public DatabaseContext CreateDbContext(string[] args) + { + var connectionString = "Data Source=localhost;Initial Catalog=BankingKata;Integrated Security=True;MultipleActiveResultSets=True;"; + var optionsBuilder = new DbContextOptionsBuilder(); + optionsBuilder.UseSqlServer(connectionString); + + return new DatabaseContext(optionsBuilder.Options); + } + } +} diff --git a/src/Optivem.Kata.Banking.Web/Optivem.Kata.Banking.Web.csproj b/src/Optivem.Kata.Banking.Web/Optivem.Kata.Banking.Web.csproj index 3347096..6e72ea7 100644 --- a/src/Optivem.Kata.Banking.Web/Optivem.Kata.Banking.Web.csproj +++ b/src/Optivem.Kata.Banking.Web/Optivem.Kata.Banking.Web.csproj @@ -8,10 +8,6 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/test/Optivem.Kata.Banking.Test/System/BankAccountControllerSystemTest.cs b/test/Optivem.Kata.Banking.Test/System/BankAccountControllerSystemTest.cs index 1344896..8280c25 100644 --- a/test/Optivem.Kata.Banking.Test/System/BankAccountControllerSystemTest.cs +++ b/test/Optivem.Kata.Banking.Test/System/BankAccountControllerSystemTest.cs @@ -44,7 +44,7 @@ public async Task Nothing() } - [Fact] + [Fact(Skip = "In progress")] public async Task Should_open_bank_account_given_valid_request() { var url = "bank-accounts";